sticky position on css grid items

后端 未结 1 991
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 23:43

I\'ve looked at other examples of this on here but can\'t find one that makes this work. I want the sidebar (section) to be sticky while the page scrolls. the position: stic

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-04 00:00

    the problem you are facing here is, that your section block consumes the full height. so it won't stick, since it is too large to do so. you would need to put a child element inside your section and give that your sticky attributes, to make it work. based on your example, i simply wrapped your 'hi' inside a div.

    main {
      display: grid;
      grid-template-columns: 20% 55% 25%;
      grid-template-rows: 55px 1fr;
    }
    
    nav {
      background: blue;
      grid-row: 1;
      grid-column: 1 / 4;
    }
    
    section {
      background: grey;
      grid-column: 1 / 2;
      grid-row: 2;
    }
    
    section div {
      position: sticky;
      top: 0;
    }
    
    article {
      background: yellow;
      grid-column: 2 / 4;
    }
    
    article p {
      padding-bottom: 1500px;
    }

    one

    two

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