The short answer

#

display: none removes the element completely: no space, no clicks, no focus, and screen readers skip it. visibility: hidden keeps its space but hides it from everyone. opacity: 0 only makes it transparent: it still takes space, can be clicked and focused, and is still read by screen readers.


_10
.gone { display: none; } /* no box at all */
_10
.invisible { visibility: hidden; } /* empty space left behind */
_10
.clear { opacity: 0; } /* still there, just see-through */

Why it happens

#

The three properties work at different stages of how the browser renders a page.

  • display controls whether a box is generated at all. With none, the element and all its children are taken out of layout, so the content around them moves up to fill the gap. It is also removed from the accessibility tree.
  • visibility controls whether a box that is already laid out gets painted. The space is reserved, but nothing is drawn, pointer events do not hit it, it cannot receive focus, and it is removed from the accessibility tree.
  • opacity controls how transparent the painted box is. At 0 it is fully see-through, but in every other way it is a normal element.
display: nonevisibility: hiddenopacity: 0
Takes up spaceNoYesYes
Receives clicksNoNoYes
Keyboard focusableNoNoYes
Read by screen readersNoNoYes
Children can be shownNoYesNo
Can transition smoothlyOnly with extra workYes, as a stepYes

How to fix it

#

Use display: none to remove something

When content should not be there at all, such as a closed menu, an inactive tab panel or a step not yet reached, use display: none. The HTML hidden attribute does the same job and keeps the state in your markup.


_10
<div id="panel-2" role="tabpanel" hidden>...</div>

If your CSS sets display on that element, it overrides hidden. Add [hidden] { display: none !important; } to your reset if that keeps catching you out.

Use visibility: hidden to keep the layout stable

When you need the space to stay reserved, for example a loading spinner that should not shift the button text, use visibility: hidden. Unlike the others, a child can override it with visibility: visible.


_10
.button[aria-busy="true"] .label {
_10
visibility: hidden;
_10
}

Use opacity for fades, and pair it with visibility

opacity animates smoothly, which makes it the best choice for fade effects. On its own, though, an invisible element still blocks clicks and traps keyboard users on controls they cannot see. Combine it with visibility, and delay the visibility change until the fade has finished.


_11
.dropdown {
_11
opacity: 0;
_11
visibility: hidden;
_11
transition: opacity 200ms ease, visibility 0s linear 200ms;
_11
}
_11
_11
.dropdown.is-open {
_11
opacity: 1;
_11
visibility: visible;
_11
transition: opacity 200ms ease, visibility 0s linear 0s;
_11
}

Because visibility is a discrete property, it flips in one step, so the delay keeps the menu visible while it fades out. Modern browsers can also animate to and from display: none with transition-behavior: allow-discrete and @starting-style, which is worth looking at for new code.

Hide visually but keep it for screen readers

None of the three is right for text that should be read aloud but not shown, such as a label on an icon button. Use a visually hidden utility class instead.


_11
.visually-hidden {
_11
position: absolute;
_11
width: 1px;
_11
height: 1px;
_11
padding: 0;
_11
margin: -1px;
_11
overflow: hidden;
_11
clip-path: inset(50%);
_11
white-space: nowrap;
_11
border: 0;
_11
}

Common pitfalls

#
  • opacity: 0 on interactive elements. Hidden buttons and links can still be clicked and tabbed to. Add visibility: hidden or pointer-events: none plus inert.
  • Expecting display: none to animate. Toggling display alone jumps instantly. Fade with opacity, or use allow-discrete where supported.
  • Hiding focusable content with visibility on a parent, then showing a child. A child set to visibility: visible becomes visible and focusable again, which can surprise you.
  • Using display: none for screen reader text. It hides the text from assistive technology too. Use the visually hidden class.
  • Layout shift. Switching between display: none and visible moves surrounding content. If that jump is unwanted, use visibility.
#
file under:
CSS

quick question

Was this resource helpful?