Favorites

The difference between display: none & visibility: hidden

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

The difference between display: none & visibility: hidden

The key difference

If you've ever needed to hide an element on a page, you've probably reached for one of these two CSS properties. At first glance they seem identical: the element disappears. But they work in very different ways, and choosing the wrong one can cause subtle, frustrating layout bugs.

  • display: none removes the element from the layout entirely, as if it had never been there.
  • visibility: hidden makes the element invisible, but keeps its space in the layout.

display: none, gone without a trace

With display: none, the browser doesn't draw the element or any of its children, and doesn't give it any space. Everything around it moves to close the gap, as though it weren't in the page at all.

.hidden-box {
  display: none;  /* removed from the layout */
}

Press the button to hide Box B, and watch Box C:

Results
Box A: always visible
Box B: toggle me
Box C: always visible

When Box B is hidden, Box C immediately moves up into the freed space. There's no gap left behind. That makes display: none ideal for things that should appear and disappear completely, such as menus, tab panels, and sections that expand and collapse.

Note: elements with display: none are also hidden from screen readers (software that reads pages aloud), and can't receive keyboard focus.

visibility: hidden, the invisible ghost

visibility: hidden stops the element from being drawn, but leaves it in the layout. The browser still works out its size and position, so the space it takes up stays reserved, like an invisible placeholder.

.ghost-box {
  visibility: hidden;  /* invisible, but its space is kept */
}

/* Children can override it! */
.ghost-box .child {
  visibility: visible;  /* this child shows through */
}
Results
Box A: always visible
πŸ‘» Space preservedBox B: toggle me
Box C: always visible

Box B disappears, but its space stays put, and Box C doesn't move. And notice the "space preserved" label: it's a child of Box B with visibility: visible, so it shows through its hidden parent.

Bonus trick: unlike display: none, visibility: hidden can be overridden by children. Setting visibility: visible on a child makes it appear even though its parent is hidden.

Like display: none, a visibility: hidden element can't be clicked or focused, and screen readers ignore it.

Side-by-side comparison

Here's a quick reference, with opacity: 0 included as well, as it's often mixed up with the other two:

Behaviourdisplay: nonevisibility: hiddenopacity: 0
Keeps its space in the layoutβœ— Noβœ“ Yesβœ“ Yes
Visible on screenβœ— Noβœ— Noβœ— No
Read by screen readersβœ— Noβœ— Noβœ“ Yes
Can be clicked or focusedβœ— Noβœ— Noβœ“ Yes
Children can override itβœ— Noβœ“ Yesβœ— No
Can be animatedβœ“ Yes, with allow-discrete (see below)βœ“ Yes, switches at the start or end of a transitionβœ“ Yes, fades smoothly
Changes the layout when toggledβœ“ Yesβœ— Noβœ— No
Typical usesMenus, tab panels, collapsed contentKeeping layouts stable, fadesFade animations (combined with visibility)

Watch out for opacity: 0: it only makes an element see-through. The element can still be clicked, tabbed to and read by screen readers, which can be very confusing if someone lands on an invisible button.

Animating show and hide

Fading while keeping the space

A long-standing pattern combines opacity for the fade with visibility, so the element can't be clicked once it's faded out:

.fade-element {
  visibility: visible;
  opacity: 1;
  transition: opacity 0.3s ease, visibility 0.3s;
}

.fade-element.hidden {
  visibility: hidden;
  opacity: 0;
}

The opacity animates smoothly, while visibility waits until the end of the transition before switching to hidden. When the element is shown again, it switches back to visible straight away, so the fade-in can be seen.

Fading with display: none

For years, display: none couldn't be animated: elements simply popped in and out. Two newer CSS features fix that. Press the button to see a panel fade and slide in and out, while the content below moves to make room:

Results
This panel fades out, then is set to display: none.
Content below the panel
.panel {
  opacity: 1;
  transform: none;
  transition:
    opacity 0.4s ease,
    transform 0.4s ease,
    display 0.4s allow-discrete;   /* let display wait for the fade-out */
}

.panel.closed {
  display: none;
  opacity: 0;
  transform: translateY(-8px);
}

/* Where to start from when the panel appears */
@starting-style {
  .panel {
    opacity: 0;
    transform: translateY(-8px);
  }
}
  • transition-behavior: allow-discrete, written here as allow-discrete in the transition shorthand, lets properties that can't blend, like display, take part in a transition. On hiding, display stays visible until the fade has finished.
  • @starting-style sets where the fade-in starts from when the element appears. Without it, the browser has no "before" state to animate from, so the panel would just appear.

Both features have worked in all major browsers since 2024. Older browsers simply show and hide the panel instantly, without the fade.

Other ways to hide things

  • The HTML hidden attribute (<div hidden>) hides an element just like display: none, but in the HTML itself. It's handy for content that JavaScript shows and hides. Note that any CSS that sets display on the element will override it.
  • Visually hidden text is hidden from sight but still read by screen readers, which is useful for extra context such as "(opens in a new tab)". The usual technique is a small set of CSS rules:
    .visually-hidden {
      position: absolute;
      width: 1px;
      height: 1px;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
    }
  • content-visibility: hidden hides an element's contents while keeping the element's own box, and lets the browser keep the hidden content's layout ready, so showing it again is fast. It's useful for content that's toggled frequently.

Which one should you use?

  • Use display: none (or the hidden attribute) when the element shouldn't take up any space: menus, tab panels, collapsed sections and anything that appears only in certain situations.
  • Use visibility: hidden when the space must be kept, so the content around it doesn't jump, or when you want a child to show through a hidden parent.
  • Use opacity together with visibility for smooth fades that keep the space.
  • Use visually hidden text when something should be read by screen readers but not shown on screen.

Dig deeper

The display property goes far beyond none: it's the foundation of flexbox and grid. The visibility property has a third value, collapse, which behaves specially in tables and flex layouts. If you only want to stop clicks, take a look at pointer-events. And for smooth fade-outs, opacity paired with visibility is the go-to pattern.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.