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

17.11.2014 -

bryanparker45 says …

Education tells us the culture of the area. What is the culture in our society, which we should have to maintain our culture and civilization? We should aware about our history which we have reorganization of our culture and civilization with the help of education.

http://www.oneidauniversity.com/online-degree-programs/
http://www.charlestonstateuniversity.com/

17.11.2014 -

bryanparker45 says …

Superb one! I have never read such a nice article; topic and the comments are good. I have learned many things from all your post and this one was the best post for me. I really enjoyed reading it and craving more tips from you… thank you for such an engaging article.

http://www.sheffieldstateuniversity.com/professional-degree/

10.11.2014 -

ocaab3 says …

Hi! everybody, Our shop cheap wholesale and retail Air Jordans shoes,Air Max Shoes,Designer Handbags,Jewelry,Belts,hats,Jeans,Sports Jerseys,sunglasses online,high quality goods and Low price,outlet online,free shipping, Welcome to ==== http://www.fashionfast.net ==
nike air max shoe $42
Air Jordan (1-24) shoes $42
Jordan (1-22)&2014 shoes $42
Nike shox (R4, NZ, OZ, TL1, TL2, TL3) $38
Handbags ( Lv fendi D&G MK) $42
T-shirts (polo, ed hardy, lacoste) $14
Jean (True Religion, ed hardy, coogi)$34
Sunglasses ( Oakey, coach, Gucci, Armaini)$12
New era cap $16
Jewelry $15
FREE SHIPPING
http://www.fashionfast.net

http://www.fashionfast.net

http://www.fashionfast.net

http://www.fashionfast.net
http://www.fashionfast.net

http://www.fashionfast.net

http://www.fashionfast.net

http://www.fashionfast.net

===( http://www.fashionfast.net )===

===( http://www.fashionfast.net )===

===( http://www.fashionfast.net )===

===( http://www.fashionfast.net )===

===( http://www.fashionfast.net )===

===( http://www.fashionfast.net )===
This is a shopping paradise
We need your support and trust

27.10.2014 -

jamilbd says …

just try

16.09.2014 -

polo2014 says …



http://www.shopbestgoods.com/
http://www.nike-jordanshoes.com/
http://www.beatsbydreoutlet.net/
http://www.michaelkorsus.com/
http://www.polo-tshirts.com/
http://www.northsclearance.com/
http://www.ralph-laurensale.com/
http://www.gucci-shoesuk2014.com/
http://www.michael-korsusa.com/
http://www.polo-outlets.com/
http://www.ralph-laurenhome.com/
http://www.marcjacobsonsale.com/
http://www.mcmworldwides.com/
http://www.salongchamppairs.com/
http://www.canada-gooser.com/
http://www.burberryoutlet2014.com/
http://www.michaelkors.so/
http://www.hermes-outletonline.com/
http://www.oakley-sunglassoutlet.com/
http://www.north-faceoutlets.net/
http://www.moncler-clearance.com/
http://www.woolrich-clearance.com/
http://www.barbour-jacketsoutlet.com/
http://www.moncler-jacketsoutletonline.com/
http://www.monsterbeatsbydres.net/
http://www.louis-vuittonblackfriday.com/
http://www.lv-guccishoesfactory.com/
http://www.mcmoutlet-jp.com/
http://www.cheapdiscountoutlet.com/
http://coachoutlet.iwopop.com/
http://www.coachsfactoryoutlet.com/
http://www.coach-blackfriday2014.com/
http://www.coach-storeoutletonline.com/
http://www.coach-factorysoutletonline.com/
http://www.coachccoachoutlet.com/
http://www.coach-factories.net/
http://www.coach-pursesoutletonline.com/
http://www.llouisvuitton-factory.net/
http://www.coach-outletsusa.com/
http://www.mksfactoryoutlet.com/
http://www.zxcoachoutlet.com/
http://www.mischristmas.com/
http://www.misblackfriday.com/
http://www.bestcustomsonline.com/
http://www.newoutletonlinemall.com/
http://www.clickmichaelkors.com/
http://www.cmichaelkorsoutlet.com/
http://www.ralphlaurenepolo.com/
http://michaelkorsoutlet.mischristmas.com/
http://mcmbackpack.mischristmas.com/
http://monsterbeats.mischristmas.com/
http://northfaceoutlet.mischristmas.com/
http://mk.misblackfriday.com/
http://coachoutlet.misblackfriday.com/
http://coachfactory.misblackfriday.com/
http://uggaustralia.misblackfriday.com/
http://coachpurses.misblackfriday.com/
http://coachusa.misblackfriday.com/
http://coach.misblackfriday.com/
http://michaelkorss.misblackfriday.com/
http://michaelkors.misblackfriday.com/
http://airmax.misblackfriday.com/
http://michael-kors.misblackfriday.com/
https://www.facebook.com/pages/Coach-Factory-Outlet-Online-Store-Michael-Kors-Outlet-Online-Sale-75-Off/712060898859091
https://www.facebook.com/pages/Ralph-Lauren-Polo-Outlet-Online-Sale/1404100279810690

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:
94495
Total comments:
464

Advertisements