November 6, 2012

Tips and Tricks – Pure CSS Sticky Footers

Posted by in Development, Tips and Tricks

By now, if you’ve seen my other blog posts, you know that I’m fascinated with how much JavaScript has evolved and how much you can do with jQuery these days. I’m an advocate of working smarter, not harder, and that maxim knows no coding language limits. In this post, I want to share a pure CSS solution that allows for “sticky” footers on a web page. In comparing several different techniques to present this functionality, I found that all of the other routes were overkill when it came to processing time and resource usage.

Our objective is simple: Make the footer of our web page stay at the bottom even if the page’s content area is shorter than the user’s browser window.

This, by far, is one of my *favorite* things to do. It makes the web layout so much more appealing and creates a very professional feel. I ended up kicking myself the very first time I tried to add this functionality to a project early in my career (ten years ago … already!?) when I found out just how easy it was. I take solace in knowing that I’m not alone, though … A quick search for “footer stick bottom” still yields quite a few results from fellow developers who are wrestling with the same frustrating experience I did. If you’re in that boat, fear no more! We’re going to your footers in shape in a snap.

Here’s a diagram of the problem:

CSS Footer

Unfortunately, a lot of people try to handle it with setting a fixed height to the content which would push the footer down. This may work when YOU view it, but there are several different browser window heights, resolutions and variables that make this an *extremely* unreliable solution (notice the emphasis on the word “extremely” … this basically means “don’t do it”).

We need a dynamic solution that is able to adapt on the fly to the height of a user’s browser window regardless if the resize it, have Firebug open, use a unique resolution or just have a really, really weird browser!

Let’s take a look at what the end results should look like:

CSS Footer

To make this happen, let’s get our HTML structure in place first:

<div id="page">
 
      <div id="header"> </div>
 
      <div id="main"> </div>
 
      <div id="footer"> </div>
 
</div>

It’s pretty simple so far … Just a skeleton of a web page. The page div contains ALL elements and is immediately below the tags in the page code hierarchy. The header div is going to be our top content, the main div will include all of our content, and the footer div is all of our copyrights and footer links.

Let’s start by coding the CSS for the full page:

Html, body {
      Padding: 0;
      Margin: 0;
      Height: 100%;
}

Adding a 100% height allows us to set the height of the main div later. The height of a div can only be as tall as the parent element encasing it. Now let’s see how the rest of our ids are styled:

#page {
      Min-height: 100%;
      position:relative;
}
 
#main {
      Padding-bottom: 75px;   /* This value is the height of your footer */
}
 
#footer {
      Position: absolute;
      Width: 100%;
      Bottom: 0;
      Height: 75px;  /* This value is the height of your footer */
}

These rules position the footer “absolutely” at the bottom of the page, and because we set #page to min-height: 100%, it ensures that #main is exactly the height of the browser’s viewing space. One of the best things about this little trick is that it’s compliant with all major current browsers — including Firefox, Chrome, Safari *AND* Internet Explorer (after a little tweak). For Internet Explorer to not throw a fit, we need concede that IE doesn’t recognize min-height as a valid property, so we have to add Height: 100%; to #page:

#page {
      Min-height: 100%;  /* for all other browsers */
      height: 100%;  /* for IE */
      position:relative;
}

If the user does not have a modern, popular browser, it’s still okay! Though their old browser won’t detect the magic we’ve done here, it’ll fail gracefully, and the footer will be positioned directly under the content, as it would have been without our little CSS trick.

I can’t finish this blog without mentioning my FAVORITE perk of this trick: Should you not have a specially designed mobile version of your site, this trick even works on smart phones!

-Cassandra

    18 Responses to “Tips and Tricks – Pure CSS Sticky Footers”

    1. Arvy Says:

      Nice trick, very useful, thank you!
      Arvy, Sao Paulo, Brazil

    2. Neil Says:

      worked like a charm! after trying others that didn’t~

    3. John Brooks Says:

      Really nice trick. Will used in live projects!

    4. Robyn Says:

      Excellent

    5. Sasha Says:

      Thanks, but still not good for responsive design (position absolute…). Regards.

    6. Kevin Hazard Says:

      What do you mean, Sasha? It’s “position:absolute” specifically because it’s supposed to stick to the bottom of the page … The bottom of the page is always at the bottom of the page, so why would “position:absolute” be a problem?

    7. Nadine Says:

      Can’t seem to make this work on Chrome -,- too bad for me :/

    8. airtonix Says:

      kevin hazard, because there is a missing position:relative; on the #page

    9. Kevin Hazard Says:

      Thanks for the tip, airtonix. As we put this together and tested it, the position:relative; didn’t appear to be necessary, but including it does appear to improve browser compatibility.

    10. Matt Says:

      I’m also running Chrome and wasn’t able to get this working properly. Were you guys able to get it working in Chrome correctly? Would love to know if the problem is on my end or not. Thanks.

    11. How To Position Web Page Elements with HTML, CSS, and Absolute Positioning « Web Design Tips Says:

      [...] ryanfait.com/stickyfooter/, http://ryanfait.com/resources/footer-stick-to-bottom-of-page/, http://blog.softlayer.com/2012/tips-and-tricks-pure-css-sticky-footers/, and http://css-tricks.com/snippets/css/sticky-footer/ proved particularly useful. Many thanks to [...]

    12. Cassandra Says:

      Hi all! I’ve noticed an overwhelming positive response to this trick in particular, and have also noticed a lot of Chrome users having issues. I am re-evaluating this snippet to make it cross-browser friendly and will post an update soon!

    13. Dean K Says:

      As someone mentioned earlier, position absolute is not a good solution for a sticky footer. If you add enough content in main that causes the page to scroll, the footer will remain in it’s location at the bottom of the screen (similar to a fixed footer) and the content will go behind. If you scroll the page, the footer will stay in its absolute position and actually move up the page, always blocking the same portion of main’s content.

      Simply put, the footer will not be pushed down below the content if the content are is taller then the viewport height.

    14. Rockit Says:

      Here’s a good example:
      http://mystrd.at/modern-clean-css-sticky-footer/

    15. Code Hater Says:

      Good Call Rockit! Much better!

    16. TJ Says:

      dosen’t work in IE10 standard mode..

    17. How To Design a Basic Webpage Template with a Header, and a Footer | Web Design And Development Tips Says:

      [...] ryanfait.com/stickyfooter/, http://ryanfait.com/resources/footer-stick-to-bottom-of-page/, http://blog.softlayer.com/2012/tips-and-tricks-pure-css-sticky-footers/, and http://css-tricks.com/snippets/css/sticky-footer/. Many thanks to their authors! For a live [...]

    18. Angel Luv Says:

      Nice tricks to stick with for pure CSS sticky footers. Thanks for sharing this with us!

      Angel

    Leave a Reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word