How can I make a div stick to the top of the screen once it’s been scrolled to?

How can I make a div stick to the top of the screen once it’s been scrolled to?

I would like to create a div, that is situated beneath a block of content but that once the page has been scrolled enough to contact its top boundary, becomes fixed in place and scrolls with the page.


Solution 1:

You could use simply css, positioning your element as fixed:

.fixedElement {
    background-color: #c0c0c0;

Edit: You should have the element with position absolute, once the scroll offset has reached the element, it should be changed to fixed, and the top position should be set to zero.

You can detect the top scroll offset of the document with the scrollTop function:

  var $el = $('.fixedElement'); 
  var isPositionFixed = ($el.css('position') == 'fixed');
  if ($(this).scrollTop() > 200 && !isPositionFixed){ 
    $el.css({'position': 'fixed', 'top': '0px'}); 
  if ($(this).scrollTop() < 200 && isPositionFixed){
    $el.css({'position': 'static', 'top': '0px'}); 

When the scroll offset reached 200, the element will stick to the top of the browser window, because is placed as fixed.

Solution 2:

You’ve seen this example on Google Code’s issue page and (only recently) on Stack Overflow’s edit page.

CMS’s answer doesn’t revert the positioning when you scroll back up. Here’s the shamelessly stolen code from Stack Overflow:

function moveScroller() {
    var $anchor = $("#scroller-anchor");
    var $scroller = $('#scroller');

    var move = function() {
        var st = $(window).scrollTop();
        var ot = $anchor.offset().top;
        if(st > ot) {
                position: "fixed",
                top: "0px"
        } else {
                position: "relative",
                top: ""
<div id="sidebar" style="width:270px;"> 
  <div id="scroller-anchor"></div> 
  <div id="scroller" style="margin-top:10px; width:270px"> 
    Scroller Scroller Scroller

<script type="text/javascript"> 
  $(function() {

And a simple live demo.

A nascent, script-free alternative is position: sticky, which is supported in Chrome, Firefox, and Safari. See the article on HTML5Rocks and demo, and Mozilla docs.

Solution 3:

As of January 2017 and the release of Chrome 56, most browsers in common use support the position: sticky property in CSS.

#thing_to_stick {
  position: sticky;
  top: 0px;

does the trick for me in Firefox and Chrome.

In Safari you still need to use position: -webkit-sticky.

Polyfills are available for Internet Explorer and Edge; seems to be a good one.

Solution 4:

I had the same problem as you and ended up making a jQuery plugin to take care of it. It actually solves all the problems people have listed here, plus it adds a couple of optional features too.


stickyPanelSettings = {
    // Use this to set the top margin of the detached panel.
    topPadding: 0,

    // This class is applied when the panel detaches.
    afterDetachCSSClass: "",

    // When set to true the space where the panel was is kept open.
    savePanelSpace: false,

    // Event fires when panel is detached
    // function(detachedPanel, panelSpacer){....}
    onDetached: null,

    // Event fires when panel is reattached
    // function(detachedPanel){....}
    onReAttached: null,

    // Set this using any valid jquery selector to 
    // set the parent of the sticky panel.
    // If set to null then the window object will be used.
    parentSelector: null


Solution 5:

And here’s how without jquery (UPDATE: see other answers where you can now do this with CSS only)

var startProductBarPos=-1;
  var bar = document.getElementById('nav');



function findPosY(obj) {
  var curtop = 0;
  if (typeof (obj.offsetParent) != 'undefined' && obj.offsetParent) {
    while (obj.offsetParent) {
      curtop += obj.offsetTop;
      obj = obj.offsetParent;
    curtop += obj.offsetTop;
  else if (obj.y)
    curtop += obj.y;
  return curtop;
* {margin:0;padding:0;}
.nav {
  border: 1px red dashed;
  background: #00ffff;
  padding: 21px 0;

  margin: 0 auto;

.header {
  padding: 65px 0;
  border: 1px red dashed;

.content {
  padding: 500px 0;
  border: 1px red dashed;
.footer {
  padding: 100px 0;
  background: #777;
  border: 1px red dashed;
<header class="header">This is a Header</header>
<div id="nav" class="nav">Main Navigation</div>
<div class="content">Hello World!</div>
<footer class="footer">This is a Footer</footer>

Solution 6:

This is how i did it with jquery. This was all cobbled together from various answers on stack overflow. This solution caches the selectors for faster performance and also solves the “jumping” issue when the sticky div becomes sticky.

Check it out on jsfiddle:


.stick {
    position: fixed;
    top: 0;


$(document).ready(function() {
    // Cache selectors for faster performance.
    var $window = $(window),
        $mainMenuBar = $('#mainMenuBar'),
        $mainMenuBarAnchor = $('#mainMenuBarAnchor');

    // Run this on scroll events.
    $window.scroll(function() {
        var window_top = $window.scrollTop();
        var div_top = $mainMenuBarAnchor.offset().top;
        if (window_top > div_top) {
            // Make the div sticky.
        else {
            // Unstick the div.