Zymic

Webmaster resources

Follow us on Twitter!

Hosting status updates - Click here

CSS & HTML

Free Tutorials » CSS & HTML » Effective Footers

Code a footer which sticks to the bottom of the page except when content pushes it down.

Step 1

Having the right footer is important in a website, it can be a large part of the look of the page. In this tutorial we will be covering how to make a fixed-height footer, which sticks to the bottom of the page except when content pushes it down using purely HTML and CSS. You can see an example of this here. This is not entirely trivial because position: absolute; or fixed; would not move down when the page was filled with content, and merely placing it at the bottom of the markup would not ensure it was at the bottom of the page when there was not much content. The solution to the problem is that we make everything but the footer altogether have a minimum height of 100%, then we use a negative top margin on the footer equal to its total height to bring it up, placing the bottom of the footer at the bottom of the page when the content doesn't have a height larger than 100%, perfect! The HTML required for this is fairly simple, something like this will suffice:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <title>Footer Example</title>
</head>
<body>
   <!-- Container Divs -->
   <div id="container"><div id="padding">
      <!-- Header -->
      <h1>Header</h1>
 
      <!-- Content -->
      <h2>Example Content</h2>
      <p>
         Some content...
      </p>
   </div></div>
 
   <!-- Footer -->
   <p id="footer">
      Page Footer
   </p>
</body>
</html>

You can view what this code (with some basic styling to make things clearer) look like here.

Step 2

Now we can start styling the document to get the footer how we want it. First of all, we need to make the container div have a min-height of 100%, this can be achieved in most browsers with the following CSS:

html, body
{
   height: 100%;
   margin: 0;
   padding: 0;
}
div#container
{
   min-height: 100%;
}

Both html and body need to be set to 100% height so that the container div can take up the space. This does the job in a lot of browsers, but unfortunately IE doesn't understand min-height, for some reason IE's height is equivalent to min-height and min-height does not exist, so to combat this we need to set height: 100%; in IE without affecting compliant browsers. We can achieve this using !important in the CSS like so:

div#container
{
   height: auto !important;
   height: 100%;
   min-height: 100%;
}

The !important makes compliant browsers use height: auto; instead of height: 100%; while IE uses height: 100%; instead, fixing it nicely. Our layout now looks like this, almost done!

Step 3

Now that our main content area is formatted correctly we can turn our attention to the footer. At the moment it is just below the main content, so we need to apply our negative top margin to pull it up into the correct position. To do this, first we need to calculate its total height, in this case we have made the footer 17px in height, with an additional 9px padding at the top, and another 9px at the bottom. Therefore, the total height of the footer is 17+9+9 = 35px. So we need to move the footer 35 pixels up, which is done with this line:

p#footer
{
   margin: -35px 0 0 0;
   /* ... other styles ... */
}

This makes the top margin negative 35 and sets left, right and bottom to 0. Now, this is now perfect when there is little content, but when more is introduced to the page we see a problem emerge, the content overflows onto the footer which is hardly something we want. We fix this using the "padding" div that was introduced in the HTML at the beginning, we can't use padding on the container div, because that would make the height of the container div 100%+padding, and would push the footer below the bottom of the page again, so instead we have to pad another container inside the first, in this case the "padding" div. The fix is very simple, and is simply this:

div#padding { padding: 0 0 45px 0; }

Once this is in place, content no longer overflows onto the footer when there's a lot of it, and when there is little the page is unaffected.

Step 4

That's pretty much that on making footers like this, it's a useful tool and widely compatible, this method has been tested on:

  • IE 5
  • IE 5.5
  • IE 6
  • IE 7
  • Firefox 2
  • Opera 9
  • Konqueror 3.5.7
  • Safari 2.0.4
NOTE: IE5 and 5.5 don't play well with the dynamic example at the top of the page, but are fine with static pages.

Tutorial comments

30.01.2016 -

master802 says …

Thanks for the sharing!

20.01.2016 -

hinnoamiure says …

On its per A week we have not stopped yet and like that said the most recent I'm still has a gathering more I am action and specialized development that we still innovating Herbs For Brain on an as yet inspecting and offer on Mike just give you a look a few results.
http://herbsforbrain.com/

19.01.2016 -

arabellalatin says …

This is really trusted Reviews

10.01.2016 -

spellau says …

When did Zymic die?

23.12.2015 -

rokupkhan007 says …

nice article for begineers

For More Information Visit
packers and movers hyderabad
packers and movers bangalore
packers and movers in mumbai

View all user comments for this tutorial.

Tutorial statistics

Date added:
25.08.2007
Author:
Alex Elliott
User rating:
4/5
Rate tutorial:
Total views:
13517
Total comments:
475

Advertisements