Quick Tip: CSS 100% Height
Tutorials September 11th, 2008I 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 11th, 2008 at 1:24 pm
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!
September 21st, 2008 at 7:25 am
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.
September 21st, 2008 at 7:30 am
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
September 21st, 2008 at 10:03 am
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 !!
September 21st, 2008 at 12:47 pm
Very nice! A good example of thinking outside the box (or container for that matter)!
Thanks!!!
September 22nd, 2008 at 7:14 am
Nice perception, usefull, really …
September 26th, 2008 at 4:00 pm
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.
September 28th, 2008 at 2:26 pm
oo! that is perfect! Thanks You!
September 28th, 2008 at 4:30 pm
beautiful article .
thank you
September 28th, 2008 at 5:26 pm
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!
September 30th, 2008 at 11:07 am
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%’.
October 1st, 2008 at 3:23 am
Wow, so simple, yet elegant. I’ve been fighting with this problem for years. Thanks for the information.
October 1st, 2008 at 10:49 am
@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.
October 2nd, 2008 at 1:53 pm
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.
October 2nd, 2008 at 2:57 pm
@Monte: I have seen that before - thanks for sharing!
October 3rd, 2008 at 9:58 am
Still, it doesn’t work for me. Firefox doesn’t show the page at 100% height. Neighter does Internet Explorer!
October 3rd, 2008 at 1:40 pm
@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?
October 10th, 2008 at 12:57 pm
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.
October 10th, 2008 at 1:16 pm
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!)
October 15th, 2008 at 6:14 pm
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.
October 21st, 2008 at 3:07 pm
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!
October 23rd, 2008 at 8:49 am
Well, that is solution for firefox, what to do while using internet explorer?Maybe use the if conditional statement.
October 30th, 2008 at 2:40 pm
Hmm, does this still work if the divs are floating? Does not seem to on my Web site…
November 11th, 2008 at 9:13 am
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
November 11th, 2008 at 11:43 am
@Greg: Thanks for pointing that out! I’m sure that this will come in use for many of the people reading the article.
November 21st, 2008 at 3:01 pm
Doesnt work. Not a good tutorial!
November 21st, 2008 at 4:53 pm
@Rob: What problem do you seem to be having?
November 22nd, 2008 at 9:03 pm
@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?
November 22nd, 2008 at 9:57 pm
I find that it doesnt stretch the container if you use floats on the other divs that are wrapped inside the container
November 22nd, 2008 at 10:20 pm
Hey Josh I found the solution to my problem
http://www.quirksmode.org/css/clearing.html
November 23rd, 2008 at 10:48 pm
@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.
November 28th, 2008 at 2:05 am
Agreed! good luck with you Josh and keep up the good work with your website!
*handshake*
November 29th, 2008 at 9:21 pm
Thanks for this tip, I’ve been trying to work this out for ages!
December 2nd, 2008 at 6:17 am
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…
December 6th, 2008 at 10:30 am
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.
December 20th, 2008 at 7:29 pm
Thanks a lot, now getting the hang of css, this was very helpful.