CSS opacity Property

The CSS opacity property controls how transparent or opaque an element appears.

Opacity affects the entire rendered element, including its background, text, borders, images, and descendants. Values range from 0 (fully transparent) to 1 (fully opaque), with intermediate values producing partial transparency.

opacity Syntax

The opacity property accepts a number or percentage representing the element's opacity.

Basic Syntax

selector {
  opacity: 0.5;
}

Fully Opaque

.element {
  opacity: 1;
}

Partially Transparent

.element {
  opacity: 0.5;
}

Fully Transparent

.element {
  opacity: 0;
}

Percentage Values

.element {
  opacity: 75%;
}

Calculated Values

.element {
  opacity: calc(0.25 + 0.25);
}

Custom Properties

.element {
  --element-opacity: 0.8;
  opacity: var(--element-opacity);
}

CSS-Wide Keywords

.element {
  opacity: initial;
}
.element {
  opacity: inherit;
}
.element {
  opacity: unset;
}
.element {
  opacity: revert;
}
.element {
  opacity: revert-layer;
}

The initial value is 1, meaning the element is fully opaque.

Property Values

Value Description
<number> Specifies opacity using a number from 0 to 1.
<percentage> Specifies opacity using a percentage from 0% to 100%.
initial Resets the property to 1.
inherit Uses the computed opacity value of the parent element.
unset Resets the property to 1 because opacity is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 1
Inherited No
Applies to All elements
Computed value Specified number or percentage, with values outside the supported range clamped to 0 through 1
Animation type By computed value type
Creates stacking context Yes, when the used opacity is less than 1

How It Works

The browser renders an element and its descendants, then applies the element's opacity to the resulting visual content as a group.

Opacity does not change the element's dimensions or its position in the document layout.

Fully Opaque Element

.box {
  background-color: #2563eb;
  opacity: 1;
}

The box appears with its normal background color.

Half-Transparent Element

.box {
  background-color: #2563eb;
  opacity: 0.5;
}

The box and its contents are blended with whatever appears behind them.

Fully Transparent Element

.box {
  background-color: #2563eb;
  opacity: 0;
}

The box is invisible but remains part of the layout and can still receive pointer and keyboard interactions.

Opacity and Background Content

.container {
  background-color: #f1f5f9;
}
.box {
  background-color: #2563eb;
  opacity: 0.5;
}

The box appears lighter because its rendered pixels are blended with the light background behind it.

Important Consideration

Opacity is applied to the entire rendered element. It does not independently adjust the transparency of individual properties such as color or background-color.

Number Values

Number values range from 0 to 1, where larger numbers produce greater opacity.

Common Opacity Values

Value Opacity Appearance
0 0% Fully transparent
0.1 10% Very transparent
0.25 25% Mostly transparent
0.5 50% Half-transparent
0.75 75% Slightly transparent
0.9 90% Nearly opaque
1 100% Fully opaque

Low Opacity

.element {
  opacity: 0.2;
}

The element is mostly transparent.

Medium Opacity

.element {
  opacity: 0.5;
}

The element is half-transparent.

High Opacity

.element {
  opacity: 0.9;
}

The element is almost fully opaque.

Out-of-Range Values

.element {
  opacity: 1.5;
}
.element {
  opacity: -0.5;
}

Values outside the supported range are clamped to the nearest valid opacity level when computed. In these examples, the effective values are 1 and 0.

Important Consideration

Use values within the 0 to 1 range for clarity. Values outside this range do not produce additional opacity or transparency.

Percentage Values

Percentage values provide an alternative way to express opacity. A value of 100% is equivalent to 1, while 0% is equivalent to 0.

Fully Opaque

.element {
  opacity: 100%;
}

Half-Transparent

.element {
  opacity: 50%;
}

Mostly Transparent

.element {
  opacity: 25%;
}

Equivalent Values

Number Percentage
0 0%
0.2 20%
0.4 40%
0.5 50%
0.8 80%
1 100%

Calculated Percentage

.element {
  opacity: calc(100% - 25%);
}

The calculated opacity is 75%.

Important Consideration

Number and percentage values express the same opacity levels. Choose the format that makes your stylesheet easiest to read and maintain.

Opacity and Descendants

Although opacity is not inherited, applying it to a parent affects the appearance of all content rendered within that parent.

Transparent Parent

.parent {
  opacity: 0.5;
}
.child {
  opacity: 1;
}

The child cannot become fully opaque relative to the page because the parent's entire rendered group is displayed at 50% opacity.

Nested Opacity

.parent {
  opacity: 0.5;
}
.child {
  opacity: 0.5;
}

The child's rendered content is first displayed at 50% opacity within the parent, and the parent group is then displayed at 50% opacity. For an isolated child pixel, the combined effective opacity is approximately 25%.

Keeping Text Fully Opaque

If only the background should be transparent, use a color with an alpha channel instead of applying opacity to the parent.

.panel {
  background-color: rgb(37 99 235 / 50%);
  color: #fff;
}

The background is partially transparent while the text retains its own opacity.

Separate Background Layer

.panel {
  position: relative;
  isolation: isolate;
}
.panel::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background-color: #2563eb;
  opacity: 0.5;
}

A pseudo-element can provide a transparent background layer without reducing the opacity of the panel's foreground content.

Important Consideration

Setting opacity: 1 on a child does not cancel the opacity applied to an ancestor. Use separate layers or alpha-transparent colors when different parts of a component need different transparency levels.

Opacity vs. Alpha Transparency

The opacity property and alpha-transparent CSS colors both produce transparency, but they affect different parts of an element.

Using opacity

.panel {
  background-color: #2563eb;
  color: #fff;
  opacity: 0.5;
}

The entire panel, including its text and background, becomes half-transparent.

Using an Alpha-Transparent Background

.panel {
  background-color: rgb(37 99 235 / 50%);
  color: #fff;
}

Only the background color has 50% alpha transparency. The text remains fully opaque.

Using Hexadecimal Alpha

.panel {
  background-color: #2563eb80;
}

The final two hexadecimal digits specify the alpha channel. 80 represents approximately 50% opacity.

Using Transparent Text

.panel {
  background-color: #2563eb;
  color: rgb(255 255 255 / 60%);
}

The text is partially transparent while the background remains opaque.

Comparison Table

Feature opacity Alpha Color
Affects the whole element Yes No
Affects descendants visually Yes Only through the color being applied
Can make only a background transparent No Yes
Can be animated Yes Yes, for compatible color values
Creates a stacking context by itself Yes, below 1 No

Important Consideration

Use opacity when the entire element should fade. Use an alpha-transparent color when only a particular color, such as the background or text color, should be transparent.

Opacity vs. visibility and display

The opacity, visibility, and display properties can all affect whether an element is visible, but they behave differently.

Using opacity: 0

.element {
  opacity: 0;
}

The element is invisible but still occupies space and can remain interactive.

Using visibility: hidden

.element {
  visibility: hidden;
}

The element is hidden while retaining its layout space. It is generally removed from pointer interaction and keyboard focus.

Using display: none

.element {
  display: none;
}

The element does not generate a layout box and does not occupy space in the normal layout.

Comparison Table

Behavior opacity: 0 visibility: hidden display: none
Visible No No No
Retains layout space Yes Yes No
Can receive pointer events by default Yes No No
Can remain keyboard-focusable Yes Generally no No
Supports gradual fading Yes No, not by itself No, not by itself

Important Consideration

Opacity is primarily a visual property. Do not use opacity: 0 alone when an element must also be removed from interaction or accessibility navigation.

Stacking Contexts

An element with a used opacity value below 1 creates a new stacking context.

A stacking context groups elements for painting and controls how their z-index values interact with elements outside that context.

Creating a Stacking Context

.panel {
  opacity: 0.9;
}

The panel creates a stacking context because its opacity is less than 1.

Nested z-index Values

.panel {
  opacity: 0.9;
}
.panel .child {
  position: relative;
  z-index: 999;
}

The child's large z-index value cannot independently move it outside the parent's stacking context.

Opaque Element

.panel {
  opacity: 1;
}

An opacity value of 1 does not create a stacking context by itself.

Important Consideration

If overlapping elements behave unexpectedly after adding opacity, inspect the stacking contexts created by their ancestors. Increasing a descendant's z-index cannot make it escape its parent's stacking context.

Pointer Events and Interaction

Reducing opacity does not automatically prevent an element from receiving mouse, touch, or keyboard interactions.

Invisible Interactive Element

.button {
  opacity: 0;
}

The button can remain clickable and keyboard-focusable even though it is invisible.

Disabling Pointer Events

.button {
  opacity: 0;
  pointer-events: none;
}

Pointer interactions no longer target the button, but it may still be reachable using keyboard navigation.

Hiding an Interactive Component

.panel {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.panel.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

Combining opacity with visibility provides a more appropriate hiding behavior for many interactive components.

Disabled Buttons

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

Opacity can visually indicate a disabled button, but the actual disabled state must be established using the HTML disabled attribute when supported.

Important Consideration

Visual transparency does not define whether a control is enabled, disabled, hidden from assistive technology, or included in keyboard navigation.

Hover Effects

Opacity is commonly used to provide visual feedback when users hover over links, buttons, and images.

Image Hover Effect

.preview-image {
  opacity: 1;
}
.preview-image:hover {
  opacity: 0.7;
}

The image becomes partially transparent when hovered.

Button Hover Effect

.button {
  opacity: 0.85;
}
.button:hover {
  opacity: 1;
}

The button becomes fully opaque when hovered.

Link Hover Effect

.navigation-link {
  opacity: 0.75;
}
.navigation-link:hover,
.navigation-link:focus-visible {
  opacity: 1;
}

The link becomes fully opaque on hover or visible keyboard focus.

Important Consideration

Hover-only effects are not available to all users. Include appropriate keyboard focus styling and ensure interactive elements remain identifiable without hovering.

Opacity Transitions

The opacity property can be smoothly transitioned between values using the CSS transition property.

Basic Transition

.element {
  opacity: 0.5;
  transition: opacity 0.3s ease;
}
.element:hover {
  opacity: 1;
}

The element fades from 50% to 100% opacity over 0.3 seconds.

Linear Transition

.element {
  opacity: 1;
  transition: opacity 0.5s linear;
}
.element:hover {
  opacity: 0.3;
}

The element fades at a constant rate.

Delayed Transition

.element {
  opacity: 0.5;
  transition: opacity 0.4s ease 0.2s;
}
.element:hover {
  opacity: 1;
}

The transition begins after a 0.2-second delay.

Transition with Visibility

.panel {
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.3s ease, visibility 0.3s;
}
.panel.is-visible {
  opacity: 1;
  visibility: visible;
}

The panel can fade in and out while also changing its visibility state. The discrete visibility transition allows the panel to remain visible during the fade-out.

Important Consideration

A transition requires a change between values. Declaring transition alone does not start an animation.

Opacity Animations

CSS keyframe animations can change opacity automatically over time without requiring a hover or other state change.

Fade In

.fade-in {
  animation: fade-in 1s ease forwards;
}
@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

The element fades from fully transparent to fully opaque.

Fade Out

.fade-out {
  animation: fade-out 1s ease forwards;
}
@keyframes fade-out {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

The element gradually becomes transparent.

Pulsing Opacity

.status-indicator {
  animation: pulse-opacity 2s ease-in-out infinite;
}
@keyframes pulse-opacity {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.4;
  }
}

The element repeatedly fades between full and partial opacity.

Fade and Move

.notification {
  animation: show-notification 0.5s ease forwards;
}
@keyframes show-notification {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

The notification fades into view while moving upward slightly.

Reduced Motion

@media (prefers-reduced-motion: reduce) {
  .fade-in,
  .fade-out,
  .status-indicator,
  .notification {
    animation: none;
  }
}

For components that depend on animation to reveal content, ensure their static appearance remains visible and usable when animation is disabled.

Important Consideration

Opacity animations change visual appearance without removing elements from layout or automatically disabling their interactions.

Accessibility Considerations

Opacity can affect readability and interaction, so it should be used carefully with text, controls, and essential content.

Text Contrast

Reducing the opacity of text changes its effective color against the background and can reduce its contrast ratio.

.secondary-text {
  color: #334155;
  opacity: 0.5;
}

The resulting text may be too faint for comfortable reading. Check the final rendered contrast rather than relying only on the original text color.

Interactive Controls

Do not rely on opacity alone to communicate whether a control is disabled or unavailable.

<button type="button" disabled>Unavailable</button>
button:disabled {
  opacity: 0.5;
}

The HTML attribute establishes the disabled state, while opacity provides a visual cue.

Invisible Focusable Elements

Elements with opacity: 0 can remain keyboard-focusable. This can cause users to navigate to controls they cannot see.

Reduced Transparency Preferences

The prefers-reduced-transparency media feature can be used to provide more opaque alternatives in browsers and operating systems that support it.

.overlay {
  opacity: 0.8;
}
@media (prefers-reduced-transparency: reduce) {
  .overlay {
    opacity: 1;
  }
}

Reduced Motion Preferences

Repeated fading effects may be distracting. Respect prefers-reduced-motion for nonessential opacity animations.

Important Consideration

Ensure that essential text, navigation, and controls remain readable, discoverable, and operable regardless of their opacity.

opacity Example

This example compares four opacity values using the same blue background and white text. Each box is displayed over a checkerboard background to make transparency easier to see.

HTML Example

<div class="opacity-demo">
  <div class="opacity-box opacity-100">1</div>
  <div class="opacity-box opacity-75">0.75</div>
  <div class="opacity-box opacity-50">0.5</div>
  <div class="opacity-box opacity-25">0.25</div>
</div>

CSS Example

.opacity-demo {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 16px;
  background-color: #fff;
  background-image: linear-gradient(45deg, #e2e8f0 25%, transparent 25%), linear-gradient(-45deg, #e2e8f0 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e2e8f0 75%), linear-gradient(-45deg, transparent 75%, #e2e8f0 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
}
.opacity-box {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 90px;
  height: 70px;
  background-color: #2563eb;
  color: #fff;
  font-weight: 700;
}
.opacity-100 {
  opacity: 1;
}
.opacity-75 {
  opacity: 0.75;
}
.opacity-50 {
  opacity: 0.5;
}
.opacity-25 {
  opacity: 0.25;
}
Browser Result
1
0.75
0.5
0.25

The first box is fully opaque. Each following box becomes progressively more transparent, including its text.

Interactive Hover Example

The following example uses a transition to increase a button's opacity when hovered or focused using the keyboard.

<button class="opacity-button" type="button">Hover or Focus Me</button>
.opacity-button {
  padding: 12px 20px;
  border: 0;
  background-color: #2563eb;
  color: #fff;
  opacity: 0.7;
  cursor: pointer;
  transition: opacity 0.3s ease;
}
.opacity-button:hover,
.opacity-button:focus-visible {
  opacity: 1;
}
@media (prefers-reduced-motion: reduce) {
  .opacity-button {
    transition: none;
  }
}
Browser Result

The button begins at 70% opacity and smoothly becomes fully opaque when hovered or keyboard-focused.

Common Mistakes

Expecting Only the Background to Become Transparent

The opacity property affects the entire rendered element, including its text and descendants. Use an alpha-transparent background color when only the background should be transparent.

Assuming opacity Is Inherited

Opacity is not inherited, but a parent's opacity affects the appearance of its descendants because the rendered group is composited together.

Trying to Restore Child Opacity

.parent {
  opacity: 0.5;
}
.child {
  opacity: 1;
}

The child cannot cancel the parent's group opacity.

Assuming opacity: 0 Removes an Element

An element with zero opacity remains in layout and can still receive interactions.

Using Opacity to Disable Controls

Opacity changes appearance, not the actual enabled or disabled state of an HTML control.

Using Incorrect Percentage Equivalents

opacity: 0.5 is equivalent to opacity: 50%, not opacity: 0.5%.

Ignoring Text Contrast

Lower opacity can reduce text contrast and make content difficult to read.

Overlooking Stacking Contexts

Opacity values below 1 create stacking contexts, which can affect overlapping elements and z-index behavior.

Forgetting Transition Requirements

A transition requires a change in opacity between states. Declaring a transition without changing the value produces no visible effect.

Ignoring Keyboard Focus

Hover effects should have appropriate focus styles so keyboard users receive comparable visual feedback.

Important Considerations

Keep the following points in mind when using opacity:

  • The property controls the transparency of an element's rendered appearance.
  • Its initial value is 1.
  • The property is not inherited.
  • It applies to all elements.
  • It accepts number and percentage values.
  • 0 and 0% represent full transparency.
  • 1 and 100% represent full opacity.
  • Values outside the supported range are clamped when computed.
  • Opacity affects text, backgrounds, borders, images, and descendants together.
  • A child cannot cancel the opacity of an ancestor.
  • Alpha-transparent colors are better when only a specific color should be transparent.
  • Opacity does not change an element's dimensions or layout position.
  • An element with opacity: 0 can remain interactive.
  • Opacity does not automatically remove elements from accessibility navigation.
  • Used opacity values below 1 create stacking contexts.
  • Stacking contexts can affect z-index behavior.
  • Opacity can be animated using CSS transitions and keyframes.
  • Hover effects should include appropriate keyboard focus behavior.
  • Reduced opacity can affect text contrast and readability.
  • Opacity alone should not indicate a control's disabled state.
  • Reduced-motion preferences should be respected for nonessential animations.
  • Reduced-transparency preferences can be supported where available.
  • Test transparency against the actual backgrounds used in the design.

Browser Support

The CSS opacity property is Baseline - Widely available and has been supported in major browsers for many years.

Modern versions of Chrome, Edge, Firefox, Safari, and other major browsers support opacity values and opacity animations. Percentage syntax is also supported in current browsers.

For current compatibility information, visit Can I Use? and search for opacity.

Although opacity itself is broadly supported, related features such as prefers-reduced-transparency may have more limited browser support.

Specifications

The opacity property is defined in CSS Color Module Level 4.

CSS Color Module Level 4: Transparency

The specification defines how opacity values are interpreted and how transparency is applied during rendering.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: opacity