CSS – Sticky Footer
Half the websites out there have what is called a footer, or a block at the bottom of the page with quick links. For example, my website has a footer at the bottom of the page if you scroll down with links to major sections of my site. When I was looking into implementing this on my site, I ran into some problems. On some pages, the content didn’t fill the whole screen (which also varies with different screen resolutions, so the footer would be in the middle of the screen. Obviously, on pages where the content went past the bottom of the screen, this wouldn’t be a problem because the footer would be pushed to the bottom. So clearly, I needed a footer that would “stick” to the bottom of the screen if the content wouldn’t push it, but if there was enough content, then it would be below the screen bottom. I searched and searched for solutions, and I found one which I adapted just a little bit into my own implementation (in thanks, here is a link to his site, ryanfait.com.
Ryan’s solution was fantastic. However, I did still have some problems with it, probably because of my wrapper div that I used to center my content. So below, I have implemented my fix which I have tested in Firefox 2 and 3, Internet Explorer 6 and 7, Safari, Opera, and Chrome. The footer sticks to the bottom of the screen when content doesn’t push it that far, but goes to the bottom of the page when content goes below the fold. Below is the CSS and HTML for the footer with a wrapper as well. The sticky footer combined with a wrapper allow you to account for pretty much any screen resolution) too, which is fantastic! However, there is just one downside to this sticky footer, you absolutely MUST know the height of your footer. Luckily, there are not many cases where you’d be putting dynamic content into your footer.
First, the CSS:
html{
height: 100%;
}
body{
height: 100%;
}
div.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -100px auto;
The -100px mentioned above needs to be the exact height of the footer.
}
div.footer{
height: 100px;
clear: both;
position: relative;
}
div.nudge{
height: 100px;
clear: both;
}
And now the HTML:
<body>
<div class=”wrapper”>
Your main content goes in here
<div class=”nudge”></div>
The nudge div should remain empty.
The empty nudge div will make space for the negative margin assigned above.
</div>
<div class=”footer”>
Your footer content goes in here
</div>
</body>
</html>
To see a working example of this footer, obviously you can look on this page (because I have one implemented here), or you can check out this example page that I made.
So yeah, it works pretty simply, though I must stress that every line of that CSS is important. You must declare a 100% height for the two parent elements of html and body (and if you have any other tags between those and the wrapper div, then you need a 100% on that as well). This forces the browser to use the whole screen. The three separate height declarations for the wrapper div are each individually important, including the !important addition. !important is a statement which makes that lines declaration take precedence over declarations that come after it. This however does not apply in IE, but that’s okay, because we want it that way for this Sticky Footer. Then most important is how the margins work. By putting a negative margin of the footer’s height on the wrapper, we allow whatever comes after the wrapper to overlap with it. The nudge div creates empty space inside the main wrapper so that when the footer overlaps exactly with the wrapper, nothing is blocked out. If you theoretically wanted a margin between the wrapper and the footer then make your negative margin on the wrapper 100px, your footer div would be 70, and give the footer a 30px margin on top. The nudge div would also still be 100px again to make space for the footer and its margin.
As a last suggestion, I usually put another div inside the wrapper div and the footer div, give them both a width of 990px and make them float left. The float with the full width will make sure that nothing (such as padding or a margin) manages to creep outside the edge of the wrapper or footer div. This is a safety precaution, but one that I highly suggest. If you notice a problem when you try to implement the sticky footer, this is the first thing that I would suggest.
Got any questions? Need help troubleshooting? Critiques are welcomed too! Post a comment.
January 16th, 2009 at 12:34 AM
You are an amazing human. I spent so much time trying to make Fait’s code work in Firefox. This worked perfectly. Thanks!
June 11th, 2009 at 2:01 AM
I have a question on using this with php actually.
how will you use this using include header and footer? any solution?
thanks a bunch!
June 11th, 2009 at 5:51 PM
Thanks for the question Lawrence. All that a php include does is transfer content straight from the included file into the file that calls it. The best way to work with php includes (for a header and footer) is to create one entire page and then split it up into the page that changes, plus the header and footer template files.
To put it more simply regarding the example code written above, you could put everything up to and including the opening wrapper div tag in the header template file. Then put everything starting from and including the closing wrapper div tag into the footer template file.
I hope that helped!
June 12th, 2009 at 12:09 AM
Hi Dave,
Aside from the sample i gave you via email. I have another one using the code you use. The PHP files that i wanted works all right this time.
Thanks mate!
July 13th, 2009 at 10:40 PM
I was helping someone implement this sticky footer and I actually found a minor bug which I thought I should point out. The sticky footer works, but the bug appears if you apply a background to your wrapper div. Because of the height definitions, and an absence of a float definition, the wrapper background will not appear beyond the user’s screen size. To fix this, add a float: left definition to your wrapper (or, if you are using the centered wrapper described above, create a new div that starts just inside your wrapper and closes just before your nudge div and give that both the float and background properties. That should solve that apparent problem.
This was actually only a bug in Firefox, go figure. That’s probably the only firefox issue I’ve ever come upon!
August 4th, 2009 at 9:24 AM
[...] http://www.davidjrush.com/blog/2009/01/css-sticky-footer/ [...]
August 20th, 2009 at 10:17 PM
Thank you so much, finally my tweaks to my site work, thanks to your simple coding, now to change over 200 pages, eek.