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 









This doesn’t work if the content goes farther below the 100%. You should change your example to have the text go below the screen and scroll…..it doesn’t work.
How can you go further than 100%? 100% is 100%…if it’s going further then you’re righting your html incorrectly.
This works, its awesome.
I’m seeing that problem too. The 100% that’s being set in the css refers to the browser window height. What JamesD is saying is that when your content goes beyond the browser height creating vertical scrollbar, this doesn’t work.
instead of specifying the width property as 100%, use the min-width property. this should allow larger content to flow off the page and scrolling to happen
I wonder why I never had the Idea to give the html tag itself some css.
Thanks for that.
nice article i solved my problem with this
thanx
thanks man. Troubled me a lot! You really helped me out thanks for writing this article i really appreciate it!
Thank you very much, this is great & helped article
Thanks man. This really helped me a lot.
Hi
I have had this problem before and could not solve it. I am working on a new site and am going mad trying to get my left div to expand to full height. I have tried all the suggested solutions. i.e. setting all the parent containers to height:100%, height:auto, min-height:100%. None of it seems to work for me.
As I am having this problem repeatedly and as the problem occurs in FF, Chrome and IE8, I must be doing something wrong.
Some pages wil have minimal content and others a lot, so the solution will have to work with and without scrolling.
The site really is in the beginning stages so is rudimentary and not a lot of content at present.
The temp site is at: http://www.koffeeklatch.co.uk/wordpress/
and the css can be found here: http://www.koffeeklatch.co.uk/wordpress/wp-content/themes/koffeeklatch/style.css
Any help is appreciated. Thanks
Hey Ryan! In your case, I would suggest a slightly different tactic than what I proposed in the article. Instead of using heights and such, I would just use put a background image on the body tag and tile it vertically. So just make an image with the yellow color you want at the width you want, and put this line of code in your body’s css:
background: url(your-yellow-image.jpg) repeat-y;That should tile the image for the full width of the page, including scrolling. Then you would just make a div for your left navigation bar and set its background to none.
Let me know if this solution works for you.
Thanks. I’ll try that. What is the reason you suggest this different tactic ? I’d like to understand why I’m having different issues.
Thanks
Well I’m sure there are different ways to get the div to stretch the full height of the page (including the technique outlined in this article), but in your case you would have to change around some of the HTML structuring of the page to make them work. This solution will be the most unobtrusive for the future, in my opinion.
Stretch in a how to do that?
I like this tipp much thank; i think it will help me one time; but not now, because i have included in of a table and i like them to have the same dimensions as the other on the same row but when having different text length the divs on the same row are not equal
I used to be suggested this website by my cousin. I am not positive whether this post is written via him as nobody else recognize such precise approximately my difficulty. You are amazing! Thank you!
This is still not working for me, and tiling a bg image is not an option. I would call this a browser bug. This is not acceptable from IE and FF. Thanks for your efforts.
I have tried all of the above solutions and either I am doing something incorrect or it does not work for my site.
I have a wrapper, height=auto
within that wrapper in order is
header = height 125px;
navbar = height:45px;
mainbody, this container holds a content section (left) and what will be a links section(right)
I have currently had to specify the height as when I try to use auto or 100% the page is scattered all over the place.
As obviously content will change and that also means the amount of content as well I want the content pain to expand and contract accordingly.
Could anyone advise as to the best way of rectifying this problem?
Thanks in advance.
Thanks a lot, man! This was helpful
Thank you for this tutorial. I am wondering if it is possible now to put a 100% div with a background color (like gray), NESTED within the “content” div so that after the Latin text, the div will stretch to the bottom of the browser.
Thanks man, you maked my day better.
Thank you for this.
However, if #content contains content which is less than the height of the screen, #content does not stretch to 100% of the screen. If the contents of #content is greater than the screen height, it works great.
Any ideas?
Thank You
Rob
Hello there, just became aware of your blog through Google, and found that it’s really informative. I am gonna watch out for brussels. I will be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers!
This was just amazing.
you are really a good webmaster. The site loading speed is amazing. It seems that you’re doing any unique trick. Furthermore, The contents are masterpiece. you’ve done a wonderful job on this topic!
[...] if you perform it on a 100% height, 100% width element:http://www.tutwow.com/htmlcss/quick-tip-css-100-height/ [...]
i found this jquery solution of 100% height which calculates the document height and makes the div height accordingly at http://www.irtaza.com/scripting/div-100-height-scroll-issue-on-ie-fixed-with-jquery/
I figured out how to get the height to at least 100% and to allow it also to grow … its fairly easy …
Check out the solution at www.chimpwebdesign.com/blog.php
Awesome!
Thanks so much for the tip and the step-by-step explanation of a very common frustration for web designers!
-Ollie
Very good stuff
If the div is placed immediately after the body open tag, it works perfectly.
Thanks great solution
Awesome tip – most other web posting on this topic fall short, leaving out the html element CSS declaration.
Thanks a million!
thanks!
Thanks for this topic
I am looking for a solution to stretch a nested div to as height as its nested div on the left. I followed your instruction, but the right div doesn’t stretch to the height of left div, what am I missing, I tried any height, overflow…but didn’t work. Can you help please? Thanks a lot!
To make it more clear, here is the codes:
My container div was set to ‘height: 100%’ but never went all the way down the page in Chrome. I was struggling with this from tips on other sites stating to put the 100% body and html tag which I did – didn’t work. I changed the container div setting to ‘min-height: 100%’ instead of just ‘height: 100%’ and everything works as intended. Thanks!
This does not work if there is a form tag within the HTML … any suggestions?
The fix is to change the “content” css tag to whatever the name of the form tag is.
overflow: hidden; // if you don’t want the scrollbar
in main #div id = content will fix it.
Don’t you mean overflow-x:hidden?
Thanks Bhava
Very informative. Thanks.
Thanks for the tip! どうもうありがとうございます。
This is an article that makes you think “never tuoghht of that!”
A-M-A-Z-I-N-G!
[...] 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. [...]
[...] CSS 100% Height [...]
Great! this works.
Initally it didn’ work when I tried without setting margin and padding of body tag.
Once both margin and padding of body tag need to set to 0 px, it perfectly fits the screen.
Great! this works.
Initially it didn’ work when I tried without setting margin and padding of body tag.
Once both margin and padding of body tag need to set to 0 px, it perfectly fits the screen.
Thanks Much it worked for me after removing margin and padding of body!!
Heya just creating my new website and this helped a treat!!
Thanks HEAPS
Thanks yaar
Nice article. it works perfectly.