具有100%宽度重叠的固定div滚动条

sh0*_*ama 8 html css

如此处所示:http : //codepen.io/anon/pen/rVPqeL

我正在使用3个简单的div,我想获得必须遍历标题的“全局”滚动条的效果。

HTML是非常基本的

<div class="container">
    <div class="header">
    </div>
    <div class="content">
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

这是CSS:

.container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
  overflow-y: scroll;
}

.header {
  position: fixed;
  width: 100%;
  height: 50px;
  background-color: red;
}

.content {
  margin-top: 50px;
  min-height: 2500px;
  background-color: blue;
}
Run Code Online (Sandbox Code Playgroud)

滚动条继续标题div下移动。我究竟做错了什么?

小智 5

我尝试替换position:fixedposition:sticky并添加top:0,它对我来说效果很好,不再有重叠的垂直滚动条。

.header {
  position: sticky;
  top: 0;
  width: 100%;
  height: 50px;
  background-color: red;
}
Run Code Online (Sandbox Code Playgroud)


小智 1

如果我理解正确的话,你希望滚动条始终位于顶部。为此,请将 css 更改为以下内容

html{
    overflow-y: scroll;
}
.container {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: gray;
}
Run Code Online (Sandbox Code Playgroud)

在 html 上滚动将允许整个页面滚动,同时保持标题静态并从容器中删除滚动。