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:
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 thewidth
was greater than that...To center horizontally, you can use the
margin: auto;
attribute in css. Also, you'll have to make sure that thebody
andhtml
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
2009-06-05 01:47:42
+9
A:
<body>
<div style="width:800px; margin:0 auto;">
centered content
</div>
</body>
AgileJon
2009-06-05 01:49:10
This is correct for demo purposes, but obviously not using inline styles in the final markup
gonzohunter
2009-06-05 10:36:57
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
2009-06-05 15:08:36
+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.
2009-06-05 07:54:07