1

I made a very basic hero section with a background and an overlay.

.hero
    width: 100%
    height: 100vh
    position: relative
    background-image: url('../assets/hero.png')
    background-position: center
    background-repeat: no-repeat
    background-size: cover

    &:before 
        content: ''
        position: absolute
        top: 0
        right: 0
        bottom: 0
        left: 0
        background: $secondary-blue
        opacity: .5
        overflow: hidden
        z-index: 1

.hero--welcome
    display: flex
    align-items: center
    justify-content: center
    flex-direction: column
    align-items: left
    width: 100%
    height: 100%
    z-index: 2
<div className="hero">
  <div className="hero--welcome">
    <h6>Welcome to</h6>
    <h1>Alhandsya</h1>
  </div>
</div>

But the text at hero--welcome is showing in a really dark way under the overlay and I want it to go over it, how do I do it? I tried z-index but didn't work.

Result (screenshot)

Screenshot

Yashu Mittal
  • 1,478
  • 3
  • 14
  • 32
Ahmed Maher
  • 45
  • 1
  • 8

1 Answers1

1

You are only adding z-index to the &:before, and didn't add z-index to the actual parent div tag.

This solution will work:

.hero
    width: 100%
    height: 100vh
    position: relative
    background-image: url('../assets/hero.png')
    background-position: center
    background-repeat: no-repeat
    background-size: cover
    z-index: 1

    &:before 
        content: ''
        position: absolute
        top: 0
        right: 0
        bottom: 0
        left: 0
        background: $secondary-blue
        opacity: .5
        overflow: hidden
        z-index: -1

.hero--welcome
    display: flex
    align-items: center
    justify-content: center
    flex-direction: column
    align-items: left
    width: 100%
    height: 100%
    z-index: 3
<div className="hero">
  <div className="hero--welcome">
    <h6>Welcome to</h6>
    <h1>Alhandsya</h1>
  </div>
</div>
Yashu Mittal
  • 1,478
  • 3
  • 14
  • 32