Sub topic 10
display: none vs visibility: hidden vs opacity: 0
This chapter assumes you know the CSS box model, how display: none removes an element from layout, and how to write basic CSS selectors. Here you compare three common ways to hide an element and learn how each one affects layout, clicks, keyboard focus, and assistive technology.
Three ways to hide an element
All three rules below make an element invisible. They differ in what else changes.
.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. Its descendants are removed too, and a child cannot override this.visibility: hiddenkeeps the box and its space but does not paint it. A descendant can opt back in withvisibility: visible.opacity: 0keeps the box and paints it fully transparent. The element still takes space, and its children cannot become visible again, because opacity applies to the element and its children together.
See the difference in layout
The markup below places a hidden box between two visible ones.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hiding compared</title>
<style>
.box { height: 40px; margin: 4px 0; background: steelblue; color: white; }
.a { display: none; }
.b { visibility: hidden; }
.c { opacity: 0; }
</style>
</head>
<body>
<div class="box">First</div>
<div class="box a">display none</div>
<div class="box">Second</div>
<div class="box b">visibility hidden</div>
<div class="box">Third</div>
<div class="box c">opacity zero</div>
<div class="box">Fourth</div>
</body>
</html>You see four blue boxes, but gaps appear after "Second" and "Third". Those gaps are the visibility: hidden and opacity: 0 boxes, which still occupy 40 pixels each. The display: none box leaves no gap, so "First" and "Second" sit next to each other.
Comparison table
| Behavior | `display: none` | `visibility: hidden` | `opacity: 0` |
|---|---|---|---|
| Takes up space | No | Yes | Yes |
| Painted | No | No | Transparent |
| Receives clicks | No | No | Yes |
| Keyboard focusable | No | No | Yes |
| Read by screen readers | No | No | Yes |
| Children can override | No | Yes, with `visibility: visible` | No |
| Animatable | Not directly | Yes, as a discrete switch | Yes, smoothly |
Interaction and accessibility
opacity: 0 is the risky one. A transparent link still receives clicks, so users can activate something they cannot see. A transparent button still takes keyboard focus, so a keyboard user tabs into an invisible control. Screen readers also still announce it.
display: none and visibility: hidden both take the element out of the accessibility tree and the tab order. Assistive technology skips the content, which is the right result when the content is truly unavailable.
Choosing between them
- Use
display: nonewhen the element should not exist for layout or users, such as a closed menu or an inactive tab panel. - Use
visibility: hiddenwhen other content must not shift, for example a placeholder that appears later in a reserved spot. - Use
opacity: 0for fade transitions. After the fade ends, switch tovisibility: hiddenordisplay: noneso the element stops receiving interaction.
A common fade pattern combines two of them:
.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 closes, opacity fades over 0.3 seconds, and visibility flips to hidden only after that delay. When it opens, visibility changes immediately so the fade-in is seen.
Common errors
- Expecting
display: noneto animate. It switches instantly, so a fade-out gets cut off. - Hiding a form field with
opacity: 0and leaving it in the tab order. - Using
visibility: hiddenand wondering why blank space remains. - Setting
visibility: visibleon a child of adisplay: noneparent. It has no effect.
Exercise
A dropdown menu fades out over 0.3 seconds when closed. After it closes, keyboard users must not be able to tab into its links, and the page layout must not change. Which properties should you use, and why?
Show answer
Use opacity for the fade and visibility: hidden for the end state, with the visibility change delayed by 0.3s. Opacity gives the smooth fade. Visibility hidden then removes the links from the tab order and the accessibility tree while keeping the reserved space, so the layout does not shift. opacity: 0 alone would leave the links focusable, and display: none would change the layout and cannot be transitioned.
Exercise
A parent has display: none and its child has visibility: visible. Is the child shown?
Show answer
No. The parent generates no box, so its descendants generate none either. The child's visibility value is irrelevant.