ComparisonComparisons

display vs visibility vs opacity

display: none removes an element from the layout, visibility: hidden hides it but keeps its space, and opacity: 0 makes it transparent, yet still clickable.

On this page

Side-by-side comparison

#

Example

#
CSS
.item-none { display: none; }
Preview

Definition and usage

#

The three ways to "hide" an element differ in how much of its presence remains on the page. display: none removes the element from the layout completely, along with the space it occupied, as if it had never been there. visibility: hidden only hides it; the space in the layout stays empty. opacity: 0 also keeps the space, but structurally the element is still fully present, just visually transparent.

This difference in presence carries over to interaction. An element with display: none or visibility: hidden can’t be clicked or focused with Tab, and screen readers skip it. An element with opacity: 0 behaves as if it were visible: it reacts to clicks, takes its place in the Tab order and is read out by screen readers, which is easy to forget.

This also matters for animation. opacity changes smoothly, so it’s well suited to animated fade-ins and fade-outs. visibility changes in a single step, but you can pair it with transition so the element drops out of interaction exactly when the animation ends. display doesn’t take part in transitions at all by default, though the newer transition-behavior: allow-discrete changes that.

Syntax

#
Syntax
display: none;
visibility: hidden;
opacity: 0;

More examples

#
visibility: hidden keeps its space
CSS
.item-hidden { visibility: hidden; }
Preview

The space left by item 2 stays empty, unlike with display: none.

opacity: 0 still reacts to clicks
CSS
.ghost { opacity: 0; }
Preview

Despite being invisible, the button can still be clicked and focused with Tab, because opacity doesn’t turn off interaction.

Good practices

#
  • For an animated fade-in, use opacity, possibly paired with visibility and transition-behavior: allow-discrete.
  • If the element should disappear completely and free up its space, choose display: none.
  • A transparent but still clickable element is a common mistake. Add pointer-events: none if it shouldn’t react.

Related entries

#

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