FortySeven Media

Kick Awesome Web Design, Graphic Design & Media Creation

Making Your Footer Stay Put With CSS

Monday, January 14, 2008 ~ 10:50 pm

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:

Footer is broken

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!

Footer fixed

Update: Sorry guys, we had to turn off comments. Too much spam :(

  • Social Web
  • E-mail

Your Email:*  

Your Name:*

               To:*

Submit the word you see below*

Posted by (JavaScript must be enabled to view this email address) in Design ~ Tutorials ~

Hi, thank you for the tutorial!

I don’t know what I’m doing wrong, but for me it’s only working when the only thing there is the containter and the footer.  When I add the other divs inside the container, the footer does not stay put.

I made sure all the heights were correct, but it still doesn’t work with the other divs.

Any ideas on what I could be doing wrong?

thanks!

Never mind, I think I fixed the problem.  It might’ve been more than one thing, but one of the problems had to do with the IE6 hack. Maybe there was something wrong with that code (but I copied & pasted it, so I don’t know where it went wrong) but anyway I re-put in that code, and now the footer seems to be working as it should.

Thanks again and God bless!

lily

Glad you got it figured out Lily!

thank you. thank you. thank you!

Hi,

Thanks for sharing this! I have to put a footer on my site and I’m wondering if this would work? I’m worried about setting the margins that I already have established to zero,  they have gotten a little messy as the site has evolved - heheh!

JB

@Jon Sorry, I’m not sure what you mean. We don’t talk bout setting any margins to zero.

What do I do if I have a footer with height: auto; in order for the content to grow?

@Henrik I have no idea. Haven’t found a good solution. This is for a fixed height footer only. Maybe there’s some jQuery stuff out there that could help?

This is a great little trick, I use it all the time, thanks!

Nice article, I would normally position a footer using floats, but in one site I needed the footer the width of the html, not the width of the body (which I’d normally set to the width of the central content).

It could be worth adding that whilst using my method above I’d use a { margin: 0 auto; } in the body and float everything within this. Using your method, you need to apply this margin method not to the #container element, but to each div within the container individually to achieve the centre alligned content effect which many would be after.

@Doug I usually just margin: 0 auto the container and everything stays inside it while it’s centered. Just don’t float the container. You shouldn’t have to setup the auto margin for every div.

Great, it worked really well for me.

I found a bug with what I copy-pasted from this article:

The tags <!—[if lt IE 7]> and <![endif]—> are shown with a special “long dash” instead of two normal dashes. probably the auto-formatting from your site’s backend that made this..

Hey guys, GREAT tutorial!! I’ve been trying to find a fix for this for years, especially with the wide proliferation of screen sizes and aspect ratios nowadays! This is the simplest, and best fix, that I’ve seen yet! Thanks for sharing!

wow, thats really helpful, thank you!

Beautiful work around.  BTW…how much longer will we have to build for IE6?  I really like your site design, very clean.

it does not work in safari or firefox.

@bhavna I’ve never had any problems in Firefox, Safari or IE. Don’t forget there’s three places to set the footer height.

thank you so much, ive been having trouble with this, and looked at so many wrong solutions but this worked great.

Hey, thanks so much for writing this. I’ve gotten farther with this code than many other solutions I’ve found.

However, I’m still having trouble getting my content background to reach the footer.  Is there something I’m doing wrong, or will this solution not work for this type of problem? confused


http://bit.ly/6iyIhs —is the temporary page I’m testing on.

@steph I took a quick look, but there are a lot of nested divs. I would guess it has something to do with your container div having a bunch of other divs on top of it. That will mess with the 100% height declaration.

This has always been my source for this solution! And I will be using it on my own personal site when I’ve gotten round to re-designing it. Keep up the awesome helpful work!

Hi, thanks for this fix. I had the scroll bar on my site. Problem was that i had margin-top on the wrapper, so what i did is i removed it, and added padding-top to header.

@Glad to help, Aleski!

I just wanted to address Derek’s concern about not being able to click the links located in the footer.  I’ve gratefully used Jonathan’s solution for some time, and I know if that you don’t mark BOTH the main body div (id=“container” in this example) AND the footer div (id=“footer” in this example) - the links will not work.

Thanks for the tip, Brant!

« First  <  5 6 7

Leave Your Thoughts Behind

Commenting is not available in this weblog entry.

Live Preview

 
Separator
Design Hope for Startups 2009
Stuff we Love

BestChristianDesign is helping give churches inspiration for their next project. Good call, guys.

Seth Godin reminds us to enjoy and take advantage of the uphills.

Stop announcing your new project. Just do it.

Rock and Roll people, Rock and Roll.

To all you staff designers out there, here’s a link for you. (via @ryanlascano)

Fascinating look into the music business.

Derek Sivers posts some great advice about balancing your life.

archives

Rad Deals
Troy McNeil - Things I said

Denskägg
Names and Places

Buy in iTunes
Tweets
profile image

@ryanirelan Hate that, too. Was hoping it would grow on me but too many panes all over the place. Too much like my desktop :D

Thu 9:53 | Follow
profile image

I've noticed I'm not noticing my spelling errors. I need more sleep.

Thu 9:40 | Follow
Tweets
Recommended

ExpressionEngine

Basecamp