I don’t know about you, but I always get frustrated trying to figure out how to get my layout to stretch vertically to 100% of the page. I have a div that I want to stretch, but it just doesn’t stretch. Now why wouldn’t it do that? Today I will share the solution with you.
Say you have coded an HTML file like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> <title>CSS 100% Height</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="content"> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. </div> </body> </html>
And you have a CSS file like this:
body {
margin: 0;
padding: 0;
}
#content {
background: #EEE;
border-left: 1px solid #000;
border-right: 1px solid #000;
padding: 0 20px 0 20px;
margin: auto;
font: 1.5em arial, verdana, sans-serif;
width: 960px;
height: 100%;
}
That gives you this example file. As you can see, the content box on that page doesn’t stretch to the whole height of the page, even though we added the “height: 100%;” line to the CSS file. Why is that?
Let me give you a different way of thinking about HTML. HTML is pretty much just a bunch of containers stacked inside each other. So in our page, first we have the <html> container, then the <body> container inside of that, and finally the <div id=”content”> container inside of that. When we put content into one of those boxes, it stretches that box and all the boxes containing that box so that they can hold the new content. So when we put our text into the <div id=”content”> box, that box streches, which in turn stretches all of the boxes that it is in (in our case the <body> and <html> boxes).
When we put the “height: 100%;” style onto the <div id=”content”> box, what we are doing is telling it to stretch to the full height of the box that it is in. In this example, the box that it is in is the <body> box. So the <div id=”content”> box is 100% of the height of the <body> box. Well, how tall is the <body> box? It’s just as tall as the <div id=”content”> box, because we never told it how tall it should be! So when we put the “height: 100%;” style onto the <div id=”content”> box, we are just telling it to be as tall as itself!
To fix this, we need to tell the <body> box to get bigger. But then we run into the same problem with the <html> box – it is only as big as the <body> box! So to fix our problem (to get the <div id=”content”> box to stretch the whole height of the page), we need to tell the <html> and <body> boxes to be the full height of the page.
So if we change our CSS file to this:
html {
min-height: 100%;
}
body {
margin: 0;
padding: 0;
min-height: 100%;
}
#content {
background: #EEE;
border-left: 1px solid #000;
border-right: 1px solid #000;
padding: 0 20px 0 20px;
margin: auto;
font: 1.5em arial, verdana, sans-serif;
width: 960px;
min-height: 100%;
}
And that’s it! This is what we have now. The content box is now stretched to the full height of the page!
A big thanks to Mark in the comments for letting me know about using the “min-height” style instead of just “height”. The reason for doing this is to make sure that if there is enough text in the content box to stretch it below the bottom of the page, the design won’t get distorted.




September 11, 2008 in 









[...] the screen and not just stop. This is what I can't seem to figure out how to do. I even found this CSS 100% Height | TutWow and tried doing it like it says (setting html & body to min-height: 100%) but that didn't work [...]
So after not getting this to work properly and comparing your example css (above) to the actual css attached to your example pages, “min-height:100%” doesn’t work, just use “height:100%”
Hi, thought I’d drop this tidbit in:
Using the following used to work fine until the latest release of Firefox (3.5.2):
html {
font-size: 100%;
height: 100%;
margin-bottom: 1px;
}
But since the release of this new FF, I’m having to change height: 100%; to height: 101%; (min-height: 100%; – or – min-height: 101%; did nothing).
(I ran across others that said 100.1% or 100.05%, but neither work in the latest Firefox.)
So what I ended up with is:
html {
font-size: 100%;
height: 101%;
margin-bottom: 1px;
}
Hope it helps someone….
-D-
I’m trying to use this to force a div w/ a background image to have 100% height. When I put the height into the body and html CSS sections, the page will no longer scroll when resized smaller than the content.
Nevermind i found the solution on another page…
bgcontainer {min-height: 100%;} /* for proper browsers */
* html bgcontainer {height: 100%;} /* for IE */
There’s still one little issue that I can’t seem to resolve.
This works great, until you vertically shrink your window to the point where there’s a scroll bar. Now, when you scroll down, you lose your background.
Any thoughts? I can’t seem to find the solution.
Thanks,
Kyle
for all browsers including internet exploder
min-height: 500px;
height:auto !important;
height: 500px;
Woo hoo! Big help, thanks for the great post. I’ve been pulling my hair out trying to figure out how to make those pesky divs stretch 100%. Nice blog BTW =)
I’ve found this solution all over the web, but it still doesn’t solve a nagging issue I’m having, which is to have the divs nested inside the 100% height “container” also fill vertically when they don’t have content.
As far as I can tell they only fill when you use height:100% on the container. As soon as you change it to min-height:100%, the children divs no longer calculate their height and end up as though you have no 100% height attribute on them. Sure the container still fills vertically, but how to get the children to follow suit?
I’ve got a student website where I’ve tried to do this layout and outlined the problem here: http://edison.seattlecentral.edu/~jmohr001/
Is it possible? I’ve been working on every solution I’ve come across on the internet, and there doesn’t appear to be a way without using javascript or something. I’d like to find a pure HTML/CSS solution to this.
BTW, I found the “faux columns” method after finding this post. I found it very helpful. You can read about it here (not my blog at all, not a promotional thing, just a helpful explanation:)
http://www.positioniseverything.net/articles/onetruelayout/equalheight
Hope it helps someone.
I have been trawling the web trying to find an answer to my css nightmare question.
The main problem seems to be getting a min-height:580px and a height:100%
The thing I need to achieve is:
1. Site to have a min height of 580px including footer so total height before scroll bars appear is 580px.
2. Also, a min width is needed of 930px including right and left margin of 15px each side.
3. left menu of 216px wide and 100% high minus the footer height of 30px.
4. Main part of the screen should fill all the space available. Unless the screen height is less than 580px or width less than 930px. In this case you get scroll bars.
5. Compatiblity ie6,7,8, Firefox and Safari.
Can it be done with no Javascript?
Really great entry to hang on.. I
Thanks for another informative entry. Where else would anyone get that kind of information in such a great way of writing.
Really interesting stuff. I loved it. Thanks for sharing these type of works with developers. Thanks once again.