I'd like to use the CSS property position:fixed to fix the position of an element but at the same time prevent the element from being positioned over my footer when the user scrolls to the bottom of the page. Is there a way of stopping an element from showing over a footer in this way?
-
I don't think I've explained this very well! What I mean is that when you scroll down the page the element shouldn't go any lower than the footer. It should disappear behind the footer, but rather stay fixed about the footer if you scroll that far down the page. – Alex May 10 '10 at 20:15
-
So, you want to position the element somewhere on the screen, but when you scroll down and the footer comes into view, you want it to stay above the footer, correct? And the footer is high enough (and the fixed element low enough) for both of them to come in contact with each other? – Alec May 10 '10 at 21:19
-
Ha it didn't help that I made a typo. 'It SHOULDN'T disappear behind the footer' is what I meant of course. What you wrote is exactly right. It needs to be position:fixed until the footer comes into view. Do you have any ideas what this kind of behaviour might be called? I've tried searching Google but nothing has come up. – Alex May 10 '10 at 22:14
4 Answers
If you want the element to be fixed sometimes but not others you will need to use JavaScript to add/remove position:fixed
First of all set the element without position:fixed
so that it appears in the page above the footer where you want it once scrolled to the bottom.
Then set a css class that when added to the element fixes its position, something like:
div#sometimesfixed.fixed
{
position:fixed;
bottom: 0px;
}
The following code uses jquery, detects the scroll position on the page, and adds/removes the fixed
class accordingly:
$(window).scroll(function (event) {
var windowTop = $(this).scrollTop();
if (windowTop >= $("#footer").offset().top) {
$("div#sometimesfixed").addClass("fixed");
} else {
$("div#sometimesfixed").removeClass("fixed");
}
});

- 1,885
- 17
- 25
As long as all the elements are positioned (absolute or relative) you can use the z-index
attribute. The default value is 0, so give your footer a higher value and it will appear above the other content.

- 1,211
- 1
- 11
- 17
-
Apologies, I didn't phrase my question well. I actually want the element to stop in place above the footer rather than disappear behind the footer. – Alex May 10 '10 at 20:37
I ran into this same issue recently.
You can achieve a solution leveraging the position
property of the element with jQuery, switching between the default value (static
for divs
), fixed
and absolute
.
You will also need a container element for your fixed element. Finally, in order to prevent the fixed element to go over the footer, this container element can't be the parent of the footer.
The javascript part involves calculating the distance in pixels between your fixed element and the top of the document, and comparing it with the current vertical position of the scrollbar relatively to the window object (i.e. the number of pixels above that are hidden from the visible area of the page) every time the user scrolls the page. When, on scrolling down, the fixed element is about to disappear above, we change its position to fixed and stick on top of the page.
This causes the fixed element to go over the footer when we scroll to the bottom, especially if the browser window is small. Therefore, we will calculate the distance in pixels of the footer from the top of the document and compare it with the height of the fixed element plus the vertical position of the scrollbar: when the fixed element is about to go over the footer, we will change its position to absolute and stick at the bottom, just over the footer.
Here's a generic example.
The HTML structure:
<div id="content">
<div id="leftcolumn">
<div class="fixed-element">
This is fixed
</div>
</div>
<div id="rightcolumn">Main content here</div>
<div id="footer"> The footer </div>
</div>
The CSS:
#leftcolumn {
position: relative;
}
.fixed-element {
width: 180px;
}
.fixed-element.fixed {
position: fixed;
top: 20px;
}
.fixed-element.bottom {
position: absolute;
bottom: 356px; /* Height of the footer element, plus some extra pixels if needed */
}
The JS:
// Position of fixed element from top of the document
var fixedElementOffset = $('.fixed-element').offset().top;
// Position of footer element from top of the document.
// You can add extra distance from the bottom if needed,
// must match with the bottom property in CSS
var footerOffset = $('#footer').offset().top - 36;
var fixedElementHeight = $('.fixed-element').height();
// Check every time the user scrolls
$(window).scroll(function (event) {
// Y position of the vertical scrollbar
var y = $(this).scrollTop();
if ( y >= fixedElementOffset && ( y + fixedElementHeight ) < footerOffset ) {
$('.fixed-element').addClass('fixed');
$('.fixed-element').removeClass('bottom');
}
else if ( y >= fixedElementOffset && ( y + fixedElementHeight ) >= footerOffset ) {
$('.fixed-element').removeClass('fixed');
$('.fixed-element').addClass('bottom');
}
else {
$('.fixed-element').removeClass('fixed bottom');
}
});

- 41
- 1
Add some extra margin-bottom
to your footer so the user can scroll past the bottom a bit so your footer can clear the fixed element.

- 112,730
- 33
- 157
- 176