Computer and Business Skills training

Sign Up
Home / HTML / Full-Height Layout

Creating a Full-Height Web Page Layout

A common issue that comes up when creating web pages is what happens when the content on a page is smaller than the screen height. For example, take a look at the following simple layout:

See the Pen HTML Example 1 by Jeff Wiatt (@jwiatt) on CodePen.

See the Pen HTML Example 1 by Jeff Wiatt (@jwiatt) on CodePen.

We can see that the content is not large enough to fill the screen, leaving a gap below the footer. While this is functional, it's not particularly attractive. Ideally, even when there's very little content on a page, it would be nice if the page were at least as large as the viewport, so the footer would be at the bottom of the screen. Fortunately, this is relatively simple to accomplish.

Setting the Layout Height

Although it's common to use the width, min-width, and max-width properties in layouts, it's less common to use the height, min-height, and max-height properties. It makes sense, since most of the time in our layouts, we want to have the height property left at its default auto value so the element is sized to whatever content it contains. Sometimes, though, that can backfire on us, leaving us with some unsightly gaps such as the one in the example above.

The first step is to add the min-height property to the wrapper container:

See the Pen HTML Example 2 by Jeff Wiatt (@jwiatt) on CodePen.

The min-height property prevents the element from becoming any shorter than the property’s value. Because we can't know the viewport's exact size, we use "100%" as the size.

Also note that the box-sizing property has been set to "border-box" instead of the default "content-box" setting. This is a good idea if you decide to do things such as add padding and/or border (but not margin) to the wrapper later, as "border-box" sets the height value to equal the content box height plus padding and border. It just makes things easier, especially with full-height layouts.

Anyway, we're not done yet. Just setting the wrapper's min-height to "100%" won’t do anything. Any time you use the percent unit in layout properties, you're setting that element's property relative to the element's parent property. In this case, we're setting the wrapper to the full height of the BODY element.

The problem is that the BODY's height property is also set to "auto", and thus can't contribute a value for wrapper's min-height to work. We have to give all of the wrapper element's ancestors a height value:

See the Pen HTML Example 3 by Jeff Wiatt (@jwiatt) on CodePen.

Why do we need to do this? Because we're still using the percent unit for the height value. So the wrapper is at least the full height of the BODY element, which is the full height of the HTML element, which gets its height from the viewport. Once we do that, we begin seeing the fruits of our labor:

See the Pen HTML Example 4 by Jeff Wiatt (@jwiatt) on CodePen.

It's not quite perfect, but we're making progress!

Now all we have to do is get the footer to the bottom of the page.

Repositioning the Footer

We can get the footer where we want it with a bit of positioning CSS:

See the Pen HTML Example 5 by Jeff Wiatt (@jwiatt) on CodePen.

First we set the wrapper to be position: relative. This is because when we set an element's positioning to "absolute", its positioning will be anchored to either its closest relatively-positioned ancestor or to the document if no such ancestor is found. So the "pagefooter" element will be anchored to the wrapper element position instead of the document. Otherwise setting the pagefooter's width to 100% would be 100% of the overall document width (the viewport width in this case) instead of 100% of the wrapper's width.

If you would like to learn more about using CSS Styles and HTML, Training Connection offers a three-day HTML/CSS class in our Chicago and Los Angeles locations.

The result is seen here:

See the Pen HTML Example 6 by Jeff Wiatt (@jwiatt) on CodePen.

Looking much better! One final thing to do is to add padding (not margin) to the bottom of the wrapper equal to the height of the footer so that any content longer than the viewport height doesn't get obscured by the absolutely-positioned footer:

See the Pen HTML Example 7 by Jeff Wiatt (@jwiatt) on CodePen.

After adding additional paragraphs to the page we see that everything looks ok:

See the Pen HTML Example 8 by Jeff Wiatt (@jwiatt) on CodePen.

There we have it – a full-height layout that works across all modern (IE 8+) browsers.

Related HTML/CSS Lessons

Using HTML Reset Stylesheets

Onsite HTML/CSS training

Through our network of local trainers we deliver onsite HTML/CSS classes right across the country. Obtain a quote for an onsite HTML/CSS class.

To view a sample of our past students testimonials, please click on the following link: HTML/CSS reviews.

Why train with us?Need Group training quotation?Fresh Starbucks brewed every dayFREE Class Repeat with each course