A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Code Snippets > jQuery > jQuery Sticky Footer Submit one!

jQuery Sticky Footer

In general the CSS Sticky Footer is the best way to go, as it works perfectly smoothly and doesn't require JavaScript. If the markup required simply isn't possible, this jQuery JavaScript might be an option.

HTML

Just make sure the #footer is the last visible thing on your page.

<div id="footer">
    Sticky Footer
</div>

CSS

Giving it a set height is the most fool-proof.

#footer { height: 100px; }

jQuery

When the window loads, and when it is scrolled or resized, it is tested whether the pages content is shorter than the window's height. If it is, that means there is white space underneath the content before the end of the window, so the footer should be repositioned to the bottom of the window. If not, the footer can retain it's normal static positioning.

// Window load event used just in case window height is dependant upon images
$(window).bind("load", function() { 

       var footerHeight = 0,
           footerTop = 0,
           $footer = $("#footer");

       positionFooter();

       function positionFooter() {

                footerHeight = $footer.height();
                footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";

               if ( ($(document.body).height()+footerHeight) < $(window).height()) {
                   $footer.css({
                        position: "absolute"
                   }).animate({
                        top: footerTop
                   })
               } else {
                   $footer.css({
                        position: "static"
                   })
               }

       }

       $(window)
               .scroll(positionFooter)
               .resize(positionFooter)

});

Demo

View Demo

Subscribe to The Thread

  1. Seems to have a few kinks… appears to queue animations if you keep resizing and dragging the window size larger and smaller?

    • you could either just add a .stop(); before the animation. Or, don’t use .animate at all, just set the top value with the .css function. Just a little jerky that way.

    • BEata says:

      I am using your demo files that already has the stop() in it and the animation is till happening.
      How can I stop it at all and just have the footer stay at the bottom of the page?

      $footer.css({
      position: “absolute”
      }).stop().animate({
      top: footerTop
      })
      } else {
      $footer.css({
      position: “static”
      })
      }

  2. Michael says:

    sorry, but it is not very smooth… what’s about:
    { position: fixed; bottom: 0; width: 100%; }

    • That is completely against the whole point of this. This STOPS before the bottom of the content, so it doesn’t overlap any content. Fixing it to the bottom will happily lay over the top of content.

    • Elite Seo says:

      That is completely against the whole point of this. This STOPS before the bottom of the content, so it doesn’t overlap any content. Fixing it to the bottom will happily lay over the top of content.

  3. EVula says:

    Mac 10.5.8, Safari 4.0.3… this is incredibly buggy. Resizing the window makes the footer shift (a good thing), but it doesn’t sit flush with the bottom of the window, instead having a big white border show thru. Eww.

    • Seems to be running fine for me with the same exact specs. I just added the .stop() before the animate, which might help. I think Safari was just firing a shit-load of resize events and you computer was probably having trouble to keep up.

    • Seems to be working fine for me.

      OS X 10.6.2 using Chrome 4.0.2, Safari 4.0.4 and FF 3.5.5; keep up the good work.

      It did get a little jumpy but it seems to be more my computer than the code itself.

  4. Kris says:

    Works great and looks great for me on Safari, FF, and Chrome on Snow Leopard.

    Chris, you do good stuff. Keep it up!

  5. metal says:

    you could achieve the same result entirely through CSS, utilizing 100% heights properly along with absolute positioning… the resulting CSS would actually be more “liquid” when resizing a window, and thus not have the buggy effect of the footer jumping around after the window has been resized. the important thing to pay attention to, is to get the footer to NOT overlap your content, but rather force a scroll when it hits the bottom of that container (which this demo succeeds at)…. example of pure CSS version: http://oribe.com/

  6. Thad Bloom says:

    I usually just stick to CSS when I’m doing a “sticky footer” but this is a cool example, thanks!

  7. Cool example. I like to use this pure CSSsticky footer. Pretty similar to your example but with an empty push div.

  8. Mauricio says:

    Buenisimo Chris!
    Era justo lo que estaba buscando.
    Mil gracias desde Argentina.
    Mauricio

    Awesome Chris!
    It’s just what I was looking for.
    Thanks a lot from Argentina.
    Mauricio

  9. Ant says:

    This method has some bugs when using zoom, but it’s good when too lazy to make css trick.

    And advantage of that method is that it works on footer without defined height (some sites have long lists in footer and you cannot use height for it).

    Note that you will have to use width:100% for footer, because position:absolute shrinks width to content. And you may also have to use footer wrapper, if you want to center it.

  10. Stuart says:

    I love it footers have always gave me issues
    it’s nice thanks.

  11. Fantastic, thank you it is very nice

  12. Lkenneth says:

    This is excellent thanks so much. Is there away to make it start from the bottom rather than slide down from the top when the script loads?

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---