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.
Set the CSS for the
#footer
to:You will then need to add a
padding
ormargin
to the bottom of your#sidebar
and#content
to match the height of#footer
or when they overlap, the#footer
will cover them.Also, if I remember correctly, IE6 has a problem with the
bottom: 0
CSS. You might have to use a JS solution for IE6 (if you care about IE6 that is).A similar solution to @gcedo but without the need of adding an intermediate content in order to push the footer down. We can simply add
margin-top:auto
to the footer and it will be pushed to the bottom of the page regardless his height or the height of the content above.You could use
position: absolute
following to put the footer at the bottom of the page, but then make sure your 2 columns have the appropriatemargin-bottom
so that they never get occluded by the footer.Flexbox solution
Flex layout is preferred for natural header and footer heights. This flex solution is tested in modern browsers and actually works :) in IE11.
See JS Fiddle.
HTML
CSS
Sticky footer with
display: flex
Solution inspired by Philip Walton's sticky footer.
Explanation
This solution is valid only for:
It is based on the
flex
display, leveraging theflex-grow
property, which allows an element to grow in either height or width (when theflow-direction
is set to eithercolumn
orrow
respectively), to occupy the extra space in the container.We are going to leverage also the
vh
unit, where1vh
is defined as:Therefore a height of
100vh
it's a concise way to tell an element to span the full viewport's height.This is how you would structure your web page:
In order to have the footer stick to the bottom of the page, you want the space between the body and the footer to grow as much as it takes to push the footer at the bottom of the page.
Therefore our layout becomes:
Implementation
You can play with it at the JSFiddle.
Safari quirks
Be aware that Safari has a flawed implementation of the
flex-shrink
property, which allows items to shrink more than the minimum height that would be required to display the content. To fix this issue you will have to set theflex-shrink
property explicitly to 0 to the.content
and thefooter
in the above example: