Opacity in child elements is typically inherited from their parent element.
However, we can utilize the css position property to achieve the desired result.
One way to do this is by placing the text container div outside of the parent div but using absolute positioning for the intended effect.
Desired Outcome------------------>>>>>>>>>>>>
HTML
<div class="container">
<div class="bar">
<div class="text">The text inherits opacity from the parent div.</div>
</div>
</div>
CSS
.container{
position:relative;
}
.bar{
opacity:0.2;
background-color:#000;
z-index:3;
position:absolute;
top:0;
left:0;
}
.text{
color:#fff;
}
Result:--
The text is not visible due to inheriting opacity from the parent div.
Solution ------------------->>>>>>
HTML
<div class="container">
<div class="text">Opacity is not inherited from the parent div "bar"</div>
<div class="bar"></div>
</div>
CSS
.container{
position:relative;
}
.bar{
opacity:0.2;
background-color:#000;
z-index:3;
position:absolute;
top:0;
left:0;
}
.text{
color:#fff;
z-index:3;
position:absolute;
top:0;
left:0;
}
Output :
The text is now visible with the same color as the background because the div is no longer within the transparent div.