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: noneremoves the element from the layout entirely, as if it had never been there.visibility: hiddenmakes 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:
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 */
}
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:
| Behaviour | display: none | visibility: hidden | opacity: 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 uses | Menus, tab panels, collapsed content | Keeping layouts stable, fades | Fade 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:
.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 asallow-discretein thetransitionshorthand, lets properties that can't blend, likedisplay, take part in a transition. On hiding,displaystays visible until the fade has finished.@starting-stylesets 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
hiddenattribute (<div hidden>) hides an element just likedisplay: none, but in the HTML itself. It's handy for content that JavaScript shows and hides. Note that any CSS that setsdisplayon 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: hiddenhides 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 thehiddenattribute) when the element shouldn't take up any space: menus, tab panels, collapsed sections and anything that appears only in certain situations. - Use
visibility: hiddenwhen 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
opacitytogether withvisibilityfor 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.