Solved

Center a div using CSS?

Posted on 2003-11-04
9
47,662 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
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

708 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now