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 









[...] the screen and not just stop. This is what I can't seem to figure out how to do. I even found this CSS 100% Height | TutWow and tried doing it like it says (setting html & body to min-height: 100%) but that didn't work [...]
So after not getting this to work properly and comparing your example css (above) to the actual css attached to your example pages, “min-height:100%” doesn’t work, just use “height:100%”
Can someone help me it is not working, below is the html and css code.
Untitled 1
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%;
}
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
asdfdsfjdklsfjkdslf kldsfjklsdjfklsdjf sdkfjdkslfjsdklfj klsdjfsdklfjkldsf ksdfjsdkfjkdlsfj klsdfjkdslfj
Hi, thought I’d drop this tidbit in:
Using the following used to work fine until the latest release of Firefox (3.5.2):
html {
font-size: 100%;
height: 100%;
margin-bottom: 1px;
}
But since the release of this new FF, I’m having to change height: 100%; to height: 101%; (min-height: 100%; – or – min-height: 101%; did nothing).
(I ran across others that said 100.1% or 100.05%, but neither work in the latest Firefox.)
So what I ended up with is:
html {
font-size: 100%;
height: 101%;
margin-bottom: 1px;
}
Hope it helps someone….
-D-
I’m trying to use this to force a div w/ a background image to have 100% height. When I put the height into the body and html CSS sections, the page will no longer scroll when resized smaller than the content.
Nevermind i found the solution on another page…
bgcontainer {min-height: 100%;} /* for proper browsers */
* html bgcontainer {height: 100%;} /* for IE */
There’s still one little issue that I can’t seem to resolve.
This works great, until you vertically shrink your window to the point where there’s a scroll bar. Now, when you scroll down, you lose your background.
Any thoughts? I can’t seem to find the solution.
Thanks,
Kyle
for all browsers including internet exploder
min-height: 500px;
height:auto !important;
height: 500px;
Woo hoo! Big help, thanks for the great post. I’ve been pulling my hair out trying to figure out how to make those pesky divs stretch 100%. Nice blog BTW =)
I’ve found this solution all over the web, but it still doesn’t solve a nagging issue I’m having, which is to have the divs nested inside the 100% height “container” also fill vertically when they don’t have content.
As far as I can tell they only fill when you use height:100% on the container. As soon as you change it to min-height:100%, the children divs no longer calculate their height and end up as though you have no 100% height attribute on them. Sure the container still fills vertically, but how to get the children to follow suit?
I’ve got a student website where I’ve tried to do this layout and outlined the problem here: http://edison.seattlecentral.edu/~jmohr001/
Is it possible? I’ve been working on every solution I’ve come across on the internet, and there doesn’t appear to be a way without using javascript or something. I’d like to find a pure HTML/CSS solution to this.
BTW, I found the “faux columns” method after finding this post. I found it very helpful. You can read about it here (not my blog at all, not a promotional thing, just a helpful explanation:)
http://www.positioniseverything.net/articles/onetruelayout/equalheight
Hope it helps someone.
I have been trawling the web trying to find an answer to my css nightmare question.
The main problem seems to be getting a min-height:580px and a height:100%
The thing I need to achieve is:
1. Site to have a min height of 580px including footer so total height before scroll bars appear is 580px.
2. Also, a min width is needed of 930px including right and left margin of 15px each side.
3. left menu of 216px wide and 100% high minus the footer height of 30px.
4. Main part of the screen should fill all the space available. Unless the screen height is less than 580px or width less than 930px. In this case you get scroll bars.
5. Compatiblity ie6,7,8, Firefox and Safari.
Can it be done with no Javascript?
Really great entry to hang on.. I
Thanks for another informative entry. Where else would anyone get that kind of information in such a great way of writing.
Really interesting stuff. I loved it. Thanks for sharing these type of works with developers. Thanks once again.
This won’t work if your content is bigger than the viewport.
I have tried this but it’s not working…..!
[...] 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. [...]
But this isn’t what I typically want….. which is the div to just surround the content and apply the margins and padding I specify.
This is a long div that pays no attention to the content length. What am I missing?
[...] items to flip between sizes. You will see that an area’s scroll depends on the configuration.CSS 100% HeightA common problem among designers is how to get a div to stretch 100% of the window’s height. There [...]
[...] 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% HeightA 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% HeightA 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 know that was only a simple test case but for that example I would put the background on the html tag (which is 100% by default) rather than a container div. Save a bit of mucking around.
[...] This blog will be featuring tip and techniques that address annoyances like the CSS column height problem. Today, we’ll share a link to a tutorial at TutWow about this problem: CSS 100% Height [...]
Wow, thats kinda cool, but simple. Anyway, i think this article could be much more shorter, than it is now.
this is great, but when I add a header and a footer it forces the scroll bar to show up…how do you do that without have the scroll bar?
I can’t get this to work on either Firefox or Safari. All I did was cut and paste from the example.
If you pull the css right from the example html you will find that it is different from the posted css information on this page…
Not sure why that is
html {
min-height: 100%;
height:100%;
}
body {
margin: 0;
padding: 0;
min-height: 100%;
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%;
height:100%;
}
..this seems solve problems for all browser….standard or non-standard !!
this stretched to the bottom but not to the top
[...] 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. [...]
@Sefat: Sweet! That works great.
[...] 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, but I came up with one that is my personal favorite, which I will share with you today. (tags: css layout 2008) Scritto il 23/02/10 da giorgio_v. [...]
[...] CSS 100% Height [...]
Just wasted alot of time trying this out (with and without the replied ‘fix’-solutions people posted, but is simply wouldn’t do the trick! Tested in FF 3.5.8.
The example is wrong
its close but not quite there
Make sure that html is specified as height: 100%; as well
NOT min-height
The only div that should be specified as “min-height: 100%;” is the div you want to expand the full height of the browser window. If you only specify it as “height: 100%;” when your page scrolls down the bg colour or image will be cut off.
@charset “utf-8″;
/* CSS Document */
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%;
[...] http://www.tutwow.com/tips/quick-tip-css-100-height/ # [...]
This doesn’t work in Chrome. It’s fine if the content fills the screen, but falls short of 100% if it doesn’t.
html { height:100%; }
body { min-height:100%; }
#content { min-height:100%; }
That works, with the caveat that ‘html’ doesn’t go past 100%. Really, you shouldn’t even be using ‘html’ for backgrounds or borders, so it shouldn’t matter. Make ‘body’ the main background, and go for ‘#container’ elements if you need more.
No min-height on ‘html’, but there is a min-height on ‘body’ and the content area. This is because the height of the ‘html’ element is not the same as the (other kind of) height of the ‘html’ element. The border/background would only go to 100%, but the page itself will still grow (so elements inside will grow higher). A bit inconsistent, but I assume it’s part of the spec (as both Webkit and Opera do it).
Michael, that worked for a layout I was testing on. Very nice.
[...] items to flip between sizes. You will see that an area’s scroll depends on the configuration.CSS 100% HeightA common problem among designers is how to get a div to stretch 100% of the window’s height. There [...]
thank for the tip. I tried the code on several different browsers all with beautiful results.
[...] CSS 100% Height | TutWow (tags: css webdesign tutorial html tips) [...]
Man, my head was aching trying to solve this one. Thanks to you, problem solved.
Cheers
[...] CSS 100% Height [...]
[...] artikel ini dipetik dari http://www.tutwow.com/tips/quick-tip-css-100-height/ [...]
[...] 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. [...]
[...] 英文原文:Quick Tip:CSS 100% Height 转载请注明以上信息,以表示对作者和译者的尊重,多谢! [...]
what is the css version (2 or 3) ?
thanks for this tips.
Yeahh… nice move. Thanx.
Hello,
My name is Alex.
Just wanted to thank you so much!
you have just solved a problem i had since the first day i started programming!
Thank you alot!
[...] 看到一篇关于用css设置100%高度的文章:Quick Tip: CSS 100% Height(英文)于是照着练习了一下。 [...]
Yes, there are many other solutions out there. However, this is the cleanest one, hands down.
The only change I would make to the original post would be to do what Sefat says above, adding both min-height AND height, thereby taking care of standards-compliant browsers, and also IE.
Otherwise, this is a great example of clear thinking and an elegant solution in action.