Making a DIV fill the viewport vertically with CSS

How to get a DIV to stretch to fill the window using CSS?

This is a question that gets pondered by every CSS coder at some point. There are plenty of lengthly and convoluted threads on this issue, and the amount of misinformation far outweighs the few straightforward (correct) answers, to the problem. So, in the spirit of spreading the word, here it is, question and answer...


How do I make a div stretch all the way down the page, regardless of content? Or, how do I make a div fill the viewport vertically?

You may be after a means to create equal height columns. If this is the case, I'd like to point you here - be sure to read the comments on this post, as they contain fixes and tweaks for the method. If, however, your after a single column, centered, that stretches all the way down the page, then read on.

Easiest and best

The best way to overcome this problem is the simplest: fake it. create a slice of background, and repeat it vertically with css on the body. This can give the illusion of a centered column, complete with shadow if one wishes, that stretches neatly all the way to the bottom of the viewport. However, if this method doesn't suit (which sometimes it won't), there is another method

Tiresome and complicated

<div id="wrap">
<p>Content of any kind</p>
html, body {
height: 100%;

#wrap {
height: 100%

This technique has only limited use however: if the user loads the page within a small browser window, then resizes the window to greater height, the div will not stretch with it. So, adding to this, a little javascript is called for. Note that the code below utilizes the excellent jquery library; now a permanent fixture in all of my web projects.

stop = false;
function tidy() {
if($(document).height() > $('#wrap').height() && stop == false) {
$('#wrap').height( $(document).height() );
stop = true;
setTimeout('stop = false', 100);
$(window).resize( tidy );

So, we define a function that asks if the document height is great than #wrap (our div). If it is, we resize #wrap to the height of the document. We do this onload, plus every time the window is resized. Also, we've got a setTimeout in there so the function can't be called too many times too quickly (breaks IE7). All this might seem a lot bother to you, and I wholeheartedly agree. Plus things may look messy if javascript is turned off. However, every project has to be judged objectively; I've just completed something where this was the right solution.

Written by Tom Bates