Code Snippet

Home » Code Snippets » jQuery » jQuery Sticky Footer

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.

    • 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. 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.

    • 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. 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. Works great and looks great for me on Safari, FF, and Chrome on Snow Leopard.

    Chris, you do good stuff. Keep it up!

  5. 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/

    • Hi this is cool….how did u do this plz give me code for that..thnx

  6. 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. 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. 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

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

  11. Fantastic, thank you it is very nice

  12. 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?

  13. Thanks for sharing.
    his will help in my project.

  14. if you don’t want no namby-pamby animation, add ” ,-1 ” like so:

    BEFORE:

    .animate({
    top: footerTop
    })

    AFTER:

    .animate({
    top: footerTop
    },-1)

    and presto.

  15. Gerd Moors

    This demo works nice on Mac OS X Lion with Safari, Firefox and Chrome.

    But I’ve the following question:

    I want to display an image in the footer and that image must be center in the middle of that footer.
    I still keep the image seeing on the left of the page…

    Can someone tell me how that can be done?

  16. oh what a nice solution!
    thanks for the tip!
    +1

  17. Adding a -10px margin-top to the footer straightened out most of my issues. Setting an absolute width on the footer as well. Takes some toying but works great. Thanks again Chris!

  18. Matthew

    Wow, I love the new theme on your website. Wish I could plagiarize it, lol, but of course I won’t that would be very unprofessional. I’m still waiting for my moment of inspiration to strike though. Unfortunately I’m just not this talented with graphic arts :(

  19. Hello.. Maybe I didn’t understand the article, but I was wondering if you could help me make my page work right in Safari. I added {clear:both;} to my CSS to make sure the footer didn’t overlap in Firefox, but Safari is just a complete mess and I’m not sure how to correct it. An example of the content overlaying the footer issue can be found here: (use safari)

    http://www.lifeafterhealth.net/recovery/healthy-things-to-do-each-day/

    I guess I’m trying to “unstick” the footer..? Any ideas :(

Speak, my friend

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 ~