liveBook · Manning

Opacity

As explained in CSS in Depth, Second Edition

The CSS opacity property specifies the compositing opacity of an element and its contents.

A dropdown should fade into view, not snap open. Set its opacity to 0 when closed and 1 when open, then transition between them. Opacity accepts values from 0 (fully invisible) to 1 (fully opaque); unlike display, it can produce a fade. section 15.3.2 section 15.3.1

The transparent-menu trap

A drawer with `opacity: 0` and `transition: opacity 0.2s linear` appears to close smoothly. But it remains on the page: an invisible menu item can intercept a click intended for the link behind it. Transparency alone is not a closed state. section 15.3.2

Listing 15.9 Using a transition delay to manipulate when visibility changes
.dropdown__drawer {
  position: absolute;
  background-color: var(--background-color);
  width: 10em;
  visibility: hidden;    #1
  opacity: 0;            #1
  transition:
    opacity 0.2s linear,
    visibility 0s linear 0.2s;     #2
}
.dropdown.is-open .dropdown__drawer {
  visibility: visible;                  #3
  opacity: 1;                           #3
  transition-delay: 0s;                 #4
}

The complete dropdown rules pair the opacity fade with a visibility change. On closing, visibility switches to hidden after the 0.2-second fade; on opening, the delay resets to zero so the drawer becomes visible immediately. section 15.3.2

Visibility solves the click problem, but it does not remove an element from document flow. The dropdown uses absolute positioning, so a hidden drawer does not leave a gap. Also check stacking when adding opacity elsewhere: a value below 1 creates a stacking context, which can affect how descendants overlap other elements. section 15.3.2 section 6.4.3

Opacity can work alongside motion rather than provide the whole effect:

  • Navigation labels transition from `opacity: 0` and `translate: -1em` to full opacity and their final position on hover or keyboard focus within the menu. section 16.2.2
  • Fly-in cards use keyframes to reach full opacity while moving forward in 3D and rotating toward the viewer. section 17.2.2

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the 0–1 range and works through a dropdown fade, its transparent click target, and a delayed-visibility fix.
Examples
Uses a dropdown that blocks a link while transparent, then pairs its fade with delayed visibility.
What it assumes
Assumes familiarity with CSS selectors and transitions while explaining opacity values and the visibility timing needed for a dropdown.
What you can do after
Build it from scratch: fade a dropdown while hiding its transparent drawer after the fade completes.
How it connects
Connects opacity to CSS transitions, visibility, display, and stacking contexts.

Also explained in

Compare how 3 books explain opacity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.