Apply position: sticky to child of a div

后端 未结 4 1846
北荒
北荒 2020-12-16 12:48

Position: sticky doesn\'t seem to work for me when I apply it to a child of a div. How to solve?

HTML:

Lorem Ipsum
相关标签:
4条回答
  • 2020-12-16 13:24

    Your sticky element is working as intended, you can't see it because your container div is as short as the sticky element itself, so as soon as it sticks, the parent container is already scrolled out of view.

    If you add the br tags inside of the parent div then you can see it stick. Once you scroll past the parent then it will scroll with the parent and will not be visible anymore as you can see from your original fiddle. Example on this page https://developer.mozilla.org/en/docs/Web/CSS/position

    If you are trying to dock this for the entire page then you just need to place the sticky element under a higher level div, for example a div that contains all page content. Just remember when it sticks it only sticks within the parent container

    https://jsfiddle.net/n8Le2tva/3/

    HTML

    <div class="this-parent-div-is-necessary">
      <div class="div-sticky-class">
          Test
      </div>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/>
    </div>
    

    This example here https://jsfiddle.net/n8Le2tva/10/ I moved the sticky element out so it sticks to the overall viewport

    0 讨论(0)
  • 2020-12-16 13:33

    Well it is actually working .But the div with class div-sticky-class is inside another div (with class this-parent-div-is-necessary ) that moves. Well if you want it to work you can give the outer div the same class e.g

    Lorem Ipsum
    <div class="this-parent-div-is-necessary div-sticky-class">
      <div class="div-sticky-class">
          Test
      </div>
    </div>
    Lorem Ipsum
    
    0 讨论(0)
  • 2020-12-16 13:43

    Put everything inside your this-parent-div-is-necessary container.

    .div-sticky-class {
      color: red;
      position: sticky;
      position: -webkit-sticky;
      top: 0;
    }
    <div class="this-parent-div-is-necessary">
      Lorem Ipsum
      <div class="div-sticky-class">
        Test
      </div>
      Lorem Ipsum
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    </div>

    0 讨论(0)
  • 2020-12-16 13:48

    Sticky position only works inside the parent div, your html should be like this:

    Lorem Ipsum
    <div class="this-parent-div-is-necessary">
       <div class="div-sticky-class">
         Test
       </div>
       <div>Lorem ...</div>
       <div>Lorem ...</div>
       <div>Lorem ...</div>
       <div>Lorem ...</div>
       <div>
         <p>It will works here too</p>
       </div>
    </div>
    Won't work here because it's outside the parent of div-sticky-class
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    

    https://jsfiddle.net/n8Le2tva/7/

    0 讨论(0)
提交回复
热议问题