Why does opacity make a whole element translucent instead of just its background?
Quick answer
The opacity property affects the element as a whole, including its contents, so text and other child content appear translucent along with the background. [S1] To make just the background translucent, put an alpha value in the background color instead; that leaves the text at its normal opacity. [S1] Choose the approach based on which parts you want to see through.
The question, the details, the next step.
A practical checklist
Decide whether you want the whole element faded or only its background translucent.
For a translucent background, use a color with an alpha value in the background declaration.
Remove a reduced
opacityvalue if the element’s text should not fade with it.Check the result over the actual background behind the element.
Review whether the text remains easy to distinguish in your design.
A few more useful answers
Does `opacity` apply only to child elements?
No. It affects the element as a whole, including its rendered contents; it is not an inherited property, but the contents are affected together. [S1]
What should I change if I want only the background to show through?
Use a background color with an alpha channel, such as background: rgb(0 0 0 / 40%), rather than lowering the element’s opacity. [S1]
Does setting opacity to zero remove an element?
No. Zero opacity does not remove the element from the DOM and does not necessarily prevent pointer interaction or keyboard focus. [S1]
Sources
References used for this guide.
- [S1] opacity CSS property — MDN developer.mozilla.org · Checked
