views:

3039

answers:

4

Hi! I have a div tag with hight set to 800px, I want that when the browser width is greater than 800px it shouldn't stretch the div but it should bring it to the middle of the page.

+3  A: 
  1. Do you mean that you want to center it vertically or horizontally? You said you specified the height to 800px, and wanted the div not to stretch when the width was greater than that...

  2. To center horizontally, you can use the margin: auto; attribute in css. Also, you'll have to make sure that the body and html elements don't have any margin or padding:

    html, body { margin: 0; padding: 0; }
    #centeredDiv { margin-right: auto; margin-left: auto; width: 800px; }
    
Tomas Lycken
+9  A: 
<body>
    <div style="width:800px; margin:0 auto;">
        centered content
    </div>
</body>
AgileJon
This is correct for demo purposes, but obviously not using inline styles in the final markup
gonzohunter
Just make sure to apply 'text-align: center' to the <body> or else IE6 will not center the div. Then add text-align: left; to your div.
avdgaag
A: 
<div style="text-align:center">
  <div>centered</div>
</div>
AK
+2  A: 

To make it also work correctly in Internet Explorer you have to do it as following:

HTML

<body>
    <div class="centered">
        centered content
    </div>
</body>

CSS

body {
    margin: 0;
    padding: 0;
    text-align: center; /* !!! */
}

.centered {
    margin: 0 auto;
    text-align: left;
    width: 800px;
}
Kevin D.