How to make a div sticky on scroll using CSS and JavaScript?

In this post, I’ll share different ways to make an element sticky on scroll.

How to make a div sticky on scroll using CSS and JavaScript?

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.

Make element sticky on scroll using JavaScript:

Use the below code:

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() {

A nascent, script-free alternative is position: sticky, which is supported in Chrome, Firefox, and Safari.

We use the scroll event of JavaScript or jQuery and put conditions according to the height of the element or the element which we want to keep the reference. For example, if we want to make the div sticky only until we find a specific element of xPath, we’ll handle that case as well. Go through all the answers here, you’ll definitely get what exactly you’ve been looking for.

How to make a div sticky on scroll without using jQuery?

An element can be made sticky using vanilla JavaScript. Use the code example below for reference:

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>

Answer #3:

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.


.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.

Answer #4:

Here’s one more version to try for those having issues with the others. It generates the required helper DIVs dynamically so no extra HTML is required.


.sticky { position:fixed; top:0; }


function make_sticky(id) {
    var e = $(id);
    var w = $(window);
    var n =;
    var p = e.prev();
    function sticky_relocate() {
      var window_top = w.scrollTop();
      var div_top = p.offset().top;
      if (window_top > div_top) {
      } else {

To make an element sticky, do:


When the element becomes sticky, the code manages the position of the remaining content to keep it from jumping into the gap left by the sticky element. It also returns the sticky element to its original non-sticky position when scrolling back above it.

In this post, we learned how to make an element sticky on scroll using CSS and JavaScript.

Hope you learned something from this post.

Follow Programming Articles for more!

About ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ

Linux and Python enthusiast, in love with open source since 2014, Writer at, India.

View all posts by ᴾᴿᴼᵍʳᵃᵐᵐᵉʳ →