There are three common ways to hide something, and they are not the same. Pick by asking two questions: should it keep its space? and should it still be reachable?
display: none | visibility: hidden | opacity: 0 | |
|---|---|---|---|
| Visible | No | No | No |
| Keeps its space | No | Yes | Yes |
| Clickable / focusable | No | No | Yes |
| Read by screen readers | No | No | Yes |
| Can be animated | No | Partly | Yes |
<style>
.row { display: flex; gap: 8px; }
.row div { flex: 1; padding: 10px; background: #fdebd9; border: 1px solid #d94a00; }
.b { display: none; }
.c { visibility: hidden; }
.d { opacity: 0; }
</style>
<div class="row">
<div>A (normal)</div>
<div class="b">B display:none</div>
<div class="c">C visibility:hidden</div>
<div class="d">D opacity:0</div>
<div>E (normal)</div>
</div>
B has disappeared and the row has re-flowed. C and D left blank spaces. D is invisible but still exists: you could click on it.
Which one to use
- Remove it from the page (a closed menu, content for other screen sizes):
display: none. - Hide it but keep the layout steady:
visibility: hidden. - Fade it in or out:
opacity, and addvisibilityorpointer-events: nonewhen it is hidden so that it cannot be clicked.
Hidden content that is still reachable
opacity: 0 can still be tabbed to and clicked. Hiding a form field this way creates a “ghost” control that keyboard users find. Use display: none or visibility: hidden unless you have a reason.