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 ~

nice tutorial. thanks for sharing with us

The problem I’ve found with Ryan’s sticky footer is that it doesn’t take kindley to using margin’s within the content pane. I almost always always need to use margins AND padding, and hence can’t substitute the former for the latter as he suggests.

I’m going to try this method you’ve explained in your blog. Thanks.

Heyyy, this is great!  It’s the one thing I found that was easy to read and actually worked.  Thaaaanks. :D

Good tutorial and easy to understand, many thanks.

Thank you so much. I’ve been searching all morning to find a fix to my footer issue.
content height ? 100% thats right! Thank you thank you thank you

Got a nice blog site. keep blogging now.

Thanks much for the tip, this has always annoyed me as well.  CSS is nice, but definitely a few quarky things like this that make it difficult.

amazing. i spent several hours trying to figure this one out until i read your post. so helpful!!! thanks smile

Hi,

I have used this solution and it works well. However, i get the vertical scroll-bar no matter the size of the page is. I need to get this to work with no scroll bars on screens with smaller contents. Can you please suggest an alternative?

Thanks
Bindhu

Hey Bindhu, it doesn’t create scroll bars unless the content is long enough to do so. Double check you’ve set the same height in all three places: container (negative bottom margin), clearfooter and footer.

Hi, thanks a lot for this beautiful tutorial! I’ve been looking for this for a long time!
Now it finally works for me, there is just one thing.. it doesn’t seem to be working in IE7.. would you know how this comes?

Thanks in advance for looking at the page and thanks again for the tut!

Hi Raphaël,

It should work just fine in all the browsers; you may want to run through the tutorial again and double check everything. Make sure you’ve got the same pixel number for the height of the footer, height of the clearfooter and negative margin on the container.

I have gone through the tutorial numerous times, all the heights are correct. I just can’t figure it out.
It works in all browsers accept IE7.

It seems to work fine in IE7 over here..if you’re referring to the Alizé site.

I am, if you click on the AVC text in the footer you will see that the footer doesn’t get stretched to the bottom.
Somehow I think the clearfooter div doesn’t work in IE7?

I’m not sure exactly what’s going on there, but your background stops halfway through and it stops where the footer stops so it looks like somethings’ s not closed or cleared properly. It’s doing the same thing in every browser, but IE7 isn’t as forgiving. I did a quick validation check on that page and there’s almost 400 errors, so that would be the best place to start. If you get those cleared up and it’s still doing it, let me know!

Seems I’ve got the opposite problem that Raphael had, the footer doesn’t stick to the bottom of the page in any browser except IE ha ha. Any ideas what would be causing that?

My bad! I was premature, I had kept an old div from another sticky footer attempt ha ha. Thanks for sharing your method, much appreciated!

Hey Chuck, yeah man you gotta watch those extra divs! Glad we could be of help; good luck!

Thank you much!!!

Hi!
This great tutorial finally solved my problem!!!
I have one tip for those who have web hosting with PHP support.
I found a browser detection script that can return the browser type and version number.
It could be used instead of the ie6 CSS file hack in some cases.

An example how this script could be used:
If the browser type is ie and the version number is smaller than 7,
echo out IE6 specific CSS code.
Otherwise echo out the CSS for modern browsers.

The script works with a wide range of browsers (if not with all of them)
and there are many ways how to use it.
The script’s home page:
http://techpatterns.com/

Keep on coding!!!

@MatejGolian Thanks, but I don’t really see how that’s any different. You’re still specifying IE6 specific CSS anyway. Now if you could use it target some other browser, like say Firefox that would be interesting. I ran into a bug in FF3 that would have been nice to target with CSS until they got it fixed.

To Jonathan:
I guess, you can use the script to detect any type of browser from Safari Internet Explorer, Opera to Firefox (Of course I’m not 100% sure. = I’m not an expert.).

Quote from the page (Sorry if a bit long.):
“This script uses arrays to hold both browser and OS versions. It checks these against the navigatorUserAgent() strings, then packs either os or browser variables with the proper browser and os information. It also features several different test return conditions which should cover most situations you run across.
The main advantages of this browser detection script is that it will not be fooled by Opera or Safari, no matter how Opera is set to identify itself. It can also be used to deliver browser specific CSS stylesheets to the page depending on what browser is requesting the page. Since this is run on the server side, it doesn’t require client side Javascripting to be enabled to function.
This code is more or less what we use for all our websites, in one form or another, when we need to set browser CSS and other coding particulars.
Currently this script identifies the following specific browsers: Opera, Internet Explorer 4, Internet Explorer, Mozilla/Netscape Gecko based browsers, Netscape 4.x, Konqueror, Safari. It id’s Linux, Mac, most of the main Unix flavors, Windows NT, Windows regular. It is easy to add more browsers and OS’s if required.”

It returned Firefox 3 data as:
browser: moz
version number: 1.9.
I’m not sure if this is correct or not.

Help!!!!
Im having some troubleshooting with firefox and the sticky bottom footer:

Before adding your code, iv being used a background image at the very bottom behind the actual footer to be x-repeated at the “html” class tag in css. (As well, an x-repeated background image at the very top that goes at “body” class tag in css). When i add your solution works great for the actual footer in all browsers (including firefox). BUT the x-repeated bg image at the bottom in the html class doesnt stick to the real website´s bottom, just sticks at the Firefox browser window, so once you scroll you can see a gap between this bg and the actual footer.

removing the “height:100%:” at the html, body class, i noticed this bg sticks back to the very bottom, but the actual footer doesnt…

How can i solve this silly problem in firefox? Setting both xrepeated bg image and footer sticked together against the bottom?

here`s the Url : http://www.srgordo.com/oliva
it works perfect either in ie or safari

Sorry guys! raspberry
I spend a while trying to solve it by myself and actually i found the mistake. 

I shouldnt use the html class to set the bg image at the bottom. i built another footer div wraping around the “actual” footer with a 100% width and set the x-repeated bg there. Do you think its alright?

Now, it sticks at the bottom as i wanted, thanks a lot for your tutorial!

 <  1 2 3 4 5 >  Last »

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

Troy McNeil
Things I Said

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