CSS - 修复了 header 在滚动时被其他 div 覆盖的问题
CSS - fixed header gets covered by other divs when scrolling
我还在学习,完全不知道是什么原因造成的。
header 有一个固定的位置,但是当我滚动页面时,header 不在顶部并且被一些 div 覆盖。
https://codepen.io/ann9tro/pen/vYpMovj
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
}
#center {
width: 70vw;
display: flex;
flex-direction: column;
text-align: center;
border: solid 2px #f48d26;
border-radius: 5px;
margin: 165px auto 0;
font-family: Orator Std;
background-color: white;
}
#center-pics {
display: flex;
justify-content: space-between;
margin: 40px 100px 20px ;
}
.picture {
width: 16vw;
height: 12vw;
color: black;
opacity: 0.85;
border: solid 1px grey;
border-radius: 5px;
margin: 30px 0 0 0;
}
<div id="header">
<img id="header-img" src="https://i.postimg.cc/q7PLWG9j/banner-bez-tla.jpg" alt="company banner with pet faces">
<nav>
<ul id="nav-bar">
<li><a class="nav-link" href="#center">FEATURES</a></li>
<li><a class="nav-link" href="#quotes">QUOTES</a></li>
</ul>
</nav>
</div>
<div id="center">
<div id="center-pics">
<div class="picture" id="pic1"><p>High quality materials and products</p></div>
<div class="picture" id="pic2"><p>Tailored for your pet needs</p></div>
<div class="picture" id="pic3"><p>Pet, human and eco-friendly</p></div>
</div>
</div>
有人可以帮忙吗?
谢谢!
position:absolute
很可能会修复 IMG 覆盖
你的问题的答案很简单。您需要做的就是将 z-index: 100;
样式 属性 添加到您的 header 以防止它位于其他元素之下。
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
z-index: 100;
}
您应该提供比其他 div 元素更高的 z-index 以使某些元素位于其他元素之上。
https://codepen.io/joonahn/pen/BaYjqvK
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
z-index:1000; /* this */
}
我还在学习,完全不知道是什么原因造成的。 header 有一个固定的位置,但是当我滚动页面时,header 不在顶部并且被一些 div 覆盖。
https://codepen.io/ann9tro/pen/vYpMovj
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
}
#center {
width: 70vw;
display: flex;
flex-direction: column;
text-align: center;
border: solid 2px #f48d26;
border-radius: 5px;
margin: 165px auto 0;
font-family: Orator Std;
background-color: white;
}
#center-pics {
display: flex;
justify-content: space-between;
margin: 40px 100px 20px ;
}
.picture {
width: 16vw;
height: 12vw;
color: black;
opacity: 0.85;
border: solid 1px grey;
border-radius: 5px;
margin: 30px 0 0 0;
}
<div id="header">
<img id="header-img" src="https://i.postimg.cc/q7PLWG9j/banner-bez-tla.jpg" alt="company banner with pet faces">
<nav>
<ul id="nav-bar">
<li><a class="nav-link" href="#center">FEATURES</a></li>
<li><a class="nav-link" href="#quotes">QUOTES</a></li>
</ul>
</nav>
</div>
<div id="center">
<div id="center-pics">
<div class="picture" id="pic1"><p>High quality materials and products</p></div>
<div class="picture" id="pic2"><p>Tailored for your pet needs</p></div>
<div class="picture" id="pic3"><p>Pet, human and eco-friendly</p></div>
</div>
</div>
有人可以帮忙吗? 谢谢!
position:absolute
很可能会修复 IMG 覆盖
你的问题的答案很简单。您需要做的就是将 z-index: 100;
样式 属性 添加到您的 header 以防止它位于其他元素之下。
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
z-index: 100;
}
您应该提供比其他 div 元素更高的 z-index 以使某些元素位于其他元素之上。
https://codepen.io/joonahn/pen/BaYjqvK
#header {
display: flex;
width: 70vw;
height: 150px;
position: fixed;
left: 50%;
transform: translate(-50%, 0);
border: solid 2px #f48d26;
border-radius: 5px;
margin: auto;
justify-content: space-around;
background-color: white;
z-index:1000; /* this */
}