How to keep div on screen (at the bottom or top) using jQuery.

This can be achieved with basic CSS but if this fails there is some jQuery/JavaScript you can use to
potentially fix the problem of keeping a div on the bottom of the screen even when scrolling.

.bottom { position:fixed; position:absolute; right:0; bottom:0; padding:0; margin:0; }

/*IE FIX*/
.fixie {
  left: expression( ( - jsrp_related.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
  top: expression( ( - jsrp_related.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
//note you MUST set a height property for it to work correctly
if ( $.browser.msie ) {
	div.css({position: "absolute", width: jslide_width, right: 0, height: 100});
} else {
	div.css({position: "fixed", width: jslide_width, right: 0, height: 100});

See Demo

Further Techniques

There is also a jQuery plugin which can keep any element on the sidebar in view.

Tags: css fixed floating div position, jquery fixed position floating div, jquery keep div bottom, jquery keep div on screen, jquery sticky footer
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

  • Paolo

    thanks, works great on ie6 too.

  • Darin

    Nice. If you have a really tall window that requires little to no scrolling, it doesn’t show up.

Special Offer
Free course!

Git into it! Bonus course Introduction to Git is yours when you take up a free 14 day SitePoint Premium trial.