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…
October 10th, 2008 at 8:03 pmJatin Kaka wrote:
nice tutorial…. will bookmark it for future reference.
crazyrahul84 | Designer | Iexplorehere
October 12th, 2008 at 11:25 amColin wrote:
I noticed that hidden in your own CSS file referenced from the example is:
p{margin:0;}
and if I remove that then a scroll bar appears.
I do not understand this - I thought that the default was zero anyway. Can anyone explain this, and how to get to behave as default while allowing the sticky footer to keep to the designated size?
October 12th, 2008 at 10:50 pmadmin wrote:
To Colin and all others that don’t want to import my reset.css. You should replace:
with:
Good luck!
October 13th, 2008 at 12:00 pmColin wrote:
Unfortunately, that does not work for me (FF 3.0.3). If I put a background over the “content” div, I see a white top margin, which pushes the displayed area larger than the window, however large the window is! Putting p{margin:0;} solves this, but I do not understand how the “p” margin has leaked out into the enclosing section.
October 13th, 2008 at 2:52 pmadmin wrote:
Colin, again, it sounds like a specific issue. Provide us with a link to your page and we’ll see if we can help. Margins, by the way, create outer, cross-layer space, so it can leak outside parent layers.
October 14th, 2008 at 5:56 pmcolin wrote:
Try your sticky footer, but remove the link to your CSS file.
I’ve just done it in IE7 - same thing - the scroll bar appears when the margin for “p” is not set to zero,
Other than that, this is a really neat solution - much better than others that I have seen.
October 15th, 2008 at 4:17 pmJohn wrote:
Hi
Thanks for the post. I normally use the Ryan Fait’s sticky footer solution (http://ryanfait.com/sticky-footer/), but for some reason it simply would not work on the site I was cutting up today.
Anyway found your solution (the principle is basically the same but your implementations are different) and it worked like a treat.
Cheers
October 16th, 2008 at 6:06 pmColin wrote:
There is a problem with IE if you float your “about” div. The footer will obscure the last line when the window is too small to hold both the content and the footer. The “clear:both” is clearly not working in IE. This is the solution that I have found - there may be a better one:
First create a new class in the CSS:
#prefooter {
margin: 0 0 1px;
width: 100%;
height: 1px;
clear: both;
}
and then put a suitable div in after the “about” div, so the structure looks like:
.. Whatever…
My Sticky Footer
That seems to satisfy IE and does not upset FF.
October 19th, 2008 at 8:10 pmadmin wrote:
Thanks Colin for your feedback.
Regarding issue #1, see this sticky footer without the link to my CSS file. Paragraph margin is set to 1em and it still looks fine on all browsers.
Regarding issue #2 I was actually hoping IE to enclose a float within a container ‘automatically’ (which it still does in many cases and websites), but I will have to look into this in the near future why it’s not happening in this simple example.
October 25th, 2008 at 2:29 pmColin wrote:
I have re-visited the padding problem. What I did originally was to remove your “theme” stuff as well as the link to your other CSS file. I wrongly attributed the scroll bar appearance to the p margins. I now discover that it is the padding applied to your “about” div. If the padding is non-zero, then there is no scroll bar. If the padding is zero, then scroll bar appears. BUT: if the padding is too small, then when the window is short then the footer overlaps the content. If you give the footer an opacity of around ,5, then you can see what overlaps with the padding too small.
November 1st, 2008 at 4:03 amSean Bannister wrote:
Here’s some screenshots of this method from a heap of browsers you’ll notice it doesn’t work under IE 5.01 and 4.0 :
http://browsershots.org/http://www.lwis.net/profile/CSS/sticky-footer.html
Due to the small browser share of IE 5 this doesn’t really bother me but it would be nice to work it out.
November 13th, 2008 at 9:16 pmCiaran wrote:
I found this to work much better than Ryan Faits code, which doesn’t work in Firefox no matter how hard I try. It always put the footer at the bottom of the screen as opposed to the bottom of the content. I’d recommend this version anyway, seems more robust. Cheers!