html - 我如何保持位置 : relative content from overlapping my position: sticky header?

标签 html css css-position overlapping

我有一个粘性顶部导航栏,我希望它在滚动时保持可见并位于所有其他内容之上。我在页面上设置了 position: relative 的内容,这样我就可以在它周围放置其他元素。当我这样做时,相关内容在滚动时会忽略导航栏并与它重叠。有什么方法可以让我的页面内容相对定位并仍然观察粘性导航栏吗?

我试过给相关内容一个等于导航栏高度的上边距。

.nav-bar {
  position: sticky;
  top: 0px;
  font-family: Arial, Helvetica, sans-serif;
  border-bottom: solid rgb(179, 173, 173) 1px;
  background-color: rgb(255, 255, 255);
}

.nav-bar #title {
  margin: 0;
  font-size: 2em;
  padding-left: 2%;
}

.test-class #test-content {
  width: 500px;
  height: 500px;
  background-color: rgb(70, 67, 67);
  position: absolute;
}
<div class="nav-bar">
  <p id="title">Title</p>
</div>
<div class="test-class">
  <p id="test-content"></p>
</div>

预期:粘性标题保持在所有其他内容之上。

实际:当其位置设置为相对时,内容与标题重叠。

最佳答案

如果你想让你的导航栏始终可见,只需给它一个比你的内容大的 z-index

.nav-bar{
    position:sticky;
    top:0px;
    font-family: Arial, Helvetica, sans-serif;
    border-bottom:solid rgb(179, 173, 173) 1px;
    background-color: rgb(255, 255, 255);
    z-index: 1
}

关于html - 我如何保持位置 : relative content from overlapping my position: sticky header?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893642/

相关文章:

javascript - 当 Jquery 参数设置为 5 时,Slider 中的幻灯片没有响应

javascript - 删除一行 Bootstrap Table

javascript - 如何在 React 中导入特定于组件的 css 样式?

html - Divs 中的 Div 滚动不会滚动

css - INPUT 提交的按下状态是否有 CSS 类?

android - 将元素(尤其是右上角)固定到 chrome mobile 中的视觉视口(viewport),内容非常广泛?

javascript - 带有列表组的 Bootstrap 4 scrollspy 不起作用

javascript - 无法在 div 中定位图像

jquery - div 在 Internet Explorer 中的位置

jquery - 让div出现在正确的位置