Search code examples
csscss-positionz-index

CSS: Make element's z-index lower than a sibling's child


The issue is the header: I need it to seem to be fixed on the fixed section and still be clickable. When the first section scrolls over the header, it should cover the header.

I can move or change the header however I want:

<a href="#" class="header">
  THIS IS THE HEADER 
  <span style="font-size: 12px;">(you can hover me)</span><br>
  <span style="font-size: 14px; font-style: italic; letter-spacing: 0.05em;">which should be <br> OVER "invisible section" AND "fixed section" <br> BUT UNDER "first section", <br>WHILE maintaining fixed position</span>
</a>

and while I could add the header to the invisible section, I cannot change the overall structure of the sections:

<div class="section section-fixed">
  fixed section underneath "invisible section"
</div>
<div class="wrapper">
  <div class="section section-invisible">
    <h2>invisible section</h2>
  </div>
  <div class="section section-first">
    first section
  </div>
</div>

I tried adding the header to the invisible section and while this works in keeping the header clickable, I cannot make it seem to be fixed the same as the fixed section.

I realise describing it might be confusing so here are some accompanying images and please check the FIDDLE

@import url("https://fonts.googleapis.com/css2?family=Baloo+Da+2&display=swap");
html {
  box-sizing: border-box;
}

*, *:before, *:after {
  box-sizing: inherit;
}

html, body {
  margin: 0;
  padding: 0;
}

body {
  font-size: 16px;
  font-family: 'Baloo Da 2', cursive;
}

.header {
  background: floralwhite;
  display: inline-block;
  padding: 10px;
  position: fixed;
  z-index: 3;
  left: 30px;
  top: 30px;
  color: inherit;
  text-decoration: none;
  line-height: 1.2;
  box-shadow: 0 0 0 rgba(0, 0, 0, 0.5);
  transition: all .5s ease;
}
.header:hover {
  box-shadow: 2px 3px 10px rgba(0, 0, 0, 0.5);
  background-color: pink;
}

.wrapper {
  border: 2px solid red;
  z-index: 2;
  position: relative;
}

.section {
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
.section-fixed {
  background: gray;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  z-index: 1;
}
.section-invisible {
  align-items: flex-end;
  padding: 0;
}
.section-invisible h2 {
  background: goldenrod;
  padding: 50px;
  margin: 0;
  width: 100%;
  text-align: center;
}
.section-first {
  background: darkslateblue;
  color: white;
}
<a href="#" class="header">
  THIS IS THE HEADER 
  <span style="font-size: 12px;">(you can hover me)</span><br>
  <span style="font-size: 14px; font-style: italic; letter-spacing: 0.05em;">which should be <br> OVER "invisible section" AND "fixed section" <br> BUT UNDER "first section", <br>WHILE maintaining fixed position</span>
</a>
<div class="section section-fixed">
  fixed section underneath "invisible section"
</div>
<div class="wrapper">
  <div class="section section-invisible">
    <h2>invisible section</h2>
  </div>
  <div class="section section-first">
    first section
  </div>
</div>

initial: enter image description here

after scroll:

enter image description here


Solution

  • Update your z-index like below. The most important is to not set any z-index to wrapper:

    @import url("https://fonts.googleapis.com/css2?family=Baloo+Da+2&display=swap");
    html {
      box-sizing: border-box;
    }
    
    *,
    *:before,
    *:after {
      box-sizing: inherit;
    }
    
    html,
    body {
      margin: 0;
      padding: 0;
    }
    
    body {
      font-size: 16px;
      font-family: 'Baloo Da 2', cursive;
    }
    
    .header {
      background: floralwhite;
      padding: 10px;
      position: fixed; 
      z-index: 2; /* here */
      left: 30px;
      top: 30px;
      color: inherit;
      text-decoration: none;
      line-height: 1.2;
      box-shadow: 0 0 0 rgba(0, 0, 0, 0.5);
      transition: all .5s ease;
    }
    
    .header:hover {
      box-shadow: 2px 3px 10px rgba(0, 0, 0, 0.5);
      background-color: pink;
    }
    
    .wrapper {
      border: 2px solid red;
      position: relative;
    }
    
    .section {
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100vh;
    }
    
    .section-fixed {
      background: gray;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      z-index: 1; /* here */
    }
    
    .section-invisible {
      align-items: flex-end;
      padding: 0;
      position: relative;
      z-index: 1; /* here */
    }
    
    .section-invisible h2 {
      background: goldenrod;
      padding: 50px;
      margin: 0;
      width: 100%;
      text-align: center;
    }
    
    .section-first {
      background: darkslateblue;
      color: white;
      position: relative;
      z-index: 2; /* here */
    }
    <a href="#" class="header">
      THIS IS THE HEADER 
      <span style="font-size: 12px;">(you can hover me)</span><br>
      <span style="font-size: 14px; font-style: italic; letter-spacing: 0.05em;">which should be <br> OVER "invisible section" AND "fixed section" <br> BUT UNDER "first section", <br>WHILE maintaining fixed position</span>
    </a>
    <div class="section section-fixed">
      fixed section underneath "invisible section"
    </div>
    <div class="wrapper">
      <div class="section section-invisible">
        <h2>invisible section</h2>
      </div>
      <div class="section section-first">
        first section
      </div>
    </div>

    Related: Why can't an element with a z-index value cover its child?