Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu January 21, 2023 › #css #Css Tutorial

Position: sticky not working [SOLVED]

Sometimes, when we try to use position property with its value sticky, it may not work. This can happen for many reasons.

So, in this article, I will discuss how to solve the problem - Position: sticky not working.

Why is my position: sticky not working? #

There are many reasons why this problem occurs:

  1. Position Sticky will likely not work if any of the element's parents have overflow hidden, scroll, or set to auto.
  2. Position Sticky may not work if a parent element has a fixed height.
  3. Many browsers do not yet support sticky positioning.
Internet Explorer does not support sticky positioning. You must also specify at least one of the top, right, bottom, or left for sticky positioning to work.

Position: sticky not working inside div element. #

In the first example, we'll add the position: sticky property to a div element inside another div element.

<div class="parent"> 
  <div class="child"> 
     Sticky
  </div>
</div>
<style> 
  .child{
     height:200px;
     position:sticky;
     top:0;
  }
</style>
Click on the Try it Yourself » button to open the code in our online code editor.

position: sticky not working inside flex. #

In the second example, we have a flex element and three child elements inside it. We're going to add position: sticky to one of them.

<div class="parent"> 
  <div class="child">Child</div>
  <div class="child sticky">Sticky</div>
  <div class="child">Child</div>
</div>
<style> 
  .parent{
     display:flex;
     align-items:center;
     justify-content:space-between;
  }
  .sticky{
     position:sticky;
     top:0;
  }
</style>

Position: sticky not working with grid. #

In the third example, we will create a grid layout and inside it, we will add three child elements. Then, we will add the position property with its value sticky to the second child.

<div class="parent"> 
  <div class="child">Child</div>
  <div class="child sticky">Sticky</div>
  <div class="child">Child</div>
</div>
<style> 
  .parent{
     display:grid;
     grid-template-columns:1fr 1fr 1fr;
     gap:5px;
  }
  .sticky{
     position:sticky;
     top:0;
  }
</style>

Browser compatibility. #

Many browsers do not yet support sticky positioning.

Chrome Edge SafariFirefox OperaIE
Yes YesYesYes Yes No
save
listen
AI Answer
2 Answers
Write Your Answer
  1. Position: sticky will not work if the parent element has overflow: hidden set. This is because the sticky element needs to have some area to stick to when scrolling, and overflow: hidden will hide any content that extends beyond the parent element's boundaries.
    If you have an element with position: sticky inside a div with overflow: hidden, you should either remove the overflow: hidden property or set a specific height on the parent element.
    Reply Delete
    Share
  2. https://www.3schools.in/p/code-editor.html?q=PGRpdiBzdHlsZT0ib3ZlcmZsb3c6aGlkZGVuOyBoZWlnaHQ6IDkwMHB4OyI+CiAgPGRpdiBzdHlsZT0icG9zaXRpb246IHN0aWNreTsgdG9wOiAwOyI+U3RpY2t5IEVsZW1lbnQ8L2Rpdj4KPC9kaXY+
    In this example, the sticky element will not work unless you remove the overflow property from the parent element.
    Reply Delete
    Share
loading
back
View All