0

In the below HTML and CSS I created a header and an image-animation which you can also find in the JSfiddle here:

body {
  margin: 0;
}


/* 01.00 HEADER: Items in header */

.header_01 {
  width: 80%;
  height: 10vh;
  
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  z-index:99;
  
  
  text-align: center;
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: orange;    
}

.header_02 {
  width: 80%;
  height: 10vh;
  
  margin: 10vh auto 0;
  position: sticky;
  z-index:99;
  
  top:0;
  display: flex;
  justify-content: space-between;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}

.navigation {
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}


/* 02.00 NAVIGATION */

.navigation>ul {
  height: 100%;
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: blue;
}

.navigation>ul>li {
  width: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
}



/* 03.00 CONTENT */

.image_animation {
 width: 80%;
 margin-left: 10%;
 margin-top: 15%;
 float: left;
 display: flex;
 justify-content: space-between;

 background-color: green;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list {
 width: 100%;
 position: relative;
 
 background-color: red;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list img {
 width: 100%;
 height: 100%;
}
 
.image1 {
 height: 100%;
 width: 100%;
 float: left;
 position: absolute;
}
 
.image2 {
 height: 100%;
 width: 100%;
 float: left;
 animation-delay: 2s;
}
 
.image_list div {
 animation-name: animation_home_images;
 animation-duration:4s;
 animation-iteration-count:infinite;
 animation-fill-mode: forwards;
 opacity:0;
 }

@keyframes animation_home_images {
  50.0% {
    opacity: 1
  }
  0%, 100%{
    opacity: 0
  }
}
<div class="header_01">
This is our webpage.
</div>


<div class="header_02"> 

      <div class="image">
      Image
      </div>
  
      <nav class="navigation"> 
      
        <ul>
        
          <li class="button_01"> 1.0 Main Menu </li>
          <li class="button_01"> 2.0 Main Menu </li>
          <li class="button_01"> 3.0 Main Menu </li>
          
        </ul>
        
      </nav>
      
</div> 


<div class="image_animation">
    
  <div class="image_list">
    <div class="image1"><img src="http://placehold.it/101x101"></div>
  <div class="image2"><img src="http://placehold.it/201x201"></div>
 </div>
        
</div>

As you can see I have a header consisting of two parts. The first .header_01 should disappear once the user scrolls down the page whereas the second .header_02 should remain fixed. I originally achieved this with the answer from the question here.

All this worked fine so far.


Now I added an .image-animation below the header with a postion: absolute; property which is neccesary to make the animation work. Therefore, I also added a z-index to my CSS as described in the answers here to get the animation below the header once the user scrolls down the page.

However, somehow I cannot make the z-index work in combination with the position: sticky; property because when I scroll down both headers disappear.

Do you have any idea what I need to change in my code so once the user scrolls down:

a) the first .header_01 disappears and
b) the second .header_02 remains fixed and
c) the .image-animation goes behind the header.

Michi
  • 4,663
  • 6
  • 33
  • 83

1 Answers1

0

Simply remove the float (it's not needed) that are making the body having as height only the top header thus the sticky will not work as expected:

body {
  margin: 0;
}


/* 01.00 HEADER: Items in header */

.header_01 {
  width: 80%;
  height: 10vh;
  
  position: absolute;
  margin: auto;
  top: 0;
  left: 0;
  right: 0;
  z-index:99;
  
  
  text-align: center;
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: orange;    
}

.header_02 {
  width: 80%;
  height: 10vh;
  
  margin: 10vh auto 0;
  position: sticky;
  z-index:99;
  
  top:0;
  display: flex;
  justify-content: space-between;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: yellow;
}

.image {
  width: 30%;
  height: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: green;
}

.navigation {
  width: 70%;
  height: 100%;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: aqua;
}


/* 02.00 NAVIGATION */

.navigation>ul {
  height: 100%;
  display: flex;
  list-style: none;
  margin: 0;
  padding: 0;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
  background-color: blue;
}

.navigation>ul>li {
  width: 100%;
  display: flex;
  justify-content: center;
  text-align: center;
  align-items: center;
  
  box-sizing: border-box;
  border-style: solid;
  border-width: 1px;
}



/* 03.00 CONTENT */

.image_animation {
 width: 80%;
 margin-left: 10%;
 margin-top: 15%;
 display: flex;
 justify-content: space-between;

 background-color: green;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list {
 width: 100%;
 position: relative;
 overflow:hidden;
 
 background-color: red;
 box-sizing: border-box;
 border-style: solid;
 border-width: 1px;
}
 
.image_list img {
 width: 100%;
 height: 100%;
}
 
.image1 {
 height: 100%;
 width: 100%;
 position: absolute;
}
 
.image2 {
 height: 100%;
 width: 100%;
 display:block;
 animation-delay: 2s;
}
 
.image_list div {
 animation-name: animation_home_images;
 animation-duration:4s;
 animation-iteration-count:infinite;
 animation-fill-mode: forwards;
 opacity:0;
 }

@keyframes animation_home_images {
  50.0% {
    opacity: 1
  }
  0%, 100%{
    opacity: 0
  }
}
<div class="header_01">
This is our webpage.
</div>


<div class="header_02"> 

      <div class="image">
      Image
      </div>
  
      <nav class="navigation"> 
      
        <ul>
        
          <li class="button_01"> 1.0 Main Menu </li>
          <li class="button_01"> 2.0 Main Menu </li>
          <li class="button_01"> 3.0 Main Menu </li>
          
        </ul>
        
      </nav>
      
</div> 


<div class="image_animation">
    
  <div class="image_list">
    <div class="image1"><img src="http://placehold.it/101x101"></div>
  <div class="image2"><img src="http://placehold.it/201x201"></div>
 </div>
        
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • Thanks man. Exactly what I needed. I also updated it here http://jsfiddle.net/zwujpL5o/ with panel as menu. – Michi Jan 24 '19 at 12:52