css 网格项目上的粘性位置

新手上路,请多包涵

我在这里查看了这方面的其他示例,但找不到使这项工作可行的示例。我希望边栏(部分)在页面滚动时保持粘性。 position: sticky 如果我把它放在 nav 上就可以工作,所以我的浏览器 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;
}
 <main>
  <nav></nav>
  <section>
    hi
  </section>
  <article>
    <p>hi</p>
  </article>
</main>

原文由 totalnoob 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 248
1 个回答

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

原文由 honk31 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题