Solved

Center a div using CSS?

Posted on 2003-11-04
9
47,667 Views
Last Modified: 2011-08-18
Is there a way to center a div using CSS? NOT text-align -- that centers the TEXT inside it, but not the entire div. I do NOT want to center the text.

If I use align=center it works perfectly. However, that doesn't validate.
0
Comment
Question by:webwoman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Expert Comment

by:Fendrin
ID: 9680892
set margin-left and margin-right to auto

margin: 0 auto 0 auto;
0
 
LVL 4

Assisted Solution

by:Fendrin
Fendrin earned 125 total points
ID: 9680942
or of course, what ever top and bottom margin you want..i just put those zeroes there as placeholders...

also, to warn you, there are some old browsers (IE5/Mac?) that don't center like this correctly. However, as a work around, you can use 'text-align: center' on the parent div and then 'text-align:left' on the div iteslf...

(X)HTML
<div class="wrapperbox">
   <div class="centeredbox">
      non-centered text
   </div>
</div>

CSS
.wrapperbox { text align: center;}
.centeredbox {margin: auto; text-align:left;}
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9680957
oh, keep in mind that you need to use both parts for total compatability.. text-align *shouldn't* move block elements.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Author Comment

by:webwoman
ID: 9681479
Text-align does NOT work. It aligns the text INSIDE the div. That is NOT what I want.
Margins don't work either, I tried that already. It sets the margin to what I want, but that's NOT what I'm looking for. That is NOT centered. It puts the div whatever margin I set, that is NOT centered.

Try it yourself. It doesn't work.
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9682208
I have used this technique many times... rest assured it does work. take a look at this: www.lebor.net/~fendrin/centertest.htm go ahead and look at the soucre code. the only thing different i did was to explicitly set the width of the centered block

one of two things is happening here...
1) you have not implemented what I intended correctly (whether it be a mistake on your part, a mistake on my part, a simple misunderstanding.. what ever)
2) what you are looking to do is more complicated that what you have said... perhaps if you post your code, or give more context, I might be able to come up with a solution.
0
 
LVL 31

Assisted Solution

by:seanpowell
seanpowell earned 125 total points
ID: 9682210
If you know the width of the div - you can use the margin property:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page title</title>
<style type="text/css">
<!--
.centered
    {
    position:absolute;
    width:200px;
    left:50%;
    margin-left:-100px;
    background-color:#CCCCCC;
    height:100%;
    top:0px;
    }
//-->
</style>
</head>
<body>
<div class="centered">I'm in the middle</div>
</body>
</html>
0
 
LVL 17

Accepted Solution

by:
dorward earned 200 total points
ID: 9684969
0
 
LVL 8

Assisted Solution

by:tncbbthositg
tncbbthositg earned 50 total points
ID: 9701569
Webwoman, the only way I have found to produce this effect is similar to the method USED (as opposed to suggested) by Fendrin.  What Fendrin did was create a container that centers the item.  If you embed a div in a div and center the daughter div in the parent div, it works fine in IE, NS, Moz, and Firebird (even most older versions).

Another option is to stick with tables.  Last I checked (a while ago) tables were not only implemented in the XHTML Strict DTD, but there are no plans to deprecate the use of tables.  They are a very effective means of achieving desired layout.

. . . for what it's worth.


Good luck Webwoman,
TNC
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9704132
Glad to have helped. Thanks for the points!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question