when css position sticky stops sticking

user1709076 picture user1709076 · Oct 9, 2017 · Viewed 14.7k times · Source

I was wondering why position: sticky works for some x-axis-scrolling, but once you scroll past the initial width of the screen width, your 'sticky div', stops sticking.

In this example, I have a left-side-bar that sticks to the left (please note that I cannot use position: fixed or position: absolute, because in my actual project both the left-div and the right-div need to scroll up and down along the y-axis, hence we only want left-side-sticking)

is there an additional CSS parameter I can define, such as

left-sticky-distance=999999%

or something like that?

some test code illustrating is below

    <html>


    <body>

    <div style='
    position:sticky;
    z-index:1;
    left:0;
    width:100px;
    height:200px;
    overflow: hidden;
    background-color:#ff0000;
    opacity:0.8;
    >

    </div>

      <div style='position:absolute; top:10;left:10; width:200; height:50px; background-color: blue'>B</div>
      <div style='position:absolute; top:10;left:110; width:200; height:50px; background-color: blue'>C</div>
      <div style='position:absolute; top:10;left:210; width:200; height:50px; background-color: blue'>D</div>
    </body>
    <html>

Answer

Anthony Bauer picture Anthony Bauer · Nov 17, 2017

This question: https://stackoverflow.com/a/45530506 answers the problem.

Once the "sticky div" reaches the edge of the screen, it is at the end of the viewport of the parent element. This causes the sticky element to stop and stay at the end of parent's viewport. This code pen provides an example: https://codepen.io/anon/pen/JOOBxg

#parent{
      width: 1000px;
      height: 1000px;
      background-color: red;
}
#child{
      width: 200px;
      height: 200px;
      background-color: blue;
      position: sticky;
      top: 0px;
      left: 0px;
}
body{  
      width: 3000px;
      height: 3000px;
}
<html>
  <div id="parent">
    <div id="child">
    </div>
  </div>
</html>