Sub topic 9
display: none
This chapter assumes you know basic HTML, how to write CSS rules with selectors and properties, and the box model (content, padding, border, margin). Here you will learn what display: none does, how it differs from other ways of hiding content, and what it means for keyboard users and screen readers.
What display: none does
display: none tells the browser not to generate a box for the element. The element stays in the DOM (the browser's tree of the page), but it takes no space and paints nothing. Its descendants are removed too. A child cannot override this with its own display value, because the child has no parent box to sit in.
<p>First paragraph</p>
<p class="gone">Second paragraph</p>
<p>Third paragraph</p>.gone {
display: none;
}The first and third paragraphs render as if they were neighbors. The second one leaves no gap. Margins, padding, and borders of the hidden element vanish with it.
Hiding methods compared
Three common techniques look similar on screen but behave differently.
.hidden-a { display: none; }
.hidden-b { visibility: hidden; }
.hidden-c { opacity: 0; }| Property | Takes space in layout | Visible | Receives clicks | Reachable by keyboard focus | Read by screen readers |
|---|---|---|---|---|---|
| `display: none` | No | No | No | No | No |
| `visibility: hidden` | Yes | No | No | No | No |
| `opacity: 0` | Yes | No | Yes | Yes | Yes |
The last row surprises many people. An element with opacity: 0 is only fully transparent. It can still be clicked, tabbed to, and announced. Use it for fade effects, not for removing content.
With visibility: hidden, a child can set visibility: visible and reappear inside a hidden parent. With display: none, that is impossible.
Toggling with JavaScript
A common pattern is to add or remove a class that sets display: none.
<button id="toggle" aria-expanded="false" aria-controls="details">Show details</button>
<p id="details" class="gone">Extra information goes here.</p>
<script>
const button = document.getElementById("toggle");
const details = document.getElementById("details");
button.addEventListener("click", () => {
const hidden = details.classList.toggle("gone");
button.setAttribute("aria-expanded", String(!hidden));
button.textContent = hidden ? "Show details" : "Hide details";
});
</script>Pair this with the .gone { display: none; } rule from earlier. The aria-expanded attribute tells assistive technology whether the controlled content is currently shown.
Hidden content, focus, and assistive technology
Browsers remove display: none elements from the accessibility tree, the structure screen readers use. This has three effects:
- Screen readers skip the content entirely.
- Links, buttons, and inputs inside it cannot receive focus, so Tab skips them.
- Form controls inside it are still submitted with the form, unless they are disabled.
This is the right behavior for content that is truly not available, such as a closed menu or an inactive tab panel. It is the wrong tool when you want content that is visible to screen readers but not to sighted users, such as a label for an icon button. For that, use a "visually hidden" utility class that shrinks the element to a 1px clipped box instead.
The HTML hidden attribute behaves like display: none by default. If your CSS sets a display value on the same element, that rule can override hidden, so check the result.
Common mistakes
- Using
display: noneand expecting a CSS transition to animate it. Thedisplayproperty was not animatable in older browsers. Newer browsers support it with extra rules, but a simple fade is more reliably built withopacityandvisibility. - Using
opacity: 0to hide a menu, which leaves invisible links that still catch clicks and keyboard focus. - Hiding content with
display: nonewhen it needs to be measured by JavaScript. A hidden element reports a width and height of 0.
Exercise
A dropdown menu is hidden with opacity: 0. Sighted users see nothing, but keyboard users report that Tab seems to "stop" on invisible links. Why does this happen, and which change fixes it while keeping the layout space the menu would use?
Show answer
opacity: 0 only makes the element transparent. The links remain in the layout, in the tab order, and in the accessibility tree, so focus lands on them. Changing the hiding rule to visibility: hidden keeps the space, removes the links from the tab order, and hides them from screen readers. If the menu does not need to keep its space, display: none also works.
Exercise
Given <div class="box"><p class="note">Hi</p></div> and the CSS .box { display: none; } .note { display: block; }, is the note visible? Explain.
Show answer
No. When the parent has display: none, it generates no box, and none of its descendants generate boxes either. The child's own display: block cannot bring it back.