Visibility & Hiding
Hide elements in different ways.
Syntax
visibility: hidden; display: none;There are several ways to hide an element, each with a different effect:
| Method | Takes up space? |
|---|---|
display: none | No |
visibility: hidden | Yes |
opacity: 0 | Yes (still clickable) |
Example
Loading editor…
Press Run to see the result.
When to use it
- A developer uses visibility: hidden on a tooltip to hide it while keeping its space in the layout, preventing surrounding elements from jumping.
- A designer uses opacity: 0 with transition on a hover overlay so it fades in smoothly without removing it from layout flow.
- A developer uses display: none to remove an error message from the page entirely after it is dismissed, reclaiming its space.
More examples
visibility hidden versus display none
Contrasts visibility: hidden (preserves layout space) with display: none (removes the element from flow entirely).
.placeholder {
visibility: hidden; /* hidden but still takes space */
}
.removed {
display: none; /* removed from flow, no space */
}Opacity zero for animated hide
Uses opacity: 0 with transition to fade a tooltip in and out while keeping its layout position, avoiding reflow.
.tooltip {
opacity: 0;
transition: opacity 0.2s ease;
pointer-events: none;
}
.tooltip.visible {
opacity: 1;
pointer-events: auto;
}Screen-reader-only content
The standard utility class that hides content visually but keeps it accessible to screen readers and keyboard navigation.
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
Discussion