小编Sig*_*oss的帖子

CSS位置元素在滚动容器内"固定"

我想知道是否有人为此找到了解决方案?

我正在寻找一个解决方案,将元素附加到滚动容器的顶部

HTML:

<div class="container">
  <div class="header">title</div>
  <div class="element">......</div>
  ... (about 10-20 elements) ...
  <div class="element">......</div> 
</div>
Run Code Online (Sandbox Code Playgroud)

所有"元素"都有position:relative,

容器有以下CSS:

.container {
  position:relative;
  width:200px;
  height:400px;
  overflow-y:scroll;
}
Run Code Online (Sandbox Code Playgroud)

我希望标题保持在容器的顶部,独立于其滚动位置和滚动在下面的元素.

到目前为止的CSS:

.header {
  position:absolute; /* scrolling out of view :-( */
  z-index:2;
  background-color:#fff;
}
.element{
  position: relative;
}
Run Code Online (Sandbox Code Playgroud)

所有元素都是块元素,我不能将标题移动到容器之外.jquery在这一点上是没有选择的.

html css position

28
推荐指数
4
解决办法
5万
查看次数

标签 统计

css ×1

html ×1

position ×1