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)
});
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”
})
}
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.
save me a day, thank you, bro.
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.
Why so rude? “Eew” is such an awful word to stay. Why not use “Thank you” instead? I mean you couldn’t even produce a javascript code yourself.
P.S to Mr. Chris:
Sorry for the rant. Just really pissed off.
Works great and looks great for me on Safari, FF, and Chrome on Snow Leopard.
Chris, you do good stuff. Keep it up!
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
I usually just stick to CSS when I’m doing a “sticky footer” but this is a cool example, thanks!
Cool example. I like to use this pure CSSsticky footer. Pretty similar to your example but with an empty push div.
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
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.
I love it footers have always gave me issues
it’s nice thanks.
Fantastic, thank you it is very nice
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?
Thanks i like trick, its really work
Thanks for sharing.
his will help in my project.
if you don’t want no namby-pamby animation, add ” ,-1 ” like so:
BEFORE:
.animate({
top: footerTop
})
AFTER:
.animate({
top: footerTop
},-1)
and presto.
Thanks, that -1 works really well.
ME GUSTA!!!! THANKS!!
you saved my life! :D thanks!
Brilliant! What a life saver!
Luco had the best fix for the animation.. kudo’s!!
(replace the current ‘1000’ with ‘-1’ that is). Other solutions on here didn’t work for me at all (would stick to the top.. or would stick to the bottom and would overlap whatever would be there already).
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?
oh what a nice solution!
thanks for the tip!
+1
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!
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 :(
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 :(
Thx for the post. Very usefull article! Will gonna change smth in my site.
queue: false might also do the trick :)
Doesn’t work on Firefox 3.6.17 on Windows XP (even the demo page on your website). Also, on IE7 the window keeps sliding down slowly then pops back up then slides back down.
Just an FYI to anyone wondering about old-browser support for this code. Not a complaint, in fact I think it’s a great piece of code and will probably use it anyway since those are quite old now.
Ok, here is a little modification to the above code. This prevents it from snapping onto the static position, just incase that’s what you were looking for.
Hi, Can you start using fixed positioning dynamically please? You have a nice following and all but you are doing yourself a disservice by using this old animation technique. Its distracting to the website to have it animate on resize. Try messing with fixed positioning just a bit and doing that in your JS rather than using absolute positioning. It doesn’t jerk. No need for animations. Just try it before you knock it.
You can see some examples here -> PolaraGolf.com on the checkout page or Klim.com on any of the side floating deals which I did.
Please let me know when you’re done and I’ll use your plugin. Thanks.
Am I missing something? That code seems more complicated than it needs to be. I made a sticky footer with very few lines of code. Seems to be working perfectly.
// When document is ready...
$(document).ready(function() {
// Introduce short variables
$box = $('#footer');
// Call method at page load
sticky($box);
// Call function every time window is resized
$(window).resize(function() {
sticky($box);
});
/*
* This method is in charge of 'sticking'
* a DIV to the bottom of the window at all times.
*/
function sticky($b) {
// Introduce short variables
$bHeight = $b.height();
$wHeight = $(window).height();
// Manipulate the DIV's style
$b.css('position', 'fixed');
$b.css('top', $wHeight - $bHeight);
}
});
Chris, as a simple suggestion, I think you might hook into both DOMReady and load events, so that sticky footer would immediately attach to the bottom of the viewport, and if after loading images, it needs to get to its original static positioning, it would.
This fits best for me:
I was able to solve this by adding an extra div at end of the footer div. Let say we call that div
div id=”stickyfooter”>
and in css use the following
#stickyfooter{
background-color: black;
background-repeat: repeat;
bottom: 0;
height: 100%;
pointer-events: none;
position: absolute;
width: 100%;
z-index: -1;
}
Now the background-color for my case was black. It should possibly change for your case and can possibly work in most cases
Hey thanks Chris and everyone here.
Maybe i’m missing something but dont we want to stick the footer to the bottom just if body height is less than window height? So lose the footerHeight its redundant..
Hi! found another way of doing it before I saw this post =)
CSS:
.stickyfooter {
position:absolute;
bottom:0px;
}
/otherwise it’s just a normal wordpress footer
The 311 is the height of the footer…
Although, I think Waynoss way is a bit cleaner…
Nice post man. I’m working on a project that was very useful. thank you!
Thank you very much Chris Coyier and Øyvind Hauge, both combinations are fine to use with or without animations, and thanks all the others also for sharing thoughts.
Works great man thankyou.
Very helpful. Cheers !
This is pretty frustrating, this isn’t working at all. It isn’t even throwing any errors on Chrome of Firefox. All you should need is the default jQuery libraries right?
Mine does absolutely nothing and I cannot figure out for the life of me why…
So for some reason this doesn’t work with an unordered list…
Slight refinement to slide the footer from it’s current position down to the bottom. As originally written, setting position: absolute will (invisibly) set the top to 0 causing the footer to animate starting at the top of the window. The solution is to set the current top position at the same time as position:absolute.
What about using pure HTML+CSS? check out http://ryanfait.com/sticky-footer/ (IE5+, and others)
I think its simply do like this
I’m having an issue getting the sticky footer to add a “top: —px” value. Anyone able to take a look here:
http://ancellcreative.com/fmm-multiculture.org/about/board-members
and possibly give any insight? Thanks kindly!
ignore my previous comment, I’ve figured out what my problem was
Thanks again!
Hi Chris, just wanted to say thanks, works great, exactly what I was looking for.
Tried several pure CSS solutions but most if not all of them required special tag layout and names/id’s, with this jquery solution it just plugged straight in and worked. Seems reliable in all browsers I have tried so far, cheers.
pure css sticky footer works.. until you declare a jquery link. This scripts solves that. thanks for the author.
Im an asian so pardon my grammar.
but.. it is not working on samsung tab
Thanks i like trick, its really work.
Hi.
Thank you for the code. Can some one please help me with the following problem:?
Here is the piece of code that i’m using currently:
$(window).bind(“load”, function() {
});
I am using a search & results jquery script so here is the result that I would like to have:
Footer needs to be at bottom of screen when there is less results.
But when results is more than the computer screen can handle I would like the footer to be pushed to under the last result of the search.
The results are displayed in a div.
Thank you for the help.
Wow this works man… have to test in Safari though.
Thanks.
There seems to be a problem as the footers height is double counted if it has a position of static (but not if it’s in absolute, as that’s how absolute works). I worked around it by setting the footerHeight to 0 (after it was set to the actual height of the footer) if it’s position is not absolute:
if($footer.css(“position”) != “absolute”){
footerHeight = 0;
}
Of course you could also say if absolute set it to the footer height and otherwise to 0.
By doing this, a footerHeight of 0 will be added to the body height if the footer is included in the body already (not in absolute).
The only thing that is a bit ugly now is the fact that it miscalculates 32px if you are logged into the WordPress, as there is this annoying adminbar which makes the viewport 44 pixels higher relative to the body. You could take that for granted (it’s only visible for admins) or use something like
if($(“#wpadminbar”).length){
footerHeight += 32;
}
That makes the value that is compared to the viewport height 32 pixels higher, but only if the adminbar is present.
Mr Daniel Flores, Thank you for sharing this. The code in the comment is really cool and it works for me.
I am having trouble implementing this in a case where both “html” and “body” tags have “height: 100%” defined in css.
It seems to allow either “body” or “html” to be set to 100% height, but not both.
Any idea why this is?
(and I really need to have both defined as 100% because of another script I’m running)
I ran into issues whereby on certain pages the footer should’ve been about an inch away from the bottom of the previous block and therefore should’ve been sticky to the bottom of the screen, but it didn’t. My quick fix for that was that underneath footerTop = ($(window).scrollTop() + $(window).height() – footerHeight) + “px”; I would set footerHeight=0; (basically back to 0… but only AFTER the footerTop line, NOT REPLACING the previous footerHeight). This fixed that issue for me. (Replacing the original footerHeight caused the footer to end up way too low…) Just an FYI for anyone who ran into the same issue…