FortySeven Media

Jonathan Longnecker

Jonathan Longnecker

January 14, 2008

Making Your Footer Stay Put With CSS

One problem I run into pretty frequently when coding a site in to XHTML and CSS is making my footer dock to the bottom of the screen. It’s especially annoying if you have a page that’s short on content and the footer, which happens to be a different color that the body background doesn’t stay at the bottom of the browser window. I can hear you say, “But why don’t you just do a fixed position on it. That’s easy enough.” True, but if you do that then it’s always at the bottom of the screen no matter how tall the window is. So if I have a page with a lot of content that footer shouldn’t show up until the content is done. How do we fix this? Let me show you. Here’s what the problem looks like:

Footer is broken

This tutorial assumes a few things: 1. That you know basic HTML formatting, and 2. That you have a pretty good understanding of CSS.

So first we need to make sure that everything except the footer is inside a container div. So your code would look something like this:

<div id="container">
    <div id="header">Header</div>
    <div id="nav">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
      </ul>
      </div>
<div id="content">Content Here.</div>
</div><!—End Container—>
<div id="footer">Footer Here.</div>

Now that you’ve got your basic HTML set up, let’s head over to the CSS and see what we need to do there to get it to work. First, we want to give the html and body tags a height of 100%:

html, body {
height: 100%;
}

Next, we need to set the container div to position:relative, min-height: 100% and negative margin the bottom the height of the footer. What? A fixed height footer you say? Yes, I haven’t quite figured out the best way to do it otherwise:

#container {
min-height: 100%;
margin-bottom: -330px;
position: relative;
}

Now, onto the footer. Make sure the height on the footer matches the height of the negative margin on the container and set the position to relative:

#footer {
height: 330px;
position: relative;
}

What’s that? Yes, I know it doesn’t work yet. There’s still a bit of magic to be done. See it turns out we need a div to help separate the footer from the container. We’re going to call it clearfooter. So jump back to your HTML and add the div class right inside the closing div of the container. That’s very important. Inside the closing div:

<div id="container">
    <div id="header">Header</div>
    <div id="nav">
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Page 1</a></li>
        <li><a href="#">Page 2</a></li>
      </ul>
      </div>
<div id="content">Content Here.</div>
  <div class="clearfooter"></div>
</div><!—End Container—>
<div id="footer">Footer Here.</div>

Head back over to your CSS file and add this to the clearfooter div. Notice the height again:

.clearfooter {
height: 330px;
clear: both;
}

Alright! So for those of us using browsers that aren’t stupid, this should work great. But 80% of use that one browser that is stupid, our friend Internet Explorer 6. Thankfully, it’s a pretty simple fix. I know, how often does that happen? First of all, you should know that IE7 should work fine with what we’ve done up to this point. So all we need is an IE6 specific “hack.” In your header where you’re calling your external stylesheet put this:

<!—[if lt IE 7]>
  <link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]—>

This is a conditional statement for IE6 so it will load the CSS file only in that browser. In the CSS file, all you have to put is:

#container {
height: 100%;
}

So there you have it! This works in IE 6 and 7, Firefox and Safari 2 and 3. I hope you’ve found this a useful tool in your CSS arsenal. If you know a better way, please let us know in the comments!

Footer fixed

Update: Sorry guys, we had to turn off comments. Too much spam :(

Share the Love

FortySeven Media
Did you know?

Besides designing websites, FortySeven Media also makes the Kick Awesome Show and is currently working on Kicktastic - a project to tell you how we’re making more money. Sign up if you’re interested.

Comments

  1. @marchello Oh good! I was just looking at it and couldn’t find any difference smile Yeah, I’ve run into the same problem of trying to repeat bg images on HTML and Body…for some reason they don’t play nice reliably. Sound like you figured out a good solution. Good luck to you!

  2. Great post.  It works by itself, but when I attempt to implement it into my code, the footer ends up covering my content and does not stick at the bottom of the page under the content like I want it to. 

    Right now, my markup is like this:

    #container
      #header
      #nav
      #content
      #sidebar

    I think the problem has something to do with my #main-content div and how I have it laying on top of an image. 

    I just want the *happy word* footer to stay under my content div and I cannot for the life of me figure out what’s wrong.

  3. Hi jonathan, thanks for the tutorial :D
    I implemented it on a site i’m doing http://www.easybookkeeping.net.au/index.html  but on IE 6 it’s not sitting at the bottom of the page when the content is short ?

    on ffox i’ts working ok. smile

  4. huhuhu… why didn’t it work when i tried it??......

    i’ll still gonna do a second try tomorrow, for now,,, i have to get some sleep, or else, i’m gonna turn into a ZOMBIE sick !... just kidding.. hehe xx smile

  5. hehehe… thanks for checking it out. smile

  6. Hey Julius, You’re still calling your container as a div id on your IE6 stylesheet, but it’s a class on your site. Probably from copying and pasting from ours. Change it to:

    .container {
    height: 100%;
    }

  7. Hi Jonathan, thanks soo much, It works!! Appreciate your time helping me smile

  8. it is a very nice work. Keep up. I enjoyed reading it. Thnaks

  9. Awesome tutorial ! smile

    If any got vertical scrollbar with small amount of content,

    It is like that because you putted .clearfooter OUTSIDE container.. and you MUST put .clearfooter INSIDE container div smile

    In my case container means wrapper wink

  10. Good call Charlie! Also if you add padding or margin to your footer, don’t forget to add that to that magic number you have to put on the footer, clearfooter and container.

  11. Thanks Jonathan !
    Yes the magic number is also important smile I hope to see more cool tuts like that one :D

  12. I’ll try this (and the problem with other similar solutions, by the way, is with firefox 3, not with ie7), but would it not be a good idea to include a box which contains all of the final css and another which contains all of the final html?  would save the user the trouble of assembling these little snippets.

  13. Hey Founder, yeah it would be a little bit easier, but I think it’s much better if you understand it rather than copy and paste wink

  14. it did not, by the way, work for me
    but I found another solution, though again it works better in ie7 than in ff3

  15. Hey Founder, sorry you couldn’t get it to work. I’ve used it on like 15 sites so I know it’s solid. Usually just a typo or something if it’s not working.  Although if you’re having trouble with FF3 there may be other problems with your layout. IE7 is the one that’s hard to deal with. smile

  16. Hi guys,

    I am having issues getting this to work in Firefox and Google Chrome (IE 6 is fine, but haven’t test IE 7 yet).

    To help you understand my situation better, I have a Wrapper and then a Container within the wrapper.  I am sure that’s my issue, but I can’t do without the nested containers.

    Take a look at this site to see my issue.  Footer isn’t sticking in Firefox 3.0.5 or Google Chrome.
    http://dev.harvestreston.org/Mens.aspx

  17. My screen resolution is 1280 x 1024, so I am sure for some users it will be fine, but not those who are using the same resolution (or greater) as me.

  18. Hello Audiospirit,

    It looks like you have to many closing divs smile Or maybe Im wrong.. anyway it look like your div with class clear footer is OUTSIDE wrapper… itshould be IN WRAPPER DIV at end of it if you will do that it will work perfectly ;]

  19. Everything checked out ok on that note.  I was probably doing some changes when you caught the clear footer outside. 

    THE FIX:
    Since this website in using ASP.NET, the CSS requires the inclusion of the “form” element when adding 100% height values to html and body.

    BEFORE: 
    html, body {
                      height:100%;
    }

    AFTER: 
    html, form, body {
                      height:100%;
    }

    Problem solved!

  20. @audiospirit Good call on the form element. I’m not that familiar with ASP so I’m glad you figured out what was going on.

  21. Have somebody tested this solution with a page with JS based accordions or slide-downs?

  22. @sevgilier Yeah this site has some slide downs in the portfolio section. Just click the contact button under each entry to see what I mean. Should work just fine.

  23. This is a great blog, thank you for putting this information up.

    I’ve been struggling with this method for awhile now. I thought I had everything shipshape until I hit this snag:

    http://brianborupub.com/testbed/whoisbb.html

    This page has more content than the others built so far and the footer no longer behaves properly in FF2 on the Mac. I haven’t tried it on other browsers/systems yet. However, the other pages have been tested across browsers/systems and are fine.

    I would appreciate any help you can give!

  24. @hgranger I don’t have FF2 anymore. If you could make sure your code validates and test on other browsers first that will help narrow things down.

  25. Hi Jonathan,

    Sorry if I wasn’t clear… the code validates (except for missing alts), that is the first thing I checked.

    I have tested it across browsers and systems now and found the behavior consistent but not what I hoped for!  downer

    I’m guessing it is a positioning or a padding issue. I’m off to wrassle with it now, thanks for your help!

  26. More Comments:   ‹ First  < 2 3 4 5 6 >  Last ›

Leave Your Thoughts Behind

Commenting is not available in this channel entry.