opacity applies uniformly to an entire element and everything inside it, its background, border, text, and any nested children, all becoming proportionally transparent together, which is fundamentally different from setting a color using rgba() or hsl() with an alpha value, which only makes that one specific property, like just the background color, semi-transparent while leaving text and other properties fully opaque. This means opacity: 0.5 on a parent element also makes its nested children appear at 50% opacity, even if those children have their own fully opaque colors set, since opacity affects the entire rendered element as a single composited layer rather than any one individual property.
1Understanding Opacity
opacity applies uniformly to an entire element and everything inside it, its background, border, text, and any nested children, all becoming proportionally transparent together, which is fundamentally different from setting a color using rgba() or hsl() with an alpha value, which only makes that one specific property, like just the background color, semi-transparent while leaving text and other properties fully opaque. This means opacity: 0.5 on a parent element also makes its nested children appear at 50% opacity, even if those children have their own fully opaque colors set, since opacity affects the entire rendered element as a single composited layer rather than any one individual property.
Use rgba()/hsl() with an alpha value on a specific property, like background-color, when you only want that one property semi-transparent — use the opacity property only when you want the entire element, including all of its text and nested children, to become uniformly transparent together.
.overlay {
background-color: black;
opacity: 0.5;
}2Practical Example
Here is a real-world application of Opacity showing how it is used in production CSS code.
.tinted-box {
background-color: rgba(0, 0, 0, 0.5);
}
<div class="tinted-box"><p>Fully opaque text</p></div>3Best Practices
Follow these guidelines when working with Opacity:
1. Use an alpha-channel color value, like rgba(), on a specific property when you want only that one property semi-transparent, not the whole element
2. Use the opacity property specifically when an entire element, including its text and children, should fade or appear uniformly translucent together
3. Remember opacity: 0 still occupies layout space and remains in the accessibility tree/interactive, unlike display: none, which is important when hiding something purely visually versus removing it entirely
Tip: Use rgba()/hsl() with an alpha value on a specific property, like background-color, when you only want that one property semi-transparent — use the opacity property only when you want the entire element, including all of its text and nested children, to become uniformly transparent together.
.overlay {
background-color: black;
opacity: 0.5;
}