日本語

Differences and Use Cases of Three Ways to Hide Elements in CSS

​​CSS​Accessibility,JavaScript

display: none, visibility: hidden, and opacity: 0 all make an element invisible, but they behave differently once hidden. Comparing them on three points, whether the space stays, whether clicks and Tab key focus still reach the element, and whether it remains in the accessibility tree that screen readers use, makes the choice clear. This can help you decide which to use when closing a modal or dropdown menu, switching tab contents, or adding a fade-out animation.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened a page with three boxes, A, B, and C, side by side in Playwright, hid the middle box B (which contains a button) in each of the three ways, and checked its position, clicks, Tab key behavior, and the accessibility tree. I did not test Firefox or Safari, or screen reading with an actual screen reader.

The results are summarized below.

display: none visibility: hidden opacity: 0
Space Removed (following content shifts) Kept Kept
Click Does not reach Does not reach (reaches the element below) Reaches
Tab key focus Skipped Skipped Stops here
Accessibility tree Removed Removed Kept
Override in child Not possible Possible with visibility: visible Not possible
Transition Not possible as is Possible in combination with opacity Possible

Only visibility and opacity keep the space

When I hid the middle box B, display: none removed its space, and C shifted left into it. With visibility: hidden and opacity: 0, B became invisible but its space remained.

Four-panel comparison. "No change" shows boxes A, B, and C side by side. "display: none" shows only A and C, with C in B's former position. "visibility: hidden" and "opacity: 0" show a gap between A and C, outlined with a pink dotted line labeled "B's space"
Layout when the middle box B is hidden in each of the three ways. The dotted outline marks B’s space, which is still there even though B is invisible

The values read from JavaScript also differed. For display: none, B's getBoundingClientRect() width and height were 0, and its offsetParent was null. For visibility: hidden and opacity: 0, B kept its pre-hidden width of 120px and height of 80px. If you check whether an element is displayed by its width in JavaScript, an element hidden with visibility or opacity will be judged as displayed.

The MDN page for display also explains that none does not affect layout, and that if you want to make something invisible while keeping its space, you should use visibility.

opacity: 0 still accepts clicks and Tab key focus

I clicked the position where B's button had been before hiding it. The three methods gave different results.

  • display: none: C, which moved left into the space, received the click.
  • visibility: hidden: The button did not respond. The outer element that contains A, B, and C received the click. In other words, the click passed through the invisible B and reached the element below.
  • opacity: 0: The click event on the invisible button fired.

The same difference appeared with the keyboard. When I focused the input field before B and pressed the Tab key, with display: none and visibility: hidden the button was skipped and focus moved to the input field after B. Calling focus() on the button from JavaScript did not move focus to it either. With opacity: 0, focus stopped on the invisible button, and pressing Enter fired the click event.

Warning

A button or link hidden only with opacity: 0 can still be pressed while invisible. Clicking a spot that looks empty will trigger the hidden button. For a closed menu, do not leave it at opacity: 0. Make the fully hidden state visibility: hidden or display: none.

The MDN page for opacity also says that an element with opacity: 0 still receives pointer events, and receives focus if it is in the tab order.

Only opacity keeps the element in the accessibility tree

I used Playwright's ariaSnapshot() to dump the accessibility tree for the outer element that contains A, B, and C.

yaml
# when display: none and visibility: hidden
- text: A C

# when opacity: 0 (same as when not hidden)
- text: A
- button "B のボタン"
- text: C

With display: none and visibility: hidden, B's button was removed from the tree. With opacity: 0, it remained. The MDN pages for visibility and display also state that elements with hidden or none, along with their descendants, are removed from the accessibility tree and are not read aloud. The opacity page says not to use opacity alone to switch an element between visible and hidden for screen readers.

Only visibility lets you show a child inside a hidden parent

When you want to keep a parent hidden but show only a child, only visibility can be overridden. I hid the parent B, set an override value on the button inside it, checked whether anything was drawn in B's area by examining the screen pixels, and clicked the button's position. With display: none, C moves into B's area, so I judged by the button's width instead.

Parent B Child button Result
visibility: hidden visibility: visible Only the button was visible, and clicking it fired click
opacity: 0 opacity: 1 The button stayed invisible (clicking it still fired click)
display: none display: block The button was not displayed, and its width was 0 (click did not fire)

The MDN page for visibility also says that a descendant of a hidden element is displayed if its visibility is visible. opacity is not inherited by children. It applies to the element and its contents as a whole (MDN page for opacity). If the parent is 0, a child set to 1 is still invisible.

Combine opacity and visibility for a fade-out

To add a fade-out animation, transition opacity and visibility together.

css
.menu {
  transition: opacity 400ms linear, visibility 400ms linear;
}

.menu.is-hidden {
  opacity: 0;
  visibility: hidden;
}

In my test, I applied the same declarations to B and hid it with a class (the class name differs from the example). 200ms after adding the class, the computed values (read with getComputedStyle()) were opacity 0.54 and visibility still visible. At 450ms, opacity was 0 and visibility was hidden. Because visibility has no intermediate values, when transitioning between visible and hidden, it stays visible until the end of the animation (see the interpolation section of the MDN page for visibility). In the test as well, it was still visible at 200ms and changed to hidden at 450ms, after the 400ms transition duration had passed.

If you keep the same transition and use a class that sets opacity: 0; display: none, display becomes none immediately and the element disappears without fading. If you want display to transition too, add allow-discrete from transition-behavior.

css
.menu {
  transition: opacity 400ms linear, display 400ms allow-discrete;
}

.menu.is-hidden {
  opacity: 0;
  display: none;
}

With this declaration, at 200ms display was still block and opacity was 0.54. At 450ms, display became none. The MDN page for transition-behavior also explains that when a transition changes display to none, the value switches to none only at the end of the animation. Also, the MDN page for opacity notes that fading in when going from display: none back to visible additionally requires @starting-style.

Which one to choose

For a closed modal or a switched tab's contents, where you want to remove the space, the operation, and screen reading all at once, use display: none. To hide something while keeping its space (when hiding it would shift the following elements in a way that causes problems), or to hide it after a fade-out, use visibility: hidden. opacity: 0 only removes the appearance, so use it for intermediate values during a fade, and make the fully hidden state with visibility: hidden or display: none.

​Posted by 86