With this entry I want to start a new bunch of articles under the subtitle of „Pure CSS“. Basically this means that I will be posting a number of articles dealing with web design issues and ways those issues can be solved in the most efficient and elegant way using CSS only! no JavaScript!
Specifically in this article, titled “Sticky Footer”, I will give you a step by step guide on how to create css sticky footers at the bottom of the page. I know that there are other methods out there and this issue has attracted more attention recently, but this is what I use and in my view it’s cleaner and applicable to existing HTML structures.
It fulfills all main requirements of this type of solution: (a) it sticks at the bottom of the page only if all other content of the page does not occupy the full height of the page; (b) it works on IE5+, FF1+, Opera7+, Safari2+ and maybe even more; (c) and most importantly, it’s pure CSS.
So let’s get started…
The first thing we do is get acquainted with the basic HTML markup for this project. As I mentioned before this is a common HTML structure. We have a page container and content inside. The only thing that might be odd is that the footer is below and not inside the page container. This is important. Other than that it’s pretty straightforward:
<div id="container"> <div id="content"></div> </div> <div id="footer">My Sticky Footer</div>
Secondly, stretch html, body and page container elements to full window height. Notice that to make page container inherit body overflow I am using min-height property. Since IE6 or earlier does not support this, we have to make sure that IE, but only IE, applies height property to fix it. Here’s the right way to do it:
html, body,#container{ height: 100%; } body > #container { height: auto; min-height: 100%; }
Thirdly, we have to place the footer right after page container, creating a body overflow. However, we want it to be visible, so we forcibly move it up overlapping the bottom of page container — negative top margin for that reason. To make sure the whole footer is visible, negative margin must be equal to defined footer height. To fully secure footer’s position let’s clear both sides of floating elements, just in case.
#footer {
clear: both;
position: relative;
z-index: 10;
height: 3em;
margin-top: -3em;
}
Finally, we have to protect page content bottom from finding itself behind the footer by doing this:
#content { padding-bottom: 3em; }
Note that padding value is equal to footer height.
There you have it! You might want to check this sticky footer in action to get a sense of how it’s working.
April 14th, 2008 at 7:22 pmBuy Phentermine wrote:
Great Post, Thanks for sharing it. It is always good to read someone’s else point of view.
I Have bookmarked it for future use.
April 26th, 2008 at 11:58 pmKen wrote:
Great… very simple explanation and code for a sticky footer. What I am trying to do is create a layout that has a Header, more than one column (all of which will be equal height and stretch to fill the screen to the footer) and then the sticky footer. Is this possible?
May 18th, 2008 at 3:23 pmJeroen wrote:
Nice, i tried this on my own site but i have an issue. In IE it looks good but in Firefox i have to scroll down a bit (see: http://www.klassedesign.nl/test). I don’t see what i am doing wrong. Can someone help me?
Thanks!
May 18th, 2008 at 4:14 pmadmin wrote:
Jeroen, it’s your logo element that’s causing body overflow. Specifically, logo’s top margin. Margin creates space outside the element and makes nearby elements inherit this excess. In this case it would be recommended to use padding (inside space) instead. Keep that in mind, everybody.
May 18th, 2008 at 5:13 pmJeroen wrote:
Thanks! that works very well! I thought it was my logo when i was experimenting with firebug but this is the right solution.
June 6th, 2008 at 5:59 pmnic wrote:
you’re still popping up in google searches for CSS sticky footers…and it’s a good thing, too, because this is the clearest and best way to get a simple sticky footer. good work!
June 13th, 2008 at 12:46 pmSarah wrote:
Thanks for the clear explanation! I seem to have the same problem as Jeroen I checked about the use of margins but I don’t seem to be able to solve the problem. Could you help! The site I’m working on is:http://www.hyponetwork.de/index.php?id=1
Thanks!
September 2nd, 2008 at 6:17 pmtwgirl wrote:
hi
great, this works for me :)
i’m having a little problem though…
i have a div with absolute position since i need it to stay in the same place no matter what…the div would be the element thats under all the other content
the footer is working as this div doesnt exist, since it goes over the div until it finds the end of the rest of the content…
can u help me fix this little problem please?
thanks!
September 12th, 2008 at 9:25 amAbhishek wrote:
Nice Tutorial. Crisp and Clear. Thanks
September 12th, 2008 at 6:23 pmadmin wrote:
twgirl, it sounds like a specific issue. I (we) would have to see your code to figure out what’s going on. Good luck!
October 8th, 2008 at 8:22 pmAdded by a PAL to FAQ PAL wrote:
Pure CSS: Sticky Footer…
Specifically in this article, titled “Sticky Footer”, I will give you a step by step guide on how to create css sticky footers at the bottom of the page. I know that there are other methods out there and this issue has attracted more attention rece…