CSS Centering fixed-width layouts
Do you ever wonder how you can center a containing div?
The use of “tables” or “center” tag makes the job a lot easier, but it will fail validation.
Another way of doing this is using CSS stylesheet.
body{padding:0px; margin:0px; text-align:center;}
#div_container{margin-left: auto;margin-right: auto;width:800px;}
The only problem with the above solution is that “#div_container” will also have the text centered.
To overcome this problem you have to specify the “text-align” tag as below:
body{padding:0px; margin:0px; text-align:center;} #div_container{margin-left: auto;margin-right: auto;width:800px;text-align:left;}
W3C Visual formatting model explains: “If both ‘margin-left’ and ‘margin-right’ are ‘auto’, their used values are equal.
This horizontally centers the element with respect to the edges of the containing block.”

















December 14th, 2007 at 5:39 pm
Hey Joe,
Thanks for the pointer and a nice Welcome to our blog.
I love to write more but not enough time.