21-07-2008

jQuery jQuery sticky footer

This will make an element with id footer stick to the bottom of the window, but only if the document body height is less than the window height. If it isn't it 'll just follow the normal document flow.

If instead you want to make the footer always stick to the bottom of the window (comparable to css statement 'position:fixed; bottom:0' ) use the second example.

No margin,border or padding should be set on the element, only on its children if need be, for this to work properly:

$(function(){
	positionFooter(); 
	function positionFooter(){
		if($(document).height() < $(window).height()){
			$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})
		}	
	}
 
	$(window)
		.scroll(positionFooter)
		.resize(positionFooter)
});

Without the body height conditional the footer will always stick to the bottom of the window, regardless of the body height:

$(function(){
	positionFooter(); 
	function positionFooter(){
		$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})	
	}
 
	$(window)
		.scroll(positionFooter)
		.resize(positionFooter)
});

Comments:

26 comments.
Your comment:

»
steve 22/05/2009, 2:39 am
great tutorial, have you gotten this to work with the jquery animate function for smoother scrolling up and down?
CJ 30/10/2009, 1:31 am
Change this:
$("#pageFooterOuter").css({position: "absolute",top:($(window).scrollTop()+$(window).height()-$("#pageFooterOuter").height())+"px"})

To:
$('#pageFooterOuter').css({position:'absolute',bottom:0})
ping 31/10/2009, 2:25 pm
CJ - what's point of that? - doesn't deal with difference between window and document height. Also if you gonna do that you might as well just use css statement and no javascript.
nood 10/04/2010, 4:16 am
position:absolute is fine if the window contents are not bigger than the window, but position:fixed is required otherwise.
Gautam Murarka 26/03/2010, 1:46 pm
Can anyone show me the demo link?
nood 10/04/2010, 4:15 am
This is very jerky. What's wrong with simple CSS: position:fixed; bottom:0; ?
joriso 13/04/2010, 3:22 pm
IE6 sadly doesn't support position:fixed. Also this doesn't adjust to the total document height. Read the first paragraph.
sabmark 11/05/2010, 7:00 pm
wow.. thanks for this.
Andrei 21/06/2010, 1:28 pm
On my site it worked well on IE8, FF3.6, Opera10, but wrong way on IE6,7. So I think i'd have to refuse from this version of sticky footer
Andrei 21/06/2010, 2:10 pm
Manage to fix the problem in old IE versions. Sorry for my previos post
Max 03/08/2010, 1:30 pm
How do you fix problem with IE?
Linas 29/01/2011, 4:06 pm
Where to place this code?
stefano solinas - obsidianart 29/01/2011, 6:09 pm
What about something like that ? (image that footer is 60 pixel Height and I want 20px margin);
Now the footer clean move from fixed to relative and everything is more mac website style (the shopping cart on the left on the custom configuration).

jQuery(function(){
var $ = jQuery;
positionFooter();
function positionFooter(){
var winH = $(window).height();
var docH = $(document).height();
if( docH <= winH ) {
$('#content').css('margin-bottom','80px');
$("#footer").css({'position': "fixed",'bottom':'0'})
} else {
$('#content').css('margin-bottom','20px')
$("#footer").css({'position': "relative"})
}
}

jQuery(window)
.resize(positionFooter)
});
GAG 25/02/2011, 4:46 am
Please remove the apostrophe in "...only on it's children if need be...". You want the possessive version of the word, which is "its". Using an apostrophe means it's a contraction and short for "it is" or "it has".
pipo de clown 03/03/2011, 5:39 pm
Brilliant, thanks !
Fanzy 16/06/2011, 8:31 am
what if there's hidden contents in dynamic accordion or tabs?
position absolute goes wrong when the contents show up and the page is not refreshed.
Greg 31/10/2011, 7:59 am
Excellent solution, exactly that I need.
Most of sticked footer solutions based on css layout, but it not always possible.
So Thank you again, it save my time.
RL 07/11/2011, 9:28 am
Problem with IE9
Please change this:
$(messageHolder).css('bottom', 0);
To:
$(messageHolder).css({position:'absolute',bottom:0});
Shamsudheen.TK 04/04/2012, 4:14 pm
awesome code.Its working fine.. thnks :)
Fred Manceaux 04/04/2012, 6:45 pm
this does not render correctly when you change the window size quickly,
try this : http://www.codicode.com/art/the_best_sticky_footer.aspx
i call 12/05/2012, 12:26 am
bullshit
Nadeem Malik 24/05/2012, 10:37 am
We've all tried to use CSS Sticky Footer one time or another, but they never seem to come out right, do they? Well, the days of a hard to understand CSS Sticky Footer are thankfully over. With just a few simple CSS classes with minimal extra HTML markup, I've fashioned a CSS Sticky Footer that even beginners can get a handle on.


http://www.handycss.com/tutorials/creating-sticky-footer-in-css/

Petrov 13/06/2012, 9:51 pm
Tanks mate, you saved my day :)
asda 14/09/2012, 8:41 am
asda
Maxime Passenier 08/03/2013, 8:55 am
Came accross a problem using this technique, on a mac, you can always scroll.
This means that if you include scrolltop into the calculation, the page will always get bigger if a user scrolls.
I had to add an extra line of code which takes into account if the document.height is equal to the window height he rules out the scrolltop.
my code:
var scrollTop = ( $(window).height() < $(document).height() ) ? $(window).scrollTop() : 0;
var pos = ( $(window).height() - node.bottom.height() + scrollTop );
theElement.css( 'top', pos );
kiku 22/05/2013, 10:13 am
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php
http://www.hardcode.nl/images/TTest.php

 

[x]