如何使一个元素粘性,但可以滚动到其充分(可变)高度与同级元素?

huangapple go评论53阅读模式
英文:

How to make an element sticky but scrollable to its full (variable) height with sibling element?

问题

我正在尝试复制类似Twitter上看到的基本网格布局。到目前为止,一切似乎都运行正常,但我无法使右侧边栏在滚动到其完整可变高度后停止滚动。

在下面的图像中,不应出现白色空白,一旦元素滚动到其完整高度,它应该固定。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 3fr;
  min-height: 100vh;
  font-size: 2rem;
}

.inner-grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-sidebar,
.right-sidebar,
.main {
  margin: 0 1rem;
}

.left-sidebar,
.content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content {
  height: 95%;
  margin-top: 2.5rem;
}

.left-sidebar {
  background-color: burlywood;
  height: 100vh;
  position: sticky;
  top: 0;
}

.main {
  background-color: tomato;
  height: 1500px;
}

.right-sidebar {
  background-color: cadetblue;
  height: 900px;
}

.fixed-header {
  background-color: black;
  color: white;
  position: fixed;
  width: auto;
  top: 0;
}
<div class="container">
  <div class="left-sidebar">
    <div>
      <div>左侧边栏</div>
      <div>顶部</div>
    </div>
    <div>底部</div>
  </div>
  <div class="inner-grid-container">
    <div class="main">
      <div class="fixed-header">固定标题</div>
      <div class="content">
        <div>
          <div>主要内容</div>
          <div>顶部</div>
        </div>
        <div>底部</div>
      </div>
    </div>
    <div class="right-sidebar">
      <div class="fixed-header">固定标题</div>
      <div class="content">
        <div>
          <div>右侧边栏</div>
          <div>顶部</div>
        </div>
        <div>底部</div>
      </div>
    </div>
  </div>
</div>
英文:

I am trying to replicate the basic grid layout such as which is seen on Twitter. It seems to be working fine so far but I can't get the right sidebar to stop scrolling once it has scrolled to its full variable height.

In the image below the white space should not appear and once the element has scrolled to its full height it should stick.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 3fr;
  min-height: 100vh;
  font-size: 2rem;
}

.inner-grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-sidebar,
.right-sidebar,
.main {
  margin: 0 1rem;
}

.left-sidebar,
.content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content {
  height: 95%;
  margin-top: 2.5rem;
}

.left-sidebar {
  background-color: burlywood;
  height: 100vh;
  position: sticky;
  top: 0;
}

.main {
  background-color: tomato;
  height: 1500px;
}

.right-sidebar {
  background-color: cadetblue;
  height: 900px;
}

.fixed-header {
  background-color: black;
  color: white;
  position: fixed;
  width: auto;
  top: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;left-sidebar&quot;&gt;
    &lt;div&gt;
      &lt;div&gt;left sidebar&lt;/div&gt;
      &lt;div&gt;top&lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;bottom&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;inner-grid-container&quot;&gt;
    &lt;div class=&quot;main&quot;&gt;
      &lt;div class=&quot;fixed-header&quot;&gt;fixed header&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;
        &lt;div&gt;
          &lt;div&gt;main&lt;/div&gt;
          &lt;div&gt;top&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;bottom&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;right-sidebar&quot;&gt;
      &lt;div class=&quot;fixed-header&quot;&gt;fixed header&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;
        &lt;div&gt;
          &lt;div&gt;right sideabar&lt;/div&gt;
          &lt;div&gt;top&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;bottom&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

如何使一个元素粘性,但可以滚动到其充分(可变)高度与同级元素?

答案1

得分: 2

position: sticky;
bottom: 0;
margin-top: auto;

添加到右侧边栏。

英文:

Add

position: sticky;
bottom: 0;
margin-top: auto;

to the right sidebar

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 3fr;
  min-height: 100vh;
  font-size: 2rem;
}

.inner-grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.left-sidebar,
.right-sidebar,
.main {
  margin: 0 1rem;
}

.left-sidebar,
.content {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.content {
  height: 95%;
  margin-top: 2.5rem;
}

.left-sidebar {
  background-color: burlywood;
  height: 100vh;
  position: sticky;
  top: 0;
}

.main {
  background-color: tomato;
  height: 1500px;
}

.right-sidebar {
  background-color: cadetblue;
  height: 900px;
  position: sticky;
  bottom: 0;
  margin-top: auto;
}

.fixed-header {
  background-color: black;
  color: white;
  position: fixed;
  width: auto;
  top: 0;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;left-sidebar&quot;&gt;
    &lt;div&gt;
      &lt;div&gt;left sidebar&lt;/div&gt;
      &lt;div&gt;top&lt;/div&gt;
    &lt;/div&gt;
    &lt;div&gt;bottom&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class=&quot;inner-grid-container&quot;&gt;
    &lt;div class=&quot;main&quot;&gt;
      &lt;div class=&quot;fixed-header&quot;&gt;fixed header&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;
        &lt;div&gt;
          &lt;div&gt;main&lt;/div&gt;
          &lt;div&gt;top&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;bottom&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;right-sidebar&quot;&gt;
      &lt;div class=&quot;fixed-header&quot;&gt;fixed header&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;
        &lt;div&gt;
          &lt;div&gt;right sideabar&lt;/div&gt;
          &lt;div&gt;top&lt;/div&gt;
        &lt;/div&gt;
        &lt;div&gt;bottom&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 17:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定