Center a div using CSS?

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.
LVL 19
Who is Participating?
set margin-left and margin-right to auto

margin: 0 auto 0 auto;
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...

<div class="wrapperbox">
   <div class="centeredbox">
      non-centered text

.wrapperbox { text align: center;}
.centeredbox {margin: auto; text-align:left;}
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

oh, keep in mind that you need to use both parts for total compatability.. text-align *shouldn't* move block elements.
webwomanAuthor Commented:
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.
I have used this technique many times... rest assured it does work. take a look at this: 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.
If you know the width of the div - you can use the margin property:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<title>Page title</title>
<style type="text/css">
<div class="centered">I'm in the middle</div>
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,
Glad to have helped. Thanks for the points!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.