Full answer
Both make things see-through, but they work on different parts of an element.
- A colour with alpha, such as
rgb(0 0 0 / 50%)orrgba(0, 0, 0, 0.5), affects only that colour. Text and borders keep their own colours. Children are not changed. opacityaffects the whole element as one group: its background, border, text and all its children. The value goes from 0 (invisible) to 1 (normal). Children cannot be more opaque than their parent.
Use alpha when you want a see-through background with clear text, for example a dark overlay behind white text. Use opacity when you want to fade the whole element, for example in a fade animation or for a disabled look.
More points:
opacityis not inherited, but its effect covers the children anyway, because the element is drawn as one group.opacitybelow 1 creates a new stacking context. This can change howz-indexworks for the children, and it can trap a positioned child so it cannot appear above outside elements.- An element with
opacity: 0is still in the layout, it still receives clicks and keyboard focus, and screen readers still read it. It is not a way to hide content. Usedisplay: none,hiddenorinertfor that. - Transitions of
opacityare cheap for the browser, because they usually do not need layout or repaint. - Low contrast: faded text can fail the contrast rules. Check the final colours, because opacity mixes the text with the background.
In the first box, only the background is half transparent, and the white text stays fully white. In the second box, the background, the text and everything inside are half transparent, so the text looks dimmer. The text element has its own opacity of 1, but it is still faded, because its parent is drawn as a group.
Decide first what should fade, and then pick the tool.
<style>
.box { padding: 8px; color: #fff; width: 200px; }
.a { background: rgb(0 0 0 / 50%); }
.b { background: #000; opacity: 0.5; }
</style>
<div style="background: #f90; padding: 8px">
<div class="box a" id="a"><span id="at">Alpha background</span></div>
<div class="box b" id="b"><span id="bt">Whole element faded</span></div>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Alpha colour affects only that colour.
- opacity affects the whole element and its children.
- opacity below 1 creates a stacking context.
- opacity: 0 does not hide the element from keyboard or screen readers.
- Check the contrast of the final result.
Common mistakes
- Using opacity on a container when only the background should be see-through.
- Using opacity: 0 to hide content and leaving it focusable.
- Forgetting that opacity creates a stacking context.
Follow-ups
- Does opacity: 0 remove an element from the layout?
- Why can opacity change z-index behaviour?
Tips
- Say which parts each property affects.
- Give one example for each: overlay and fade.