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 









I was just trying to figure this out the other day! I never did figure it out… I just put min-height: 950px; or something like that. My page is generally that long or longer anyway, but this was very frustrating and I’m glad I finally found the answer!
This is definitely something that people should remember, I myself have always found a problem with using height:100% in CSS so I just carried on sticking with tables as that works fine. However I can now do it in CSS.
Thank you.
This solution will only work if the content occupies less than 100% of the browser window height, especially in firefox, chrome etc. Still an interesting problem
Useful tutorial ! I want to bookmark your tutorial, but I can not find the social bookmark button. I hope next, it will available on each of your post.
THX !!
Very nice! A good example of thinking outside the box (or container for that matter)!
Thanks!!!
Nice perception, usefull, really …
You have the right idea, but as Paul stated this doesnt work when the content overflows the browser window. I had a similar problem last year in University and found that the height attributes are totally useless. If you want a tip … use JavaScript! I hava one file (that i use over and over) that takes the height of my container, and if it is shorter than the browser height subtracts itself from the height, and is then adjusted using the difference found. Works a treat and I dont have three pointless height tags when I want a lot of content on one page.
oo! that is perfect! Thanks You!
beautiful article .
thank you
Thanks everyone for the positive comments!
@Bailey: Great point! However, since I wasn’t writing a javascript tutorial, I thought I’d stick with pure CSS/HTML. Also, I usually don’t recommend using javascript for layout purposes, because if a visitor doesn’t have it, then the layout gets messed up. Thanks for sharing, though!
Like has already been stated. Once the content grows to be taller than the window, the content starts to look quite odd. I fixed the issue by replacing the ‘height: 100%’ with ‘min-height:100%’.
Wow, so simple, yet elegant. I’ve been fighting with this problem for years. Thanks for the information.
@Lori Cole: I’m glad I could help!
@Mark: Thanks for that tip! I hadn’t experimented with the min-height option for this specific tutorial yet, and you reminded me to try it out.
If you need to put a footer at the bottom of your page, this guy has a cool way to do it: http://ryanfait.com/sticky-footer/
It also uses the 100% height in the body and html tag.
@Monte: I have seen that before – thanks for sharing!
Still, it doesn’t work for me. Firefox doesn’t show the page at 100% height. Neighter does Internet Explorer!
@ST Verschoof: Hmm…I have tested this in all the major current browser versions – Firefox 3, Internet Explorer 7, Safari 3, and Opera 9 – and they all display it perfectly. What does it look like on your computer?
This method causes a lot of problems, mainly with the divs contained within the “body”. If you add more than one div within body, it causes a uncontrollable amount of spacing between the divs across most browsers.
On the rare occasions that a site I am working on needs to have a 100% height, I use a div#pageWrapper to accomplish the task.All other content divs are contained within the div#pageWrapper and this eliminates any weird issues with inherited heights and paddings.
html {
height:100%;
}
body {
height:100%;
width:960px;
margin:0 auto;
}
div#pageWrapper {
background-color:#cccccc;
height:100%;
}
div.content {
font:14px Arial, Helvetica, sans-serif;
margin:0 0 50px 0;
}
**Sorry, not sure if your text area filters for code or not.
I’m sorry for my reaction last time! I forgot to set html height to 100% (haven’t tested it yet, hope it will work now!)
Looks like the right solution, I typically play around with overflows, but this ads the scroll bar at times to the div containers. Either way, will give this a shot.
great article I was using a similar technique but suffering the problem when the content expanded past the browser window. That tip from Mark ‘min-height:100%’ was the solution to my problems very grateful thanks!
Well, that is solution for firefox, what to do while using internet explorer?Maybe use the if conditional statement.
Hmm, does this still work if the divs are floating? Does not seem to on my Web site…
Very Useful. But if the content’s position within the content div isn’t static then the content div is not going to stretch.
So if anyone were to make it stretch down past 100% you have to make other container div’s that are static. as for positioning, you would have to work with margins and paddings. To make div’s sit next to each other, you would have to make the div’s float either left or right. Then you would also have to use the clear attribute to position a div onto a new line.
Basically, learn how to layout a web page with div’s positioned statically and everything should stretch fine. After you get a hang of it it’s not really that bad.
The W3 schools has a great example.
http://www.w3schools.com/CSS/tryit.asp?filename=trycss_float6
@Greg: Thanks for pointing that out! I’m sure that this will come in use for many of the people reading the article.
Doesnt work. Not a good tutorial!
@Rob: What problem do you seem to be having?
@Josh: Hey Josh thanks for the reply!!
http://robslounge.com/test.php
View it on IE. After view it on Firefox, Safari and Opera
and tell me what you think? Could you e-mail me?
I find that it doesnt stretch the container if you use floats on the other divs that are wrapped inside the container
Hey Josh I found the solution to my problem
http://www.quirksmode.org/css/clearing.html
@Rob: I’m glad that you found what you were looking for! Yes, floats can get difficult sometimes – that’s why you should try to learn them extra well.
Agreed! good luck with you Josh and keep up the good work with your website!
*handshake*
Thanks for this tip, I’ve been trying to work this out for ages!
thanks…
I tried for this, but was not able to figure out this. i was fixing this problem with a 1px wide image with specified height. Thanks a lot…
Thanks for useful tutorial! But It is still hard for me to implement height for vertical div which is inside “container” div. Two 100% height vertical stripes for example.
Thanks a lot, now getting the hang of css, this was very helpful.
For those with issues when the content is longer than the window, try putting:
overflow:hidden;
in the HTML class.
@jijeesh: You could do that, but then again you could specify a specific height for a div also.
@Danial: That would technically solve it, but it would just hide the content instead of letting you be able to see it. The better way is to just use “min-height: 100%” in place of “height: 100%”. Although IE6 won’t like this, there are some IE7.js scripts that can fix it.
This did not work at all for me, even when I copied and pasted your code exactly.
Thanks, thanks, thanks. I’d been scratching my head over these for a couple of hours. I’d put min-height on the HTML but not on the body as well. Such a simple solution!!
It finally worked for me using 100% height instead of min-height 100%.
@Milena Snyder: Glad to hear it’s working for you! Strange that it wouldn’t work with min-height. Are you using IE6? It doesn’t support min-height.
@Phil: Yes – I found it while “goofing around” one day, and thought that you guys might want to know it too.
Well, Same was told earlier in 2002 or before on internet.
But problem still persist when you have a header/footer along mid content on LHS is Vertical navigation box & on RHS is text content.
You need to make both(LHS/RHS) 100% stretchable towards bottom whether one is lesser than other.
Any solution highly regarded.
i’ve been searching for this 100% height divs to fill an extra blank space from my site but non of these tutorials over the net really help me out. if i were you just use a #wrapper background to fill those blank spaces. that’s the trick i did in my case.
just put same color or background in your wrapper like the one you are using in your div that you want to stretch. example is here http://outro.webs.com/
this works 100% in strict xhtml and in any browser lol!
This is really wonderful. Kudos. I guess you must have spent a lot of time figuring this out. I had similar issue, but I took to javascript!
Yep, javascript does the trick too. However, I usually go for the “if the user doesn’t have javascript, it should still work” approach. Though almost everyone on the web these days does enable javascript, there are always those few who don’t, and I don’t want to exclude them from having an awesome experience as well.
Excellent break down! Took me for ever to figure that one out.
The style in your blog post uses min-height, but http://img.tutwow.com/VerticalStretch/Example2/style.css is actually using height.
If people are still having problems getting this to work remember you need to have your doc type set to XHTML 1.0 Strict, it doesn’t appear to work in XHTML 1.0 Transitional.
Thanks for pointing this out, Burgess! I hadn’t experimented around with different doctypes, though I realize now how important they are.
I noticed on this page you mention “min-height” but In your example CSS file at: http://img.tutwow.com/VerticalStretch/Example2/style.css it is “height”
Example on the page is not the same as example in the file (last one):
Body style is “min-height: 100%;” on the page and “height: 100%;” in css.
But anyway, I tried the following:
html {
min-height: 100%;
}
body {
height: 100%;
}
Which works perfectly for large content (tested on IE7, Chrome, FF).