Home > other >  CSS - fixed header gets covered by other divs when scrolling
CSS - fixed header gets covered by other divs when scrolling

Time:05-09

I'm still learning and completely out of ideas what could be causing this. The header has a fixed position but when I'm scrolling the page the header is not on top and is being covered by some divs.

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  href="#center">FEATURES</a></li>
     <li><a  href="#quotes">QUOTES</a></li>
   </ul>
  </nav>
  </div>
  <div id="center">
    <div id="center-pics">
      <div  id="pic1"><p>High quality materials and products</p></div>
      <div  id="pic2"><p>Tailored for your pet needs</p></div>
      <div  id="pic3"><p>Pet, human and eco-friendly</p></div>
    </div>
  </div>

Could anyone helop with this please? Thanks!

CodePudding user response:

You should give higher z-index than other div elements to make some element is one top of other element.

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 */
}

CodePudding user response:

position:absolute

Will most likely fix the IMG overlay

CodePudding user response:

The answer to your question is really simple. All you need to do is add the z-index: 100; style property to your header to prevent it from going underneath other elements.

#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;
}
  • Related