Sub topic 11
Hidden content, focus, and assistive technology
This chapter assumes you know basic HTML, how to write CSS rules, and the box model. It also assumes you have read the chapter comparing display: none, visibility: hidden, and opacity: 0. Here the question is not how each method looks. The question is what happens to people who use a keyboard or a screen reader.
Two trees, not one
A browser builds an accessibility tree from the page. Screen readers and other assistive technology read this tree, not the pixels. Keyboard focus is handled separately, by the browser's list of focusable elements. A hiding method can affect the visual layout, the accessibility tree, and focus in different ways.
Here are the three methods from the earlier code example:
.hidden-a { display: none; }
.hidden-b { visibility: hidden; }
.hidden-c { opacity: 0; }| Method | Takes up space | Read by screen readers | Reachable with Tab |
|---|---|---|---|
| `display: none` | No | No | No |
| `visibility: hidden` | Yes | No | No |
| `opacity: 0` | Yes | Yes | Yes |
The first two methods remove the element from both the accessibility tree and the tab order. The third does neither. The element is only fully transparent.
The opacity: 0 trap
An element with opacity: 0 is invisible, but a keyboard user can still tab to it. Focus lands on something they cannot see. A screen reader also still announces it. This is a common bug in custom dropdown menus and modal dialogs that are "hidden" with opacity alone.
<button class="ghost">Delete account</button>.ghost {
opacity: 0;
}A sighted keyboard user pressing Tab will focus this button and see nothing. If they press Enter, the action runs. If you want an element to fade in or out, combine opacity with another method so it is not interactive while invisible.
Hiding for everyone
Use display: none when the content should be gone for every user. This includes sighted users, screen reader users, and keyboard users. Typical cases are a closed menu, an inactive tab panel, or a collapsed section.
Links, buttons, and form fields inside a display: none element cannot be focused. You do not need to set tabindex="-1" on them. When you switch the element back to a visible display value, they become focusable again.
Hiding only visually
Sometimes you want a label that screen readers announce but sighted users do not see. For example, an icon-only search button needs a text name. display: none would remove that text from the accessibility tree too, so it does not work here. The usual fix is a "visually hidden" class that shrinks the element to almost nothing without removing it from layout rules that assistive technology respects:
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}<button type="submit">
<span class="visually-hidden">Search</span>
<svg aria-hidden="true" width="16" height="16"></svg>
</button>The screen reader announces "Search, button". The aria-hidden="true" attribute on the icon hides it from the accessibility tree only. It does not change how it looks, and it does not remove it from the tab order, so use it only on non-interactive content.
Which method for which goal
Common errors
- Using
opacity: 0to hide a menu, which leaves invisible focusable items. - Using
display: noneon a label that screen readers need. - Adding
aria-hidden="true"to a button or link. - Toggling
displayand assuming the focus position is kept. If the focused element becomesdisplay: none, focus is lost and moves back to the page. Move focus on purpose when you hide something the user was using.
Exercise
A fade-out animation ends with opacity: 0 on a modal's close button. Keyboard users report that Tab still stops on the invisible button. Why does this happen, and what is a simple fix?
Show answer
opacity: 0 only makes the element transparent. It stays in layout, in the accessibility tree, and in the tab order. Once the fade finishes, also set visibility: hidden or display: none on the modal so the button leaves the tab order and the accessibility tree.
Exercise
An icon-only button needs the accessible name "Close". Why is <span style="display: none">Close</span> the wrong choice, and what should you use instead?
Show answer
display: none removes the text from the accessibility tree, so screen readers do not announce it. Use a visually hidden class, which keeps the text available to assistive technology while hiding it from sight.