I have a simple 2-column layout with a footer that clears both the right and left div in my markup. My problem is that I can't get the footer to stay at the bottom of the page in all browsers. It works if the content pushes the footer down, but that's not always the case.
Update:
It's not working properly in Firefox. I'm seeing a strip of background color below the footer when there's not enough content on the page to push the footer all the way down to the bottom of the browser window. Unfortunately, this is the default state of the page.
Multiple people have put the answer to this simple problem up here, but I have one thing to add, considering how frustrated I was until I figured out what I was doing wrong.
As mentioned the most straightforward way to do this is like so..
However the property not mentioned in posts, presumably because it is usually default, is the position: static on the body tag. Position relative will not work!
My wordpress theme had overridden the default body display and it confused me for an obnoxiously long time.
Here is a solution with jQuery that works like a charm. It checks if the height of the window is greater than the height of the body. If it is, then it changes the margin-top of the footer to compensate. Tested in Firefox, Chrome, Safari and Opera.
If your footer already has a margin-top (of 50 pixels, for example) you will need to change the last part for:
One solution would be to set the min-height for the boxes. Unfortunately it seems that it's not well supported by IE (surprise).
I wasn't having any luck with the solutions suggested on this page before but then finally, this little trick worked. I'll include it as another possible solution.
To get a sticky footer:
Have a
<div>
withclass="wrapper"
for your content.Right before the closing
</div>
of thewrapper
place the<div class="push"></div>
.Right after the closing
</div>
of thewrapper
place the<div class="footer"></div>
.Use CSS vh units!
Probably the most obvious and non-hacky way to go about a sticky footer would be to make use of the new css viewport units.
Take for example the following simple markup:
If the header is say 80px high and the footer is 40px high, then we can make our sticky footer with one single rule on the content div:
Which means: let the height of the content div be at least 100% of the viewport height minus the combined heights of the header and footer.
That's it.
... and here's how the same code works with lots of content in the content div:
NB:
1) The height of the header and footer must be known
2) Old versions of IE (IE8-) and Android (4.4-) don't support viewport units. (caniuse)
3) Once upon a time webkit had a problem with viewport units within a calc rule. This has indeed been fixed (see here) so there's no problem there. However if you're looking to avoid using calc for some reason you can get around that using negative margins and padding with box-sizing -
Like so: