CSS Reference ยท Colors and Backgrounds
The opacity property
Reference for the CSS opacity property: how it fades an element and its children, the stacking context it creates, how it differs from rgba colors, and examples.
The opacity property controls how transparent an element is. A value of 1 is fully opaque, 0 is fully transparent, and values in between let whatever is behind the element show through.
Opacity applies to the element as a whole: its background, border, text and all of its descendants are first drawn together as a group, and the group is then faded. That is why a child can never be more opaque than its parent.
.disabled { opacity: 0.5; } .ghost { opacity: 25%; }
Try it
Syntax
opacity: <alpha-value>; /* <number> | <percentage> */ opacity: 0.75; opacity: 75%;
- Initial value:
1 - Applies to: all elements
- Inherited: no (but the fade does cover all descendants, because they are painted as part of the element's group)
- Animatable: yes (as a number)
- Computed value: the specified number, clamped to the range 0 to 1
Values
| Value | Description |
|---|---|
1 (or 100%) |
Fully opaque. The default. |
| A number between 0 and 1 | Partly transparent. 0.5 is half transparent. |
0 (or 0%) |
Fully transparent. The element is invisible but still takes space and still receives clicks. |
| Out-of-range values | Values below 0 are treated as 0 and above 1 as 1. |
Usage notes
- A child cannot undo its parent's opacity. Setting
opacity: 1on a child of a 0.5 parent does not bring it back. To fade only a background, use a color with alpha such asrgb(0 0 0 / 50%)orbackground-colorwithcolor-mix(). - It creates a stacking context. Any value below
1makes the element a stacking context, which can change howz-indexworks for its descendants and can trapposition: fixedcontent inside its group. opacity: 0is notdisplay: none. The element still occupies space, can be focused and clicked, and is still read by screen readers. Combine it withvisibility: hiddenorpointer-events: noneif it should be truly inert.- Good for animation. Opacity is one of the cheapest properties to animate because browsers can often handle it on the compositor. Fade-ins and fade-outs are typical uses.
- Disabled look. Fading a disabled control is common, but low opacity may fail contrast requirements; check the text remains readable.
- Opacity is not a color. For text, prefer a translucent
color; for SVG shapes there are alsofill-opacityandstroke-opacity.
Browser support
Supported in every browser, including the percentage form in current ones.
Related
visibilityโ hide while keeping the spacebackground-colorโ translucent backgroundsmix-blend-modeโ blend instead of fadecolorโ text color with alpha- Lesson: Opacity and Visibility