Forum Moderators: not2easy

Message Too Old, No Replies

Centering a div is simple

No hacks needed to center divisions

         

Mark the Webber

10:43 am on Feb 29, 2008 (gmt 0)

10+ Year Member



I was looking for info about how to horizontally center a div in a browser. I found a lot of hacks to make it visible in different browsers. But when you use the right doctype (xhtml of html 4.1) there are no hacks needed.

E.G. doctype like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

CSS:
/* properties of the body */
#body {
margin: 0px;

}

/* properties of centered div*/
#centereddiv {
display:block;
margin:0px auto;
width:200px;
height:200px;

}

I hope this is usefull for people who still use text-align for centering.

rocknbil

4:13 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard Mark, isn't a div display:block by default? :-)

margin:auto also works with a valid 4.01 doctype, no real need to use XHTML unless you're actually doing extensible markup. What bugs me, with all I (seem to?) know, a raw image by itself won't accept certain positioning attributes, I always have to wrap it in something.

penders

5:17 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What bugs me, with all I (seem to?) know, a raw image by itself won't accept certain positioning attributes, I always have to wrap it in something.

May be because IMG's are inline elements, not blocks?

Wlauzon

11:21 pm on Feb 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, obviously, someone needs to come up with a block image format!

vol7ron

12:45 am on Mar 1, 2008 (gmt 0)

10+ Year Member



#body {
margin: 0px;
}

There should be no "#", that is saying body is an element, rather than the body itself. It would be:

body {margin: 0px;}

I agree with the fact that they should be using standards, but I think looking at margins and knowing a page is centered only comes with experience so new users like to see "text-align:center;" so they can easily find where to change it if they want.

The problem is that IE has been pretty robust in that they follow standards but permit non-standards, thus allowing poor cross-browser coding.

It might not be standards compliant, but for those used to text-align:

body {
margin:0px;
text-align:center;
}
body {
text-align:-moz-center;
}

#centereddiv {width:800px;}

This will center the DIV and text on the page all in once. Againt, it doesn't follow W3C standards. So use the margins as the first post suggested.

epersidi

11:11 am on Mar 1, 2008 (gmt 0)

10+ Year Member



Worked for me in firefox but not working in IE6. Any ideas why ?

SuzyUK

2:50 pm on Mar 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld epersidi..

which one didn't work for you the one in the first post?

vol7ron

4:39 pm on Mar 1, 2008 (gmt 0)

10+ Year Member



If you are to use the margins approach (the first post), you have to make sure to set a width to your DIV.

Your HTML should look something like:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>

<title></title>

<style type="text/javascript">

body {

margin: 0px;

}

#centereddiv {

display: block;

margin: 0px auto;

width: 200px;

height: 200px;

}

</style>

</head>

<body>

<div id="centereddiv">test</div>

</body>

</html>

LadynRed

1:25 am on Mar 2, 2008 (gmt 0)

10+ Year Member



No need for the -moz filter at all. Centering is easy;
1 - define a width for the element you want to center, typically a 'container' or 'wrapper' div.
2 - set margins to margin: 0 auto; (top/bottom = 0, left/right = auto)

To make IE5.5 and lower center, you'll need to add text-align: center on the body, then add text-align: left to the container to fix the text alignment. If you're not worried about that old a version of IE, you don't need the text-align stuff.

Display block is extraneous and not needed for centering a div.

penders

12:45 pm on Mar 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I hope this is usefull for people who still use text-align for centering.

I agree with the fact that they should be using standards, but I think looking at margins and knowing a page is centered only comes with experience so new users like to see "text-align:center;" so they can easily find where to change it if they want.

Just to add, text-align is for centreing inline text, not blocks of content. It will not work to centre a content block in any standards compliant browser (inc. IE6+ in stds mode). However, it will (incorrectly) centre a block-level element in IE6+ (in quirks mode) and in IE5/5.5 (regardless of mode and which doesn't support

margin:0 auto;
) as LadynRed states above.