One problem I run into pretty frequently when coding a site in to XHTML and CSS is making my footer dock to the bottom of the screen. It’s especially annoying if you have a page that’s short on content and the footer, which happens to be a different color that the body background doesn’t stay at the bottom of the browser window. I can hear you say, “But why don’t you just do a fixed position on it. That’s easy enough.” True, but if you do that then it’s always at the bottom of the screen no matter how tall the window is. So if I have a page with a lot of content that footer shouldn’t show up until the content is done. How do we fix this? Let me show you. Here’s what the problem looks like:
This tutorial assumes a few things: 1. That you know basic HTML formatting, and 2. That you have a pretty good understanding of CSS.
So first we need to make sure that everything except the footer is inside a container div. So your code would look something like this:
<div id="container">
<div id="header">Header</div>
<div id="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
</ul>
</div>
<div id="content">Content Here.</div>
</div><!--End Container-->
<div id="footer">Footer Here.</div>
Now that you’ve got your basic HTML set up, let’s head over to the CSS and see what we need to do there to get it to work. First, we want to give the html and body tags a height of 100%:
html, body {
height: 100%;
}
Next, we need to set the container div to position:relative, min-height: 100% and negative margin the bottom the height of the footer. What? A fixed height footer you say? Yes, I haven’t quite figured out the best way to do it otherwise:
#container {
min-height: 100%;
margin-bottom: -330px;
position: relative;
}
Now, onto the footer. Make sure the height on the footer matches the height of the negative margin on the container and set the position to relative:
#footer {
height: 330px;
position: relative;
}
What’s that? Yes, I know it doesn’t work yet. There’s still a bit of magic to be done. See it turns out we need a div to help separate the footer from the container. We’re going to call it clearfooter. So jump back to your HTML and add the div class right inside the closing div of the container. That’s very important. Inside the closing div:
<div id="container">
<div id="header">Header</div>
<div id="nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
</ul>
</div>
<div id="content">Content Here.</div>
<div class="clearfooter"></div>
</div><!--End Container-->
<div id="footer">Footer Here.</div>
Head back over to your CSS file and add this to the clearfooter div. Notice the height again:
.clearfooter {
height: 330px;
clear: both;
}
Alright! So for those of us using browsers that aren’t stupid, this should work great. But 80% of use that one browser that is stupid, our friend Internet Explorer 6. Thankfully, it’s a pretty simple fix. I know, how often does that happen? First of all, you should know that IE7 should work fine with what we’ve done up to this point. So all we need is an IE6 specific “hack.” In your header where you’re calling your external stylesheet put this:
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="ie6.css" />
<![endif]-->
This is a conditional statement for IE6 so it will load the CSS file only in that browser. In the CSS file, all you have to put is:
#container {
height: 100%;
}
So there you have it! This works in IE 6 and 7, Firefox and Safari 2 and 3. I hope you’ve found this a useful tool in your CSS arsenal. If you know a better way, please let us know in the comments!
@Kevin, I’m not sure why you would have a scroll bar on shorter pages; I’d have to see some code or something. I would guess it has something to do with not changing all the heights to the same value. I know if I miss the clearfooter div it makes the page taller than it should be.
@ Jonathan, I’ve checked your portfolio in IE6 and it works really fine. I guess, something was amiss when I was implementing the similar solution. Thanks.
That’s really helpful I was trying to work that out last time and gave up ..will give it a go again with this tute
It’s a good demonstration - and in general, I like your writing style and the site’s design – nice work! But (yeah, there’s a “but” here), I have to admit the fixed height requirement in this technique is a deal breaker for me :(
I’m a stickler for “bulletproof” designs and feel it’s important to ensure at least a small amount of flexibility for vertical expansion - and at least in your implementation here, this technique breaks fairly quickly. I also am not a big fan of the extra, empty, non-semantic div, though I’d be more accepting of that *if* the fixed height weren’t an issue.
Of course, here I am pointing out these perceived faults and yet I can’t offer up (right now) any solid alternatives. I actually haven’t come across this challenge in any recent designs I’ve coded, but quite some time ago I recall using an approach that included absolute positioning to fix an element (not a footer, a banner) to the bottom of the browser window. I also recall that approach not working 100% smoothly across the major browsers and requiring a few “hacks” as well.
It’s a wonder sometimes, all the hoops we have to jump through to achieve seemingly simple design requirements, eh? Thanks again for the article!
I’m fairly sure it is something in one of my inherited css styles. If I figure it out I’ll post an update, and if I don’t I’ll post some code.
Thanks.
Kevin
Nice, but there’s a way to work around using an empty DIV (something that should be avoided) to clear everything. Just add overflow:auto; to the container DIV and it should work perfectly.
Interesting approach.
However, most designers I know have been using Cameron Adam’s FooterStickAlt solution for quite some time.
http://www.themaninblue.com/writing/perspective/2005/08/29/
It works a treat.
Great Code. One thing in fire fox it seems the push div is covering my footer in firefox. It looks right but you can not click the links in the footer only ie…
Any suggestions
Hey Derek, I’m not sure I follow what your problem is. Code or a link might help.
@everyone else: thanks for all the great alternate methods. Whenever I get a chance to catch my breath I’ll try them out and update the post accordingly!
it just wont work
i’m so noob =(
Hey temhawk, what part of it isn’t working?
I think the problem was the way I had styled the #container. You’re probably not supposed to have it absolutely positioned, but I had, and if I gave up that absolute positioning, the whole page would become screwed.
Anyway, I redid all the html and css, but I used a different method, which I found a bit easier to understand. ( http://alistapart.com/articles/footers )
That method apparently also needs some javascript to fix in some IE versions. but I didn’t do that, so I don’t know if it’s necessary (because I think there is a shorter css way to fix it with IE conditional comment/hack, but I can’t test that since I don’t have IE...)
Thanks for the quick reply, I could have easily needed help with this today, but for some reason I got lucky yesterday (after spending two full days trying to make a footer!!!!) and I’m OK for now
PS: I haven’t asked anyone yet, but I thought about it very much in my desperate attempts at footers the last few days, but is it going to be easier in css 3 to do footers? I hope so =S
Glad you got it figured out, temhawk. Not sure about CSS3; When IE starts to support it then I’ll worry about it
If I were you I’d worry about whether I will ever have to worry about CSS3 when (IF) IE starts to support it.
By the way, is IE8 going to be any different than the past seven plagues? I also thought Microsoft didn’t want to continue making IE after version 7. I guess they wont back down too quickly
Hahaha, 7 plagues. I just laughed out loud! I don’t know if it’ll be better or not. Surely, since IE7 was better than 6. From what I’ve read they seem like they are trying to support standards better. Here’s to hoping!
thx for the code.
Ooh, thanks for this tutorial! It very useful, especially for my next design works.
I’m not sure what I’ve done wrong, but the footer appears at the bottom of the window, not the bottom of the page.
http://www.dvreground.com/footer/index2.html
http://www.dvreground.com/footer/styles/main.css
http://www.dvreground.com/footer/styles/ie6.css
Could you look at the code and see what I’ve done wrong?
Also, don’t worry about no images I uploaded it just so you could look at the code
Looks fine in Safari and Firefox but yeah it’s a mess in IE6. I would first try using pixels for your height and negative margins and then try taking out your “pAreas.” Since they’re all positioned absolutely and not floated I think that may be part of the problem. Just put a bunch of text in the container div and see what happens.
That’s interesting, because i have Firefox (maybe your using Firefox 3, which I don’t have). I don’t have IE6 though. In IE7, it was messed up as well.
I’ve got it working now, but I would prefer not to have them floated as it keeps them relatively pushed together at higher resolutions.
Nope, I was using Firefox 2. If you use ems for your padding it will shrink and grow based on the width of the browser window. I rarely use absolute positioning as it doesn’t seem to have good cross-browser support. Floats are more reliable
Just make sure you put display: inline on them for IE6 or it’ll double your margins. Good luck to you!
thank you, that’s what I needed
))))) :**
Hey thanks for the awesome tutorial. Footers have been the one extremely frustrating element I’ve yet to figure out, this just put me over.
Hey Domotronic, glad to be of help! Go make some kick-awesome.
A beautiful, heartbreaking photo essay from Phillip Toledano.
Hahahaha, Vista sucks.
This is some crazy cool church projector stuff.
For all of us who hate voicemail trees.
Don’t break the rules to get ahead. Work hard and it’ll pay off.
CSS Tricks has a nice, plain english version of major CSS bugs in IE 6.
Number six in my article on Top 10 Tips for Web Design was to make awesome icons. PSDTUTS has a great article on how to design them effectively.
The best Content Management System for designers. Feature rich, completely customizable, and easily extendable.
Basecamp is the smarter, easier, more elegant way to collaborate on your internal and client projects.
April 22, 2008
Hey Pavan, if you’re doing a print stylesheet, just take out most of the min-height stuff and positioning. As long as the footer comes after your content it should be in the right place. It’s a good idea to have a print and screen stylesheet anyway, especially if you know the page will be printed a lot.