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. No problem! Looks like it’s because your right info div is absolutely positioned. Take that off and float the left and right info divs accordingly (and add widths). If you want the right one to grow and shrink with the browser you’ll need to look into a fluid with fixed sidebar layout. I can’t recall exactly how to do it, but a Google search should help. I just remember it was weird and kind of messed with my head smile

  2. Hah! Yes, I am looking at something like that now and it is a brain kinker! Ah well, when I get it figured out I will post it somewhere and let the world know…

    cool smile

  3. thank you…

  4. Hi, nice tutorial!

    I dont know whats going on but when I do it, rather than the footer being at the bottom of the page, it actually sits exactly below the viewable page. So basically i see the page and there is a scrollbar to the right, and I have to scroll down to see the footer. the amount I scroll though is exactly the size of the footer.
    Doesnt matter how I resize the browser window, its exactly the same. Footer will always site just below the “view” line.

    Triple checked everything, followed your guide to the letter.

  5. @edward, sorry man I need a visual; I have no idea what you’re trying to describe. Is it in every browser? Is your code valid? More info would help wink

  6. Very clear and easy to follow tutorial.

    Unfortunately I’m having the same issue that the footer is lying just below the viewport. It’s acting as if I don’t have the negative margin on the bottom of the container. In fact, if I double the negative margin, then the footer moves up so you can see it in the viewport, leaving the same amount of body space below the footer.

    It acts like this in FF3, IE7, Chrome and Safari, so it’s something in how I’ve coded it. The site validates fine.

    http://wisecatwebdesign.com/staticsite

    This is a set up for creating a template for a particular CMS, so there are a couple extra divs and things in the code to make it easier to port over.

    When/if I locate what I’m missing, I’ll post here to see if it will fix your problem as well, @edward.

    This site is not likely to stay in this staging area for a long time, so if you are reading these comments in a few months, the site probably won’t be there.

  7. Jonathan, thanks for replying sorry I didnt reply back so soon. I was gonna post an example but Andrea has just described my problem exactly. I’m glad to know I’m not the only one having this problem.

  8. @Andrea, I’ve looked this a bit and I’m not sure what the problem is. I think it might be that your header is not inside the container…seems like it’s getting pushed down about as far as the header is tall. But without hacking up the HTML I’m not sure smile

  9. I don’t think its the header pushing it down because if that were so wouldnt that mean it is pushed down as far as the header tall ? With mine I’ve set the footer to 50px and the header is probably something like 80px tall. Yet the footer isnt pushed down by 80px, but rather by 50px.

    It seems that it is being pushed down according to the size of the footer rather than header.

    Would you like a zip of my code (its really really short) to take a look yourself?
    Anyway thanks Jonathan and Andrea.

  10. Well, it’s nice to know that I’m not missing something obvious.  I swapped the container div start to above the header and it does make the footer work fine. Of course, it screws up my header background… I’ll play around with it later this week and see if I can get both of them to work together.

    Thanks for taking the time to look at my code.  You wrote a great tutorial and it’s not your responsibility to fix all of our coding issues. I appreciate the time you’ve taken to do so.

  11. Ok, ok, I’m supposed to be working on another deadline, but I couldn’t just leave it at that.

    Instead of reusing the “container” div I already had, I created an additional div “containerwrap” that contained the header plus my old container. I moved the pertinent layout css from container to containerwrap and now both the footer and the header are working as I’d like. I checked it in IE7, Firefox, chrome, and safari. I don’t have easy access to ie6 so I’ll check that later.

  12. Hahaha yeah I know what you mean…those things bug me; can’t let them go. Glad you got it figured out! Hopefully IE6 will play nice. That’s wishful thinking though smile

  13. Thanks I had this problem on my web site. And here I can see a great solution (one of the best I’ve ever seen).

  14. doesn’t work with the given code.

    fix: add to container
    padding-bottom: 330px;

  15. @Bobby, sorry man, code works great. I’ve used it on 10-15 projects at least. Adding padding to the bottom of the container will just cause problems.

  16. Nice tutorial, I’ve been using this method before but it’s always good to have it somewhere to show people how to do it without having to explain them every single time wink

    @Bobby : adding a bottom padding would always add a vertical scrolling to your pages even if the content would fit in the page: 100% + 330px = more than 100% = scrolling.

  17. if there was a Digg button here I’d be tapping it faster than a kid to candy!
    really nice work!  +fav

    I love the site design of fortysevenmedia ... its bloody sexy!
    great job and congrats!
    grin

  18. Question . . . I have a 3 page site and for some reason the footer is working on one page but not the other two. The two pages it isn’t working on are short pages and the footer is sitting right under the content instead of at the bottom of the page.

    I did cross browser check it and it seems to be working in IE6 and on Firefox on a PC but it has the gap on Safari and Firefox on the mac. I think it is Firefox 2 on the mac and Firefox 3 on the PC.

    Any suggestions about what I should do?

  19. @Heather, Hmmm, never run into that problem before. A link or some code might help us figure it out, though.

  20. I had to move the files to a site where you could view them. Thanks for your help!

    http://heatherbsmith.com/times/

  21. Hey Heather, you’ve got to to put the

    html, body {
      height: 100%;
    }

    in there and also you’ll need to use some sort of reset for the margin and padding

    * { /* removes defaults from all elements */
      margin: 0;
      padding: 0;
      }

  22. well have i got some gubbins - i’ve followed practically every method so hope you still post here!

    I have a 2 column layout (1 header, 1 left navigation and 1 content area).  And i want to apply a footer to it.

    The length of both the left navigation and the content area are changeable.  There are no background images applied to anything (just background colors).

    I want the footer to lie at either the bottom of the navigation or the content (whichever is longest).  However if there is not enough navigation or content to fill the browser window I want the footer to sit at the bottom of the browser window.  Furthermore(!) if there is not enough navigation to fill the navigation bar with the background color is has then i want it to automatically fill the rest of the column with the background color either to the length of the browser window or the length of the content (whichever is longest) and then for the footer to appear.

    I can fill the navigation panel by setting a min-height:100% and height:100% styles but this then throws the footer halfway up the page.

    Any ideas?

  23. Hey Bendybogle, you’ll need to use a repeating background image on your navigation area to get that color to go all the way down regardless of the content in it. I’m pretty sure everything else is covered with this footer fix.

  24. Nice posting,very informative, i also interested in the seo services and web development.Thank you, very interesting to read, you should be proud of your blog. I was really enjoying to check your messages from time to time. We are looking forward to your future posts.

    Repair Credit

  25. Okay,  I tried this and while my footer is at the bottom nicely where it should be (thank you btw), it is now over to the far left instead of centered under the content columns.  I’m sure the fix is something simple—anyone have any suggestions.  It is this way for IE7 and FF.

  26. More Comments:   ‹ First  < 3 4 5 6 7 > 

Leave Your Thoughts Behind

Commenting is not available in this channel entry.