Sub topic 11
Hidden content, focus, and assistive technology
This chapter assumes you know the difference between display: none, visibility: hidden, and opacity: 0 from the previous chapter. It also assumes you can write basic HTML and CSS, and that you know what the keyboard Tab key does on a page. Here you will look at what each hiding method means for people who use screen readers or the keyboard.
Two trees, not one
A browser builds more than the picture you see. It also builds an accessibility tree, a simplified list of the page's elements with their roles (button, heading, link), names, and states. Assistive technology (AT), such as a screen reader, reads this tree and not the pixels.
A third thing matters too: the focus order, the sequence of elements the Tab key can reach. A visible page, an accessibility tree, and a focus order are three separate outputs. Each hiding technique affects them differently. That is why "it looks hidden" doesn't tell you whether it is hidden for everyone.
What `display: none` does for assistive technology
When an element has display: none, the browser generates no box for it or for any of its descendants. The same removal applies to the accessibility tree and to the focus order:
- Screen readers do not announce the element or its children.
- Links, buttons, and form fields inside it cannot receive focus, so Tab skips them.
- Descendants cannot opt back in. A child with
display: blockstays hidden because its parent generates no box.
This is usually what you want for content that is truly not available, such as a closed menu or an inactive tab panel. It is also the safest default: nobody can tab into something they cannot see.
visibility: hidden behaves the same way for AT and focus. The difference is that it keeps the element's space in layout. Unlike display: none, a descendant can set visibility: visible and reappear.
`opacity: 0` hides nothing from AT
An element with opacity: 0 is still laid out, still in the accessibility tree, and still focusable. It also still receives mouse clicks. Three problems follow:
- A screen reader reads the content aloud, even though sighted users see nothing.
- A keyboard user tabs onto a control they cannot see, and the focus ring may be invisible too.
- A mouse user may click an invisible control and trigger an action.
<style>
.ghost { opacity: 0; }
</style>
<button class="ghost">Delete account</button>Here the button is invisible, but Tab lands on it and a screen reader announces "Delete account, button". Use opacity: 0 for fade animations, but pair it with visibility: hidden or display: none once the fade ends.
.panel {
opacity: 0;
visibility: hidden;
transition: opacity 0.2s, visibility 0s linear 0.2s;
}
.panel.open {
opacity: 1;
visibility: visible;
transition: opacity 0.2s, visibility 0s;
}Because visibility changes after the delay on close, the fade stays visible while it runs. When it finishes, the panel leaves the accessibility tree and the focus order.
Comparing the techniques
| Technique | Takes space | Painted | In accessibility tree | Focusable |
|---|---|---|---|---|
| `display: none` | No | No | No | No |
| `visibility: hidden` | Yes | No | No | No |
| `opacity: 0` | Yes | Invisible | Yes | Yes |
| `hidden` attribute | No (via default `display: none`) | No | No | No |
| `aria-hidden="true"` | Yes | Yes | No | Yes |
| Visually-hidden class | No visible footprint | Not seen | Yes | Yes |
| `inert` attribute | Yes | Yes | No | No |
The last three rows are not display values, but you will meet them whenever you hide content on purpose.
The `hidden` attribute and a common override
The HTML hidden attribute is the semantic way to say "this is not relevant right now". Browsers implement it with a default style rule of display: none in their built-in stylesheet. That rule has low priority, so any author rule that sets display on the same element wins.
<style>
.card { display: flex; }
</style>
<div class="card" hidden>I am still visible.</div>The .card rule beats the browser's [hidden] rule, so the card is shown. A defensive fix is to restore the intent:
[hidden] {
display: none !important;
}This is one of the few places where !important is a reasonable choice, because hidden should always win.
Hiding visually but not from AT
Sometimes you want the opposite: content that sighted users do not see but screen readers announce. A typical case is an icon-only button that needs a text label. display: none would remove the text from the accessibility tree, so you use a visually-hidden (also called "screen reader only") class instead.
<style>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
border: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
</style>
<button type="button">
<span aria-hidden="true">×</span>
<span class="visually-hidden">Close dialog</span>
</button>The "×" is decorative, so aria-hidden="true" stops a screen reader from saying "multiplication sign". The hidden text supplies the real name, "Close dialog". The text is taken out of normal flow with position: absolute, which is why this class does not leave a gap in the layout.
`aria-hidden` is not a focus tool
aria-hidden="true" removes an element from the accessibility tree only. It does not change painting, layout, or focus. If you put it on something focusable, you create a control that keyboard users can reach and screen reader users cannot perceive. A screen reader may announce nothing, or something confusing, when focus lands there.
The inert attribute is the modern answer when you want content visible but unavailable, such as the page behind an open modal dialog. It makes the subtree unfocusable and unclickable and removes it from the accessibility tree, while leaving it painted.
Referencing hidden content
display: none has one useful exception. An element can be named or described by another element through aria-labelledby or aria-describedby, and those references can point to hidden elements. The browser still uses the hidden element's text to compute the name.
<style>
#hint { display: none; }
</style>
<label for="pw">Password</label>
<input id="pw" type="password" aria-describedby="hint">
<p id="hint">Use at least 12 characters.</p>A screen reader announces the hint when the field gains focus, even though sighted users never see it. If you want everyone to see it, drop the display: none rule. Hiding information from sighted users that others receive is a design decision, not a trick to apply by default.
Focus and elements that disappear
If the element that has focus becomes display: none, the browser can no longer keep focus on it. In practice, document.activeElement falls back to the body, and the next Tab press starts again from the top of the page. For a keyboard user this feels like being thrown out of the interface.
Whenever you hide something the user may be inside, move focus on purpose. A closing panel should return focus to the control that opened it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Disclosure panel</title>
<style>
[hidden] { display: none !important; }
</style>
</head>
<body>
<button id="toggle" type="button" aria-expanded="false" aria-controls="panel">
Show details
</button>
<div id="panel" hidden>
<p>Extra details go here.</p>
<button id="close" type="button">Close</button>
</div>
<script>
const toggle = document.getElementById("toggle");
const panel = document.getElementById("panel");
const close = document.getElementById("close");
function setOpen(open) {
panel.hidden = !open;
toggle.setAttribute("aria-expanded", String(open));
toggle.textContent = open ? "Hide details" : "Show details";
}
toggle.addEventListener("click", () => {
setOpen(panel.hidden);
});
close.addEventListener("click", () => {
setOpen(false);
toggle.focus();
});
</script>
</body>
</html>Three details matter here:
- The
hiddenattribute and the[hidden]rule together keep the closed panel out of layout, the accessibility tree, and the tab order. aria-expandedtells a screen reader whether the panel is open, so the button announces its state.- The Close button calls
toggle.focus()after hiding the panel. Without it, focus would be lost when the panel disappears.
Opening hidden content on demand
A related edge case is the reverse: moving focus into something that was just shown. If a panel changes from display: none to visible, the browser has to build its box before it can focus anything inside it. Calling .focus() in the same script step as the style change works in modern browsers, because the browser updates style when focus is requested. If the element is still hidden by a transition, for example visibility waiting on a delay, the focus call silently fails. Delay the call until the transition ends, or avoid a delayed visibility, for opening transitions.
A note on changing `display` and semantics
Changing display on elements with built-in structure, such as lists, tables, and headings, can change how some browsers expose them to AT. Historically, setting a table's rows or a list's items to a non-default display, including display: block, caused some browser and screen reader combinations to drop table or list semantics. Behavior has improved over time but still varies. If you restyle structural elements, test with a screen reader, and add an explicit role only if the semantics are actually lost.
Choosing a technique
- Content is irrelevant for everyone right now: use
hiddenordisplay: none. - Content must keep its space but be unavailable: use
visibility: hidden. - Content fades in or out: animate
opacity, then switch tovisibility: hiddenordisplay: nonewhen finished. - Content is for AT only: use a visually-hidden class, not
display: none. - Content is visible but should be unavailable, such as a background behind a modal: use
inert. - Decorative content that is visible and non-interactive: use
aria-hidden="true".
Exercise
A fixed-height toolbar contains a "Delete" button that has opacity: 0 until you hover over the toolbar. Why is this a problem for a keyboard user and a screen reader user, and what is a better approach?
Show answer
With opacity: 0 the button stays in the focus order and the accessibility tree. A keyboard user tabs onto an invisible control, and a screen reader announces "Delete, button" with nothing visible. A better approach is to show the button on both :hover and :focus-within of the toolbar, so keyboard focus reveals it. Alternatively keep it visible at all times. If it must be unavailable in some state, use visibility: hidden or display: none so it also leaves the focus order and accessibility tree.
Exercise
You mark a sidebar as aria-hidden="true" while a modal is open, but keyboard users can still tab into the sidebar links. Why, and how do you fix it?
Show answer
aria-hidden only removes the element from the accessibility tree. It does not affect focus, so the links stay reachable by Tab and can be focused without being announced properly. Fix it by adding the inert attribute to the sidebar while the modal is open, which makes the subtree unfocusable, unclickable, and absent from the accessibility tree. As a fallback, set visibility: hidden or display: none on the sidebar.