opacity

Sets the transparency of the whole element together with its contents.

Initial value
1
Inherited
No
Animatable
yes, as a number
Applies to
all elements
JavaScript
element.style.opacity = "0.5"
On this page

Example

#
CSS
.card {
  transition: opacity 0.2s;
}

.card:hover {
  opacity: 0.8;
}

.card.locked {
  opacity: 0.4;
  pointer-events: none;
}
Preview

Definition and usage

#

The opacity property takes a number from 0 (fully transparent) to 1 (fully visible) or a percentage. It applies to the whole element: background, text, border and all children.

If you want only the background to be transparent while the text stays crisp, use a colour with an alpha channel, for example background-color: rgb(0 0 0 / 0.5), instead of opacity.

An element with opacity: 0 is invisible but still takes up space and can be clicked. A value below 1 also creates a new stacking context, which affects how z-index works.

Syntax

#
Syntax
opacity: <number 0-1> | <percentage>;

Values

#
  • 1

    The element is fully visible.
  • 0.6

    Partly transparent, the background stripes show through.
  • 0.2

    Almost invisible.
  • 0

    Invisible, but still takes up space.

Values in practice

#

1

The element is fully visible.
.demo { opacity: 1; }

0.6

Partly transparent, the background stripes show through.
.demo { opacity: 0.6; }

0.2

Almost invisible.
.demo { opacity: 0.2; }

0

Invisible, but still takes up space.
.demo { opacity: 0; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For a semi-transparent background under crisp text, use a colour with transparency, not opacity.
  • Animations of opacity and transform are the cheapest for the browser. They work well for smooth fade-in effects.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.