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 a lot… Exactly what i need
U ROCKS…
thanks so much, this problem was driving me insane for an hour. works nice!
Doesn’t seem to work in Chrome. Works fine in IE and FF.
Thx for the tip, May the Omnissiah be with you always!
Please help:
i have parent div – without height – this div have inside div with clear:both;
and i have child div without content (for design only)
And this div does not inherit the computed height of the parent element.
tell me what can I do?
Thanks!
Sorry about my english))
thx a lot !!!!! Now I understand a little more about all this CSS Magic. U R AWESOME
That`s very cool . Thanks for sharing .
Thank you. I have been looking for this for a while now. Im currently working on a responsive wordpress theme where your 100% height solution this will be key.
Thanks alot!!
a million thanks!!!
You, my friend have made all my frustrations go away… i was at it the whole evening and this is exactly what i need and it worked! I was half way of pulling my hair out…. Thank you!
Cheers!
Thanks for the great tip, it have been really frustrating for me so far!
Thanks a bunch, I had been using javascript to set the height, this is so much easier.
Thank a lot!
Your tutorials is always SOOOO usefull! Thank you for helping me out
a wise .css-only solution. Thumb up!
div { position:absolute; height:100%; }
GENIUS!!!!!! LIFE SAVER!!!!!! BRILLIANT!!!!!!!! THANK YOU!!!!!!!!
[...] CSS 100% Height 设计师中一个普遍的问题即如何让一个 DIV 100%拉伸为窗口的高度。有许多不同的方案,这就是其中之一。 [...]
You are a genius! Thanks you sooo much, it was driving me crazy.
thanks very much
This isn’t the first time I’ve need confirmation of this. Thank you, Kyle
wow. solved my problem. ty
[...] CSS 100% Height A common problem among designers is how to get a div to stretch 100% of the window’s height. There are a few different techniques out there, and this tutorial shows one of them. [...]
I have a problem with this.
It only works if the div is 960px wide..
The one I have is 1230px wide though and I end up having no effect at all due to that.
Help?
Nevermind.. turns out it isn’t working at all : /
Doesn’t work at all. Rubbish
Doesn’t work for me too :\
Great THX!
solved my problem, Thanks!
Thank you. Big help!
Very useful man thanx…
Thank you very much. I finally understand why things need to be that way and why it would have never work otherwise… Thanks for explaining the behavior of those elements.
Awesome. Add to this the fact that you one absolutely position in a relatively positioned div, and it’s the ultimate method! Cheers!
Any ideas on how to make this work?
==========================
/* HTML */
html {
height: 100%;
width: 100%;
}
/* Body */
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
}
/* TOP ROW LAYOUT */
/* TOP WRAPPER */
#topwrapper {
float: left;
min-width: 100%;
height: 200px;
}
/* CONTENT TOP LEFT */
#topleft {
background: #ccc;
border-bottom: 1px solid #000;
border-left: 1px solid #000;
float: left;
width: 900px;
height: 200px;
}
/* EMPTY SPACE TOP RIGHT */
#topright {
background: #fff;
border-bottom: 1px solid #000;
float: left;
min-width: 100%;
height: 200px;
}
/* MIDDLE ROW LAYOUT */
/* MIDDLE WRAPPER */
#middlewrapper {
float: left;
min-width: 100%;
min-height: 100%;
}
/* MENU MIDDLE LEFT */
#middleleft {
background: #ccc;
border-left: 1px solid #000;
border-right: 1px solid #000;
float: left;
width: 200px;
min-height: 500px;
}
/* CONTENT MIDDLE CENTER */
#middlecenter {
background: #eee;
float: left;
width: 700px;
min-height: 100%;
}
/* EMPTY SPACE MIDDLE RIGHT */
#middleright {
background: #fff;
float: left;
min-width: 100%;
min-height: 100%;
}
/* BOTTOM ROW LAYOUT */
/* BOTTOM WRAPPER */
#bottomwrapper {
float: left;
min-width: 100%;
height: 100px;
}
/* CONTENT BOTTOM LEFT */
#bottomleft {
background: #eee;
border-left: 1px solid #000;
border-top: 1px solid #000;
float: left;
width: 900px;
height: 100px;
}
/* EMPTY SPACE BOTTOM RIGHT */
#bottomright {
background: #ccc;
border-top: 1px solid #000;
float: left;
min-width: 100%;
height: 100px;
}
Thanks in advance for your help!
THANK YOU!
[...] Quick Tip: CSS 100% Height [...]