【问题标题】:How to stick div below other sticky div when div has dynamic height content?当 div 具有动态高度内容时,如何将 div 粘贴在其他粘性 div 下方?
【发布时间】:2019-07-22 10:51:52
【问题描述】:

我在顶部有可滚动文本,它根据下面的内容具有动态高度,该文本具有粘性标题,其下方也具有动态高度,我正在尝试添加另一个粘性标题,但是对于第二个标题,我不确定我应该在多少顶部给?有什么技巧可以在运行时计算位置还是有其他方法?

.container {
  height: 200px;
  overflow: scroll;
}

.content {
  background-color: gray;
}

.header1 {
  position: sticky;
  top: 0px;/* or whatever you wish to see it sticks */
  background-color: red;
}
.header2 {
  position: sticky;
  top: 0px;/* how to stick thi div below header1 */
  background-color: blue;
}

.footer {
  /*top: 10px;*//* useless if position is not reset to fixed,absolute or sticky */
  background-color: yellow;
  height: 400px;
}
<div class="container">
  <div class="content"> runtime example text<br> runtime example text<br> runtime example text </div>
  <div class="header1"> header text runtime dynamic text </div>
  <div class="header2"> header text </div>
  <div class="footer"> footer text </div>
</div>

【问题讨论】:

  • @Junior 它不重复,给定示例具有固定高度,但我正在尝试具有动态高度的 div。请删除重复标记,以便其他人查看
  • 我的 div 也没有相互嵌套

标签: html css


【解决方案1】:

标题肯定是 &lt;h1&gt;&lt;h2&gt; 并且是 div 的子级。这应该可以解决您的问题。

.container {
  height: 200px;
  overflow: scroll;
}

.content {
  background-color: gray;
}

.header1 h1 {
  position: sticky;
  top: 0px;/* or whatever you wish to see it sticks */
  background-color: red;
}
.header2 h1 {
  position: sticky;
  top: 0px;/* how to stick thi div below header1 */
  background-color: blue;
}

.footer {
  /*top: 10px;*//* useless if position is not reset to fixed,absolute or sticky */
  background-color: yellow;
  height: 400px;
}
<div class="container">
  <div class="content"> runtime example text<br> runtime example text<br> runtime example text </div>
  <div class="header1">  <h1>header text runtime dynamic text</h1> </div>
  <div class="header2"> <h1>header text</h1> </div>
  <div class="footer"> footer text </div>
</div>

【讨论】:

  • 感谢您的帮助,但您的解决方案对我不起作用,因为如果您看到结果标题不粘,我正在寻找每个下方的粘性标题
猜你喜欢
  • 2014-07-23
  • 2017-11-29
  • 2014-02-13
  • 1970-01-01
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
相关资源
最近更新 更多