My goal is to create a div
that acts as a mask covering the entire window. It should have an opacity of 0.5 and a background color of lightgray
. However, there is one particular div
on the screen that I do not want to be affected by the mask. I attempted to set its z-index
higher than the mask's, but that approach did not work. The code snippet in question is shown below:
.parent {
height: 300px;
background-color: red;
}
.sub1 {
height: 100px;
background-color: yellow;
z-index: 10;
opacity: 1;
}
.mask {
position: fixed;
left: 0;
top: 0;
height: 100%;
width: 100%;
opacity: 0.5;
background-color: lightgray;
z-index: 1;
}
<div class="parent">
<div class="sub1">
hello
</div>
<div class="mask">
</div>
</div>
Even though I aimed for the sub1
div to appear on top of the mask
, it still displayed with an opacity of 0.5.
The complete code can be accessed and run from this link: https://codepen.io/zhaoyi0113/pen/vzERYY.