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. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum.<br /><br /> Etiam scelerisque, nunc ac egestas consequat, odio nibh euismod nulla, eget auctor orci nibh vel nisi. Aliquam erat volutpat. Mauris vel neque sit amet nunc gravida congue sed sit amet purus. Quisque lacus quam, egestas ac tincidunt a, lacinia vel velit. Aenean facilisis nulla vitae urna tincidunt congue sed ut dui. Morbi malesuada nulla nec purus convallis consequat. Vivamus id mollis quam. Morbi ac commodo nulla. In condimentum orci id nisl volutpat bibendum. Quisque commodo hendrerit lorem quis egestas. Maecenas quis tortor arcu. Vivamus rutrum nunc non neque consectetur quis placerat neque lobortis. Nam vestibulum, arcu sodales feugiat consectetur, nisl orci bibendum elit, eu euismod magna sapien ut nibh. Donec semper quam scelerisque tortor dictum gravida. In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien.<br /><br /> Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget, scelerisque a libero. Morbi eu porttitor ipsum. Nullam lorem nisi, posuere quis volutpat eget, luctus nec massa. Quisque eget odio ac lectus vestibulum faucibus eget in metus. In pellentesque faucibus vestibulum. Nulla at nulla justo, eget luctus tortor. Nulla facilisi. Duis aliquet egestas purus in blandit. Curabitur vulputate, ligula lacinia scelerisque tempor, lacus lacus ornare ante, ac egestas est urna sit amet arcu. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed molestie augue sit amet leo consequat posuere. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Proin vel ante a orci tempus eleifend ut et magna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus luctus urna sed urna ultricies ac tempor dui sagittis. In condimentum facilisis porta. Sed nec diam eu diam mattis viverra. Nulla fringilla, orci ac euismod semper, magna diam porttitor mauris, quis sollicitudin sapien justo in libero. Vestibulum mollis mauris enim. Morbi euismod magna ac lorem rutrum elementum. Donec viverra auctor lobortis. Pellentesque eu est a nulla placerat dignissim. Morbi a enim in magna semper bibendum. Etiam scelerisque, nunc ac egestas consequat, odio nibh euismod nulla, eget auctor orci nibh vel nisi. Aliquam erat volutpat. Mauris vel neque sit amet nunc gravida congue sed sit amet purus.<br /><br /> Quisque lacus quam, egestas ac tincidunt a, lacinia vel velit. Aenean facilisis nulla vitae urna tincidunt congue sed ut dui. Morbi malesuada nulla nec purus convallis consequat. Vivamus id mollis quam. Morbi ac commodo nulla. In condimentum orci id nisl volutpat bibendum. Quisque commodo hendrerit lorem quis egestas. Maecenas quis tortor arcu. Vivamus rutrum nunc non neque consectetur quis placerat neque lobortis. Nam vestibulum, arcu sodales feugiat consectetur, nisl orci bibendum elit, eu euismod magna sapien ut nibh. Donec semper quam scelerisque tortor dictum gravida. In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien. Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget, scelerisque a libero. Morbi eu porttitor ipsum. Nullam lorem nisi, posuere quis volutpat eget, luctus nec massa. Pellentesque aliquam lacinia tellus sit amet bibendum. Ut posuere justo in enim pretium scelerisque. Etiam ornare vehicula euismod. Vestibulum at risus augue. Sed non semper dolor. Sed fringilla consequat velit a porta. Pellentesque sed lectus pharetra ipsum ultricies commodo non sit amet velit. Suspendisse volutpat lobortis ipsum, in scelerisque nisi iaculis a. Duis pulvinar lacinia commodo. Integer in lorem id nibh luctus aliquam. Sed elementum, est ac sagittis porttitor, neque metus ultricies ante, in accumsan massa nisl non. </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 {
height: 100%;
}
body {
margin: 0;
padding: 0;
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! Also, here’s an example of what the page would look like if the content is too long for the page. Notice that the div doesn’t end at the bottom of the viewport, but stretches beyond the page to accommodate for the longer content.
Update: Thanks to some very helpful comments, I have updated the final CSS so that the #content div now has “min-height: 100%” instead of just “height: 100%”. This is so that if the content of the #content div extends beyond the height of the window, the background will expand as well.




September 11, 2008 in 









thanks, that was really very useful.
This helped much, especially the min-height thing.
I spent hours trying to figure how to fix the issue before I found this article, thanks!
salut
You serious bro? This technique is the most commonly given solution all over the internet. Apart from that – IT’S NOT WHAT DEVELOPERS NEED! The real problem lies when you want to stretch a child DIV inside the main parent (like your “#content”) to fit the rest of the screen. And this can be done only by javascript.
So @%$# off.
Hey, Jonur,
Why so hostile in a place where we’ve just been trying to help one another?
And, even though your last line makes me not want to, I’m going to tell you how to do what you mentioned because it may help someone else.
In the parent element, specify the height and the child will conform like you want it, min-height is not enough.
In the example given in this article, add height: 100%; to the #content style.
Example at: http://www.lorcguild.com/height_test.html
(And I included the CSS in the instead of an external style sheet, so just view the source).
Oh, and that last line of your comment…..
Right back at ya.
Hey Jonur, People like you are real turds. Go jump off a cliff. The author of this article showed his class by even helping an Ahole such as yourself… learn from it!
You don’t need JavaScript. Usually I want something like:
Header
Content
Footer
And I want the footer to stick to the bottom of the screen with content expanding to fill the screen while still allowing the content to expand beyond the height of the screen and pushing the footer along with it.
Pretty much I achieve this by creating a wrapper block that the header and content will go in with the footer block a sibling to this wrapper. Then the wrapper I give a min-height: 100% and overflow-y: visible, I pad the bottom of the content block to match the height of the footer, and I give a negative top margin to the footer that matches the bottom padding of the content block.
The good news.. CSS3 makes this a lot cleaner. The bad news.. it’ll be ten years before IE supports it and the majority of IE users run a supporting version.
Hi,
Could you please clarify this:
I pad the bottom of the content block to match the height of the footer, and I give a negative top margin to the footer that matches the bottom padding of the content block.
Thanks
I really appreciate you brother. It is an awesome tut and the briefing is very well too.
May be for professionals it may sound a bit low but for novice and intermediate web developers, this is a very well drafted technical explanation. I sincerely appreciate the tut and your way of analysis.
Last but not least i have a small doubt, how come you were able to get the div height 100% with out specifying height for body in this page, in your style or any where else.
How were you able to get the desired effect, with out implementing the style coordinates you have specified in the tutorial.
Can I have, bit more clear briefing to achieve this effect. I have tried various documentation declarations and charset for css, it is not working for me, but for you in this page, I find absolutely astonishing fact of div height implementation from your style sheet. You have not specified 100% height for body any where in the style sheet nor in the main page. but still you were able to meet the point. How?
All the help is appreciated and thanked.
Thank you very much! very good explanation
Since this article has really help me a lot, I want to share one tip for those using ASP.NET. Aside from adding the body and html height 100%, you also want to add that to the form tag. As explained, the 100% is relevant to the parent tag and since ASP.NET has body->form->your content heirarchy, your content won’t be able to inherit the height directly from the body tag.
form
{
height:100%;
}
Dude! This helped me a ton! Actually starting with the inappropriate comment up there a few posts back through boynevs post, I was able to use this tut by invoking the 100% on the tag as mentioned.
Thank you very much for the time to explain this, otherwise I woulda been using JS to do this.
keep it up!
Thanks (Darrell ?) for the effort to put this up.
This was a very simply explained tutorial, excellent when you need a clear answer quickly – just what developers need ! Well done.
@laserboy: No, I didn’t write the article. I found it like everybody else here, lol. Searching for answers one day a year or so ago. I just add my two cents every now and then.
(Just noticed at the bottom of the page in “About the Author” section. The author is Ben Lind. He actually wrote this back in 2008)
I also noticed that, in Firefox at least, if you have a position:absolute on the #content element, this will foil the 100% height/min-height settings. By removing that absolute positioning (I’m not sure why it was there in the first place) I was able to apply this fix.
Just thought I’d let you know if you wanted to experiment a bit with different permutations.
It worked!!!! thanks a lot!!!!!!
awesome !!!
May you live many long, happy, prosperous years for revealing this obviously well kept secret to me.
Thank you for this! Great tutorial for something i’ve stumbled upon without a good fix until now. Much appreciated.
This is not working with Mac / Google Chrome (18.0.1025.163)
Interesting. It works great for me on a Mac with Chrome version 18.0.1025.165. Can you link to a screenshot?
But still, I have a problem plzz see to it…
Here’s the html code:
(…)
Now my problem,
the division white(which is to be 100%) comes only till I dont scroll.
But when I scroll down, it has ended there itself.
Please see to it, I am in this problem for more than a month.
(…)
& here’s the css code:
(…)
Hey Deval,
Unfortunately I had to edit out your HTML code because this site’s comment form messed it all up. However, if you could give me a link to your problem, I will do my best to help you with it.
Thanks Ben for your support, but I have not published the page yet.I mail you the code…
but I have added the tag so that it does not mess up wen I comment…
Here it goes…
The HTML code is:
(…)
Unfortunately I had to edit out your code again because the comment form processor stripped all of the tags out. I will email you so that you can send me the files.
It did nt work….
Can I mail you?
You are a genius. So thank you.
Thank you so-much. Very helpful tip.
I have failed all my life on this. Thought this is not possible.
Thank you.
I used this to stretch a sidebar and it worked like a charm! Thank you for saving me hours of frustration.
Hi,
Awesome tip, I never though about setting height of the html box, always stopped with body one… And tried some boring tricks with javaScript…
Thanks a lot !
Man! You are the guy! Thx a lot!!! I really did a lot of table-, frames- and css-layouts already all over my years, mostly trying to achieve simple cross-browsing/cross-device/cross-platform websites without tons of hacks and bug-workarounds with conditions and so on. I understand CSS quite well, I’m not a specialist, but I did a lot already. And we all know Cross-Compatible-CSS can be a real pain.
There are tons of articles explaining the box-models, we have good CCS-Websites with perfect CSS references all over the net, but there are only few who explain the positon flow and nature of CSS and its dependencies in such an easy, understandable and natural way like you did.
THANKS!!!!
Stuff that goes after:
“Let me give you a different way of thinking about HTML.”
… is really proper way to explain things.
No “tecchie” gibberish, plain, to the point … simply spot-on.
Thanks.
[...] Quick Tip: CSS 100% Height | TutWow [...]