What is opacity?

opacity sets how transparent an entire element is, from 1 (fully opaque) to 0 (fully transparent), and it applies to the element and all its children together. To make only a background or color translucent while keeping text solid, use an alpha color such as rgb(0 0 0 / 50%) instead.

Key behaviors

  • Affects everything inside: text, images, borders and descendants. Children cannot be more opaque than their parent.
  • Creates a stacking context when below 1, which can change how z-index behaves.
  • Elements at opacity: 0 are still there: they take up space, receive clicks and are read by screen readers. Add pointer-events: none or inert (and aria-hidden where appropriate), or use visibility: hidden.
  • Animates cheaply: opacity changes can run on the compositor (GPU), making fades smooth without layout or paint. Together with transform, it is one of the two properties that are safe to animate.

opacity vs alpha colors

.overlay { background: rgb(11 16 32 / 60%); }  /* only background is translucent */
.faded   { opacity: .6; }                       /* the whole element fades */

Other transparency tools

  • filter: opacity(50%), similar but via filters.
  • color-mix(in srgb, red 50%, transparent) for translucent colors from variables.
  • mix-blend-mode and backdrop-filter: blur() for glass effects.

Accessibility

Low opacity can drop text contrast below WCAG's 4.5:1; check contrast for disabled or muted states.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 .hero { position: relative; width: 320px; height: 140px; background: url(https://picsum.photos/id/1036/640/280) center / cover; }
4 .alpha { position: absolute; inset: auto 0 0 0; padding: 10px; color: white; background: rgb(11 16 32 / 65%); }
5 .faded { opacity: .5; }
6 .card { padding: 10px; border: 2px solid #0b1020; margin: 10px 0; width: 300px; transition: opacity .3s; }
7 .card:hover { opacity: .6; }
8</style>
9<div class="hero">
10 <p class="alpha">Alpha background: the text stays fully opaque</p>
11</div>
12<div class="card faded">opacity: .5 fades the whole card, text included</div>
13<div class="card">Hover me: opacity transitions are GPU friendly</div>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app