umm.... I don;t know what you mean by elegant... but I just use: <div align="center">Stuff</div> and it works fine.
This is the better way to center it, for use with XHTML: body {text-align:center;}/*For IE to center a page*/ #container {margin:0 auto; text-align:left;} <body> <div id="container"> ... Your Code </div> </body>
If the div is of a specified width (e.g. set in style, used as inline, etc.), you can set margin-left and margin-right to 0. If it's an exact size, you can do "margin-left: 50%; left: -halfTheDiv'sWidth"
I've found the best way to do it that works across most browsers is something like this: setting the left and right margins to auto is important. body{ text-align:center; } #container{ width:640px; margin-left:auto; margin-right:auto; text-align:left; } Code (markup):
Margin 0 auto; is the same as: margin-top: 0; margin-right: auto; margin-bottom: 0; margin-left: auto; Setting a width is important as well, otherwise it won't give much of a centered effect
As mentioned above by others, margin: 0 auto is needed for firefox, whereas text-align: center is needed for IE. Note that you put margin: 0 auto in the actual container which you'd like to center (eg: #content) whereas text-align: center should be put in the outer div of the actual container (eg: body or #wrapper)
IE6 supports {margin: auto;} when in standards mode. Use a complete and proper DTD to trigger standards mode. Only IE5.x and IE6 in quirks mode require the hacky work-around. IE5 is not worth supporting, and there is no sane reason to run IE6 in quirks mode. cheers, gary
It is my experience, and that will vary with coding style I suppose, that the worst that happens to IE5.x is a little bending, but no breakage. In this post's case, IE5 is not centered in the viewport. So what? Unless the client insists on hackery for IE5, and is willing to pay for it, and understands the potential for more bugs, and knows that supporting yet another obsolete browser will increase maintenance costs, I don't do it. The usual case is that once the extra and ongoing costs of IE5 support are explained, and once the client sees what a piddling difference the extra cost makes, that support requirement is quietly dropped. cheers, gary