How to align a <div> to the middle (horizontally/width) of the page

Shimmy Weitzhandler picture Shimmy Weitzhandler · Jun 5, 2009 · Viewed 2.7M times · Source

I have a div tag with width set to 800 pixels. When the browser width is greater than 800 pixels, it shouldn't stretch the div, but it should bring it to the middle of the page.

Answer

AgileJon picture AgileJon · Jun 5, 2009
<body>
    <div style="width:800px; margin:0 auto;">
        centered content
    </div>
</body>