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!
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 









truly worth while
[...] 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. [...]
Although you make a great point, this is the same thing i have been using and it only works if the browser is NOT resized. Try resizing your “example file” so that the browser needs to scroll. Notice that it does not stretch 100% of the height anymore.
This is exactly the problem I have, and I’ve yet to find a solution for it.
This “tutorial” is only half of the solution.
I cant seem to find a solution to the scrolling thing.
One way is to use jquery or similar to get the height of the longest content container and apply that height onto the element that you want 100%
i don’t understand, you said use
“min-height: 100%;”
but your example here
http://img.tutwow.com/VerticalStretch/Example2/VerticalStretch.html
“it’s use only height:100%;”
i test with min-height it didn’t work.
with height only it will be work.
for the other readers,
please refer to example when to try this.
thanks.
Thanks for pointing that out! I have updated the article to mirror the example page.
I’m sure there are reasons in which this wouldn’t be ideal, but what about
position:absolute;
height:100%;
?
Or, what if you made margin & padding zero on the above elements?
Anyway, good tutorial. I ran across this a few weeks ago, and I forget how I solved it, but it worked fine in the end.
[...] CSS 100% Height 设计师中一个普遍的问题即如何让一个 DIV 100%拉伸为窗口的高度。有许多不同的方案,这就是其中之一。 [...]
Thanks for this technique.
Thanks so much for your tutorial. This really helped me out. Much appreciated.
Thanks Mate, you saved my life
[...] CSS 100% Height A simple technique for creating a div table that stretches the full height of a browser. [...]
I don’t think this fully works with floated elements inside the #content, though.
The solution has problem when the page gets scroll.
I do not know if this solves your problem, but if you need the 100% height background you can do so:
html, body – height: 100%;
put all the content in a block element such as div, do not set any height properties for this div.
Set the same background (duplicate) for the body and div.
It works fine with vertical border too, but it don’t solve horisontal borders problem.
I was beeing browsing the Yahoo for this info and just wanted to thank you for the post. Can you tell, just off topic, how can i find a copy of this theme? – Thanks in advance
its really helpful for me . i supper long time with this problem. thanks for hack.
Very helpful, thanks!
just Great..!! Thanks.!
This is great for sizing an element to fit a browser window, but what about sizing it to *the whole page*?
I’d like to make a column off to the side and have it continue the whole way down to the bottom of the page. I’ve been trying to get % lengths, but that seems to give me lengths relative to the “containing block” which is the size of the view port. I think I’m looking for a length equal to the size of , but don’t seem to have any way to access it…
That last line should read , but it disappeared. I’d like the length of the column to be equal to the length of the whole body of the document.
Terrible! BODY. I was trying to type the BODY tag!
I’m having the same issue.
I wanted to create a modal “popup” that floats over the rest of the page. I created a div that was absolutely positioned with 100% width and 100% height and a semi-transparent silver backround (to make the rest of the site appear disabled” and then put my popup div on top of that. Unfortunately, the 100% height of the background div only goes as far as the window – not the entire html body. So if the user scrolls down, they get to see the rest of page without the semi-transparent background
How would you get an absolutely positioned div to take 100% of the body height?
Have you tried FIXED instead of ABSOLUTE?? (Same with the modal window if you don’t want it to scroll with the page).
Thanks Darrell
“Fixed” was exactly what I needed
How could I have been using css for 2 years and not discovered that little gem?!
Thanks again
Thank-you thank-you thank-you Darrel, I’ve been trying to find the answer to Vassago’s same problem for hours now!
thanks a lot for this tutorial
this is exactly what i need
hey! thanks a lot
What a wanderful blogpost this was. Good share. But I am having trouble with your rss. I fail to subscribe. Is there anybody else experiencing similar issue with your RSS?
ahh omg this is the simplest thing ever and this problem has been driving me NUTS!!!! a million thanks!
Does anyone know how the browser scroll problem can be solved, or is this just not possible, and 100% div heights that work in any instance is just a dream to be realized in the far future?
Nice tutorial, will give a try !
Thanks mate This works great
I love you man…
one line of css…
display:block;
added to above snippet provided by author saves the day.
[...] CSS 100% Height11 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 am having a problem with something similar…. I want the content of the page to shrink or stretch depending on the how much content there is.
I have my pages stretching, but how do I get them to shrink when the content is not as long as another page?
my pages are on http://waterfrontgraph.com/mrsparksllc/
I have tried and tried to figure this out, but the samples I am finding are not working for me. I’m sure it’s a simple thing I’m not doing or doing wrong, I just don’t see it!
Any help anyone could give me would be greatly appreciated.
The solution to get your layout to not only stretch to 100% of the window, but carry on even if your page is long and you have to scroll is this:
Set the height attribute of your container to auto:
height: auto;
At least this solved it for me.
Cheers!
Thanks for the clear explanation. It worked!
Here is a solution for those who want the div container to stretch with the amount of content and also stay at 100% stretch even when you scroll the browser, following this example, you would want to put “auto” for the height property, rather than 100%. You still want to have 100% for the height property of both the body and html elements for your page.
If you would want to do a header/body/footer kind of design template (3 containers nested inside the body element) you could do what one would do when using frames, just use percentages for the header and footer div’s and then put the main body content to “auto”.
So you could do:
html {height:100%}
body{height:100%}
#header{height:20%}
#body{height:auto}
#footer{height:5%}
You can obiously fill in the rest (margins, border, colors, etc).
Hope this helps. If you are having issues, make sure you are using xml, xhtml and DTD tags.
Hi,
Thanks for this useful post. But if I have more than one container (specially when dealing with rounded corners with the old fashioned three images way) will this still work? I have tried with setting the html height to 100%, but it just messes up the whole layout. So far, only declaring the height of the container seems to solve the problem. But I really don’t know how long my pages are going to be, some are going to be really short too. So, can you please help me with that? Thanks,again.
Forgot to mention it, height auto works fine if the page has a good amount of content, bit if the page is very low on content, then, it doesn’t strech the container set to height auto. Thanks.
It’s hard to say, based on what information you gave. Do you have a link to where I could see an example of your question?
Hi,
thanks. here is the link : http://stlouisbluesboutique.com/ . The container “main_content ” now has a height of 115em. But I really need it to be liquid. I hope you’ll be able to tell me what I’m doing wrong.
P.S : it’s a wordpress site based on the thesis theme. Thanks a lot again.
Hmm… It’s hard to tell exactly what you want from the example. Are you trying to make it so that when you have little to no content in the main_content div, the whole design stays at 100% page height instead of shrinking to fit the content? If so, the technique of this article unfortunately can’t help you with that. Sorry!
Thanks for your help… I really appreciate it!
Kim.
i love you.
perfect solution to my problem..thanks alot
Richa likes this
thanks
I do not have the permission to effect the body tag information, i am only allowed to effect the partial i am working on, any solutions for that?
Also, I have not had luck with your solutions when I can effect the body, its not a perfect science, specially when working with cross browsers.
hight
Thanks for this! Kudos++
I have been checking out many of your stories and i can state pretty clever stuff. I will make sure to bookmark your website.
Hi this is Siva reddy Thanks for Quick Tip: CSS 100% Height Example.
so many time i face problem this issue
anyway thank you
I want 100% height on the content div, BUT I want margins/padding. Every “CSS 100% height” result on Google always starts with…
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
}
</style>
…when you take those examples & add margin/padding, they FAIL (cuz they cause unnecessary scrollbars).
What I want is REALLY simple (& should be the default): I want the page to stretch to the size of the browser…or the size of the context, whichever is LONGER. If I have a page with 1 line on it, stretch the bottom border of the content/body to the innerHeight of the window (accounting for any desired margin/padding). Simple. If I have a page with 1,000,000 lines on it: do nothing, it’s long enough already. So, basically: if there is no scrollbar, stretch to fit the browser’s innerHeight, if there is a scrollbar: do nothing (don’t “shrink the content to fit the innerHeight”). On resize: recalculate based on the new size.
Actually, I think the behavior I describe should be the default, without any CSS (or JS {or other “tricks”}) to “make it work”. Simply saying margin: 5px (on the body {or html?} tag) should be enough to make the browser do just that: have a 5px margin around the entire window, no more margin (not enough content to “make it stretch”), no less (“too much content, now the 100% height are gets left at the top of the page”). If you have no CSS or specify no margin for the bottom of the body tag, the bottom margin would default, like the other margins do & the content would still auto-stretch to the bottom, to make that default margin “true”.
Just to be clear: What I’m talking about is NOT “getting columns on a page to line up”, that request comes up alot in Google Searches on this Subject. What I want is a short page to look just as good as a long page. Aligning columns is another story, that I haven’t had to deal with yet.
I have never commented after reading a tutorial. I’m a lurker. I’m so busy during the day and it’s so hard for me to stay focused, I usually just pick out what I need from any given tutorial, use it, then move on to the next thing.
But I had to say something about this!
You did a beautiful job explaining and executing this information. There were so many other pieces about this on the web, with other guys acting as if they knew how, or just being so “technical” that they couldn’t simply explain it. But this here is what it should be. Simple and easy. When a person REALLY knows something there’s no need to try to SOUND smart.
And you did it with simple CSS and no hacks or javascript
Thank you.
thank you
very usefull
[...] CSS 100% Height by TutWow 1. When we put the “height: 100%;” style onto the <div id=”content”> box, [...]
Sir. you are a genius, thank you for this tut!
Ben:
Many thanks for posting this info!
Sadly, it is not working for me for some reason.
I have this in my CSS:
html{height:100%;}
body{height:100%;}
#header{width:1024px; height:150px;}
#navpanel{width:1024px; height:30px;}
#content_body{width:1024px; height:auto;}
#content_side{width:200px; height:auto;}
#footer{width:1024px; height:34px;}
I tried using 100% for both body (the parent div) and side (the nested div), and that is a no-go as well. [img]http://reaganator.com/comments/frown.gif[/img]
Any suggestions?
Okay, I figured out what is causing the problem; I have a center tag before the content_body div. So, how am I supposed to center this?
If you’re talking about centering text, just put an “align: center” in the body CSS. If you’re talking about centering the whole layout, put “margin: auto” on the main div. Let me know if this works!
Ben:
Ah, through trial and error, I figured that out! Thanks for the confirmation though.
However… one more issue. I have a nested div that I want to match the height of the parent div, and I thought I had it earlier… but now it is not working. In the aforementioned CSS, it is content_side.
To see the madness in action: http://mybrowndds.com/dev/staff/blank.php
Ah, the age old question.
Just do a Google search for “css equal height columns” and you’ll get tons of relevant results for your question. There isn’t really one easy answer, though.
Excelent… works great… thx a lot