css网格项上的粘性位置

tot*_*oob 13 html css css-position css3 css-grid

我在这里看了其他的例子,但找不到一个让这个工作.我希望在页面滚动时侧边栏(部分)是粘性的.位置:粘贴有效,如果我把它放在导航,所以我的浏览器def支持它.

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;
  position: sticky;
  top: 0;
  left: 0;
}

article {
  background: yellow;
  grid-column: 2 / 4;
}

article p {
  padding-bottom: 1500px;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <nav></nav>
  <section>
    hi
  </section>
  <article>
    <p>hi</p>
  </article>
</main>
Run Code Online (Sandbox Code Playgroud)

hon*_*k31 29

您在这里面临的问题是,您的部分块占用了整个高度。所以它不会粘住,因为它太大而不能这样做。您需要在您的部分中放置一个子元素并提供您的粘性属性,以使其工作。根据您的示例,我只是将您的 'hi' 包裹在一个 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;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <nav></nav>
  <section>
    <div>
      <p>one</p>
    </div>
  </section>
  <article>
    <p>two</p>
  </article>
</main>
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果这不起作用,请确保您的父元素没有溢出:隐藏。/sf/ask/3059495351/ (2认同)

小智 13

部分需要一个高度,以便粘性达到你想要的效果,并且因为有粘性的潜在怪癖(即当你到达页面底部时,部分会向上推过网格行1),我会在网格行开始部分1而不是2以及包装容器.

假设你的导航也是粘性的,我会设置它的z-index,使它位于该部分的顶部.

我还建议使用@support来使用其他人提到的固定解决方案.

      main {
        display: grid;
        grid-template-columns: 20% 55% 25%;
        grid-template-rows: 55px 1fr;
      }
      
      nav {
        background: blue;
        grid-row: 1;
        grid-column: 1 / 4;
        z-index: 2;
        position: sticky;
        top: 0;
      }
      
      section {
        background: grey;
        grid-column: 1 / 2;
        grid-row: 1;
        position: sticky;
        top: 0;
        left: 0;
        height: 100vh;
      }
      
      section #contents {
        position: relative;
        top: 55px;
      }
      
      article {
        background: yellow;
        grid-column: 2 / 4;
      }

      article p {
        padding-bottom: 1500px;
      }
Run Code Online (Sandbox Code Playgroud)
<main>
  <nav></nav>  
  <section>
    <div id="contents">
      contents
    </div>
  </section>
  <article>
    <p>article</p>
  </article>
</main>
Run Code Online (Sandbox Code Playgroud)


小智 11

你需要用align-self: start在你想成为的东西上sticky

main {
  display: grid;
  grid-template-columns: 20% 55% 25%;
  grid-template-rows: 55px 1fr;
  background: grey;
}

nav {
  background: blue;
  grid-row: 1;
  grid-column: 1 / 4;
}

section {
  background: grey;
  grid-column: 1 / 2;
  grid-row: 2;
  position: sticky;
  top: 0;
  left: 0;
  align-self: start;
  
}

article {
  background: yellow;
  grid-column: 2 / 4;
}

article p {
  padding-bottom: 1500px;
}
Run Code Online (Sandbox Code Playgroud)
<main>
  <nav></nav>
  <section>
    hi
  </section>
  <article>
    <p>hi</p>
  </article>
</main>
Run Code Online (Sandbox Code Playgroud)