burtonLowel
burtonLowel

Reputation: 794

CSS: Mask with nested image is inheriting opacity

I am creating a mask for a video container so that when you hover over it, it is a dark mask that has a play/pause button inside it. The issue is that the arrow is nested inside the mask div, which has an opacity on it so the nested img is inheriting that opacity. I tried to change opacity to background: rgba(255,255,255,0.5) that I found on other solutions but that didn't seem work as the background seems to be hidden by the video . I am out of ideas.

<div class="video-container>
  <video src="video.mp4"></video>
  <div class="mask>
      <img class="arrow" src="arrow.png" />
   </div>
</div>

CSS

.video-container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    margin-right: 50px;
    flex-direction: column;
    img {
        position: absolute;
        z-index: 555;
        &:hover {
            cursor: pointer;
        }
    }
    .mask {
        width: 100%;
        height: 100%;
        position: absolute;
        background: #000;
        opacity: 0.0;
        display: flex;
        justify-content: center;
        align-items: center;
        transition: opacity 0.7s ease;
        &:hover {
            transition: opacity 0.7s ease;
            opacity: 0.5;
            cursor: pointer;
        }
    }
}

Upvotes: 0

Views: 153

Answers (2)

vals
vals

Reputation: 64244

A solution using opacity : 1 in the hover state, and a background with alpha, as you wanted. I changed the video and the image to do not use external resources.

  .video-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin-right: 50px;
}

#video {
  background: linear-gradient(red, blue);
  height: 300px;
  width: 400px;
}

.arrow {
  position: absolute;
  z-index: 5;
  font-size: 30px;
  color: green;
}

.arrow:hover {
    cursor: pointer;
}

.mask {
  width: 100%;
  height: 100%;
  position: absolute;
  background: #0002;
  opacity: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: opacity 0.7s ease;
}
.mask:hover {
    opacity: 1;
    cursor: pointer;
  }
<div class="video-container">
  <div id="video"></div>
  <div class="mask">
    <div class="arrow">arrow</div>
  </div>
</div>

Upvotes: 0

AlanVelasco
AlanVelasco

Reputation: 70

What about two different backgrounds? One for the mask and another for the arrow: I gave it a try here

.video-container {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  margin-right: 50px;
  flex-direction: column;

  img {
    position: absolute;
    z-index: 555;

    &:hover {
      cursor: pointer;
    }
  }
  img.arrow {
    width:20px;
    heigth:20px;
  }

  .mask {
    width: 100%;
    height: 100%;
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;

    &:hover .bg-mask {
        transition: opacity 0.7s ease;
        opacity: 0.5;
    }
    &:hover .bg-arrow {
        transition: opacity 0s ease;
        opacity: 1;
    }
    .bg-mask {
      position:absolute;
      width: 100%;
      height: 100%;
      background: #000;
      transition: opacity 0.7s ease;
      opacity: 0.0;

    }
    .bg-arrow {
      position:absolute;
      width: 100%;
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      transition: opacity 0s ease;
      opacity: 0.0;
    }
  }
}

Upvotes: 1

Related Questions