Search code examples
htmlcssz-index

Problems with Z-index problems with Hero? Maybe overflow related?


Here's a codepen of my code, since Stack Overflow doesn't yet allow SCSS: https://codepen.io/thedonquixotic/pen/dWXOby

I am trying to make a section that contains a hero image and an h1.

The containing section has a background gradient. It's z-index is set to 1.

The containing section contains an img and an h1. The img z-index is set to -1 and has in the past properly rendered behind the gradient background.

The h1 has a z-index of 2 and should render above both the image and the gradient.

I had this working but then I added in a JS parallax script. I got it working. The h1 etc was still visible but acting kind of weird so I commented out the JS import to focus on making sure I had my flex box stuff set correctly. Now it's not working at all though!

body div section.hero {
  display: flex !important;
  width: 100vw;
  height: 81vh;
  background-repeat: no-repeat;
  background-position: center;
  background: linear-gradient(to left, rgba(255, 195, 113, 0.5), rgba(255, 95, 109, 0.5));
  position: relative;
  z-index: 1;
}
body div section.hero div img {
  display: flex !important;
  width: 100vw;
  height: 81vh;
  z-index: -1 !important;
  flex: 1 1 auto;
}
body div section.hero div.svg-flex {
  position: relative;
  display: flex !important;
  padding: 30px;
  flex-direction: column;
  justify-content: center;
  align-content: center;
  align-items: center;
  z-index: 2;
}
body div section.hero div.svg-flex .hero-text {
  width: 95vw;
  top: 30vh;
  stroke-miterlimit: 10;
  display: flex;
  position: relative;
  overflow: hidden;
  stroke-linejoin: miter;
  font-size: 6rem;
  fill: none;
  stroke: white;
  stroke-width: 2.2px;
}
body div section.hero div.svg-flex h1 {
  position: relative;
  font-family: 'Aller', Arial;
  color: white;
  text-align: center;
  font-size: 3rem;
  line-height: 3rem;
  text-shadow: 0 2px 2px rgba(0, 0, 0, 0.35);
  max-width: 70rem;
  margin: 2vh auto 0;
  display: flex !important;
  flex-direction: column;
  justify-content: center;
  align-content: center;
  align-items: center;
  z-index: 2;
}
<section class="hero scene" id="scene" style="display: flex;">
    <div class="layer" style="display: flex;" data-depth="0.40"><img src="https://s-media-cache-ak0.pinimg.com/originals/32/93/21/329321959fcaf0d24e2349ccf8c9407a.jpg">
    </div>



<div class="svg-flex layer" data-depth="0.80" style="display: flex;">
        <h1 class="class-study-title" style="display: flex;"><h1> The Title</h1>
</div>

</section>


Solution

  • Add these stlyes to h1

    h1 {
            position: absolute;
            top: 0px;