Sub topic 9
Keyboard use and accessibility
This chapter assumes you can write basic HTML and CSS, and that you know how to select an element and attach an event listener in JavaScript. It builds on the details and summary structure from earlier chapters. Here you will see what the browser gives you for keyboard and screen reader users, and which mistakes remove those benefits.
What the browser provides
A summary element that is the first child of a details element is interactive by default. The browser treats it as a control, so you do not add tabindex, role or key handlers to get basic behavior.
| Feature | Native behavior |
|---|---|
| Focus | `summary` is in the tab order and shows a focus indicator |
| Toggle with keyboard | Enter or Space on a focused `summary` toggles the widget |
| Toggle with pointer | Click or tap on the `summary` toggles the widget |
| Role and state | Browsers expose the `summary` as a disclosure control with an expanded or collapsed state |
| Hidden content | Collapsed content is not rendered, so it is skipped by Tab and by screen readers |
The last row matters. While the widget is closed, links and buttons inside the body cannot receive focus. Keyboard users do not tab through hidden controls.
<details>
<summary>Delivery options</summary>
<p>Standard delivery takes three to five days.</p>
<a href="/delivery">Compare delivery methods</a>
</details>Press Tab until the summary is focused, then press Enter or Space. The paragraph and link appear. Press Tab again and focus moves to the link. Close the widget and the link is no longer reachable.
Common errors
Putting interactive elements inside summary
A summary is already a control. Placing a button, link or form field inside it creates nested interactive content, which the HTML specification does not allow. Behavior differs between browsers, and screen readers may announce the content in confusing ways. Clicking the inner element may toggle the widget, trigger the inner action, or both.
<!-- Avoid -->
<details>
<summary>Account <button type="button">Edit</button></summary>
<p>Account details.</p>
</details>
<!-- Better: keep the action in the body -->
<details>
<summary>Account</summary>
<p>Account details.</p>
<button type="button">Edit</button>
</details>Removing the focus outline
Resetting styles often includes outline: none. On a summary this leaves keyboard users with no visible position. If you want a different look, replace the outline instead of deleting it.
summary:focus-visible {
outline: 3px solid royalblue;
outline-offset: 2px;
}The :focus-visible selector shows the ring for keyboard focus and normally hides it for mouse clicks.
Hiding the marker without a replacement
Setting list-style: none on summary, or using summary::-webkit-details-marker, removes the triangle. Sighted users lose the main cue that the label is expandable. If you remove it, add another cue, such as an icon that rotates in the open state.
summary {
list-style: none;
}
summary::after {
content: "+";
float: right;
}
details[open] summary::after {
content: "−";
}A generated + or − is decorative. The state is already exposed to assistive technology, so the symbol does not need extra markup.
Overriding the role of summary
Do not set role="button" on a summary or add aria-expanded to it. Browsers already expose the state, and a manual aria-expanded can fall out of sync with the real open state. Some assistive technology also stops announcing the disclosure semantics when the role is replaced.
Making the label too vague
The summary text is the accessible name of the control. Screen reader users often list controls out of context, so "Click here" or "More" gives little information. Write labels that stand alone, such as "Shipping information".
Moving focus yourself
If you open a widget from JavaScript, focus does not move. A screen reader user may not learn that anything changed. When opening it in response to an action, such as a link to a section inside a collapsed answer, move focus to a sensible target.
const details = document.querySelector('details');
const summary = details.querySelector('summary');
document.querySelector('#show-policy').addEventListener('click', () => {
details.open = true;
summary.focus();
});Focusing the summary lets the user hear its name and new state. This works because the open property from the previous chapters changes the same state the user would change by hand.
Other limits to know
- Browsers differ in how they search hidden content. Some open a collapsed
detailswhen find-in-page matches text inside it, and some do not. Do not rely on either behavior. - If the whole page depends on one
detailsfor essential content, users who miss it lose information. Keep critical content visible. - Test with the keyboard alone and with one screen reader. Native elements reduce risk, but your CSS can still break them.
Exercise
A teammate styles a FAQ so that each summary has outline: none and list-style: none, and adds role="button" and a hand-maintained aria-expanded attribute. Which changes would you request, and why?
Show answer
Restore a visible focus style, for example with summary:focus-visible, so keyboard users can see where they are. Replace the removed marker with another visible cue, such as a + or − or a rotating arrow tied to details[open]. Remove role="button" and aria-expanded, because the browser already exposes the disclosure role and state, and a manual attribute can become wrong when the widget is toggled.
Exercise
A closed details contains a link. Can a keyboard user tab to the link, and what must they do to reach it?
Show answer
No. Collapsed content is not rendered, so the link is not focusable. The user must focus the summary and press Enter or Space to open the widget, then press Tab to move to the link.