我有一个固定在页面顶部的导航栏,希望在滚动时保持可见并在所有其他内容之上。页面上的某些内容我已经设置为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>
期望: 粘性标题应保持在所有其他内容之上。
实际: 当其位置设置为相对位置时,内容会重叠标题。
position: relative
。 - Quentinz-index
是你的好朋友。 - 0b10011