Sub topic 10
display: none vs visibility: hidden vs opacity: 0
This chapter assumes you know basic HTML, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). It also assumes you have met display: none as a way to remove an element from the page. Here you will compare it with two other common ways to hide an element, and learn how each one affects layout, events, focus, and assistive technology.
Three ways to hide, three different results
All three declarations make an element invisible on screen. They differ in what happens to everything else.
.hidden-a { display: none; }
.hidden-b { visibility: hidden; }
.hidden-c { opacity: 0; }display: noneremoves the element from layout. It generates no box, so it takes no space, and its descendants generate no boxes either.visibility: hiddenkeeps the box in layout. The space stays reserved, but nothing is painted.opacity: 0keeps the box in layout and paints it fully transparent. The element is still there, just see-through.
A quick way to remember it: display: none means "this box does not exist," while the other two mean "this box exists but you cannot see it."
Seeing the difference in a page
The following page has three boxes between two paragraphs. Each box is hidden with a different method. Open it in a browser and compare the gaps.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hiding methods</title>
<style>
.box {
width: 200px;
height: 40px;
margin: 8px 0;
background: steelblue;
color: white;
}
.a { display: none; }
.b { visibility: hidden; }
.c { opacity: 0; }
</style>
</head>
<body>
<p>Before</p>
<div class="box a">A: display none</div>
<div class="box b">B: visibility hidden</div>
<div class="box c">C: opacity 0</div>
<p>After</p>
</body>
</html>Box A leaves no trace. Boxes B and C each leave an empty 40px slot with 8px margins. B and C look identical on screen, but they are not the same, as the next sections show.
Comparison table
| Behavior | `display: none` | `visibility: hidden` | `opacity: 0` |
|---|---|---|---|
| Takes up space in layout | No | Yes | Yes |
| Painted on screen | No | No | Yes, but transparent |
| Receives mouse clicks | No | No | Yes |
| Can receive keyboard focus | No | No | Yes |
| Read by screen readers | No | No (generally) | Yes |
| Children can override it | No | Yes, with `visibility: visible` | No |
| Animatable with transitions | Not directly | Yes (discrete switch) | Yes (smooth) |
| Inherited by children | No, but children vanish with the parent | Yes | No, but children fade with the parent |
The rest of this chapter explains the rows that are not obvious.
Layout effects
Because display: none generates no box, the surrounding content reflows as if the element was never in the markup. Siblings move up. Margins that would have collapsed with the element's margins collapse with each other instead. Counters and :nth-child() still count the element, because selectors work on the DOM tree, not on boxes. Only CSS counters from counter-increment on the element itself are skipped, since a display: none element does not increment them.
With visibility: hidden and opacity: 0, the box still takes part in layout. Its width, height, margin, and padding all apply. This is useful when you want to toggle something without shifting the rest of the page, for example an error icon beside a form field that appears on validation.
.field-error-icon {
display: inline-block;
width: 1rem;
height: 1rem;
visibility: hidden;
}
.field.invalid .field-error-icon {
visibility: visible;
}The field never changes width when the icon appears, so nothing jumps.
Inheritance and the child override
visibility is an inherited property. Children get the parent's value unless they set their own. That lets a child reappear inside a hidden parent.
<div class="parent">
<p>Hidden by inheritance</p>
<p class="show">Visible again</p>
</div>.parent { visibility: hidden; }
.parent .show { visibility: visible; }The second paragraph is painted even though its parent is hidden. This does not work with the other two methods. If a parent has display: none, no descendant can generate a box, whatever the descendant sets. If a parent has opacity: 0, the whole subtree is composited as one group at zero opacity, and a child's opacity: 1 cannot raise it above its parent's group.
Events, focus, and interaction
This is where opacity: 0 causes the most surprises. A fully transparent element is still a normal, interactive element.
- Clicks land on it. A transparent button laid over other content blocks clicks to whatever is underneath.
- Links and form controls inside it stay in the tab order. A keyboard user can tab to something they cannot see.
:hover,:focus, and event listeners all keep working.
visibility: hidden and display: none both prevent pointer events and remove the element from the tab order. The element cannot be focused, even with tabindex.
If you want an opacity: 0 element to stop reacting to the mouse, add pointer-events: none. That does not remove it from the tab order or from the accessibility tree, so it is only a partial fix.
.tooltip {
opacity: 0;
pointer-events: none;
transition: opacity 0.2s;
}
.trigger:hover + .tooltip {
opacity: 1;
pointer-events: auto;
}Hidden content and assistive technology
Assistive technology such as a screen reader builds its view of the page from the browser's accessibility tree.
display: noneremoves the element and its descendants from the accessibility tree. Screen readers do not announce them.visibility: hiddenalso removes the element from the tree in current browsers, so it is not announced either.opacity: 0leaves the element in the tree. A screen reader reads its text aloud as usual.
This gives you a way to choose what each audience sees:
| Goal | Technique |
|---|---|
| Hide from everyone, including screen readers | `display: none` or the `hidden` attribute |
| Hide visually, but keep readable by screen readers | A "visually hidden" utility class (see below) |
| Hide from screen readers only | `aria-hidden="true"` on the element |
| Transparent, but still present for everyone | `opacity: 0` |
Do not use opacity: 0 to hide content you do not want read. Also do not use display: none for a label that sighted users do not need but screen reader users do. The usual pattern for the second case is a utility class that shrinks the element to a one-pixel clipped box without removing it from the accessibility tree:
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}Transitions and animation
display is not animatable in the traditional sense, so a rule like transition: display 0.3s does nothing in older engines. The element jumps between none and its visible value at once. The consequence is that fading an element out with opacity and then removing it with display: none needs two steps.
visibility can be transitioned because it is treated as a discrete switch. When going from hidden to visible, it flips at the start of the transition. When going from visible to hidden, it flips at the end. This makes it a good partner for opacity:
.panel {
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, visibility 0s linear 0.3s;
}
.panel.open {
opacity: 1;
visibility: visible;
transition: opacity 0.3s, visibility 0s;
}When the panel opens, visibility flips to visible immediately and the opacity fades in. When it closes, the opacity fades out over 0.3 seconds, and visibility flips to hidden only after that delay. The result is a smooth fade, with a closed panel that cannot be clicked or focused. The panel still occupies its layout space, though. If it must also leave the layout, you need display: none, and newer browsers support transition-behavior: allow-discrete for that. Check browser support before relying on it.
Performance notes
Changing display forces the browser to rebuild parts of the box tree and recalculate layout for the affected area. Changing visibility and opacity leaves the layout alone, so the work is mostly painting or compositing. For opacity, browsers can often hand the work to the GPU, which makes it the cheapest property to animate.
A hidden element with display: none is also cheaper while it stays hidden, since the browser does no layout or painting for it. Many large, hidden subtrees therefore cost less as display: none than as visibility: hidden, where layout still runs.
Common mistakes
- Using
opacity: 0to "close" a menu. The invisible links stay clickable and focusable. Pair it withvisibility: hiddenorpointer-events: none, or usedisplay: nonewhen no animation is needed. - Using
display: noneand expecting JavaScript layout reads to work. Properties such asoffsetWidthreturn0for an element with no box. Measure it after making it visible, or usevisibility: hiddenwithposition: absoluteto measure without showing it. - Using
visibility: hiddenand wondering about the blank gap. The space is reserved on purpose. Switch todisplay: noneif the gap is unwanted. - Hiding important text with
display: nonefor screen reader users. Use the visually hidden pattern instead. - Expecting a child to escape
display: noneoropacity: 0. Onlyvisibilityallows the child override.
Choosing between them
- Do you want the element gone, with no space, and unavailable to everyone? Use
display: none(or thehiddenattribute). - Do you want to keep the space but make it unavailable? Use
visibility: hidden. - Do you want a fade effect? Animate
opacity, and combine it withvisibility: hiddenat the end so the element is not interactive. - Do you want it invisible but still interactive, such as a custom file input drawn over a styled button? Use
opacity: 0, and be sure that is intended.
Exercise
A dropdown menu fades out with opacity: 0 and nothing else. Keyboard users report that pressing Tab lands on invisible links, and screen readers still read the menu. Which one-property change to the closed state fixes both problems, and why does that property also need a transition entry with a delay to keep the fade-out smooth?
Show answer
Add visibility: hidden to the closed state. It removes the menu from the tab order and the accessibility tree while keeping its space. Because visibility flips at the end of a visible-to-hidden transition only if you delay it, add visibility 0s linear 0.3s (matching the opacity duration) to the transition. Without the delay, the menu would become hidden at once and the fade would never be seen.
Exercise
A page has three sibling boxes of 40px height with 8px vertical margins. You hide the middle one first with display: none, then with visibility: hidden. How does the layout differ between the two cases?
Show answer
With display: none, the middle box generates no box, so the page is shorter by that box's height and its margins, and the other two boxes sit next to each other. With visibility: hidden, the box keeps its 40px height and 8px margins, so an empty gap remains where it was.