Sub topic 9
Keyboard use and accessibility
This chapter covers how details and summary behave for people who don't use a mouse. It also covers what assistive technology is told about the widget, and what breaks that behavior. It assumes you can already build a basic disclosure widget, set open, and style summary with CSS. It also assumes you know how to attach an event listener in JavaScript.
What you get for free
A native disclosure is accessible mostly because the browser does the work. When you write <details><summary>…</summary>…</details>, the browser provides the following:
- The
summaryis in the tab order, so Tab and Shift+Tab reach it with notabindex. - Enter and Space toggle the widget when the
summaryhas focus. - The
summarygets a default focus indicator. - Assistive technology receives a control with a name (the summary text) and an expanded or collapsed state.
- When the widget is closed, the hidden content is removed from the accessibility tree and from the tab order. Links and buttons inside a closed
detailscan't be reached by Tab.
A custom widget built from div elements has to rebuild all of this by hand. It needs tabindex="0", a keydown handler for two keys, role="button", and aria-expanded kept in sync. It also needs aria-controls, and hiding must be done in a way that removes content from the tab order. Each of those is a place to make a mistake.
<details>
<summary>Delivery options</summary>
<p>Standard delivery takes three to five days.</p>
<a href="/express">See express delivery</a>
</details>With this markup, a keyboard user presses Tab to reach "Delivery options". Pressing Enter or Space opens it. The next Tab press lands on the "See express delivery" link. If the widget is closed, that same Tab press skips the link.
What the keyboard does
The behavior is small and consistent across current browsers.
| Key | Focus is on | Result |
|---|---|---|
| Tab | Anything before the widget | Focus moves to the `summary` |
| Enter | `summary` | Toggles `open` |
| Space | `summary` | Toggles `open` |
| Tab | `summary` of an open widget | Focus moves into the body, if it has focusable content |
| Tab | `summary` of a closed widget | Focus skips the body and moves on |
| Escape | Anywhere | Nothing. Native `details` does not close on Escape |
| Arrow keys | `summary` | Nothing. They scroll the page as usual |
Escape and the arrow keys do nothing because details is a disclosure, not a menu or a tab list. The WAI-ARIA Authoring Practices describe arrow-key movement between headers as optional for accordions. Native details doesn't offer it, even when you group elements with a shared name. Users move between items with Tab only.
Each toggle also fires the toggle event, whether it came from the keyboard, the mouse, or script. You don't need separate handlers for keyboard users, and you shouldn't add click handlers to the summary for the sake of the keyboard. Pressing Enter or Space on a summary produces a click event, so one click listener would see both input types.
What assistive technology is told
Browsers expose the details element as a group and the summary as a control that can be expanded or collapsed. A screen reader typically announces something like "Delivery options, collapsed, button" or "disclosure triangle, collapsed". The exact wording and role name vary by browser and screen reader, but the pieces are the same: the name, the control type, and the state. After a toggle, the state announcement changes to "expanded".
This has three consequences for how you write the markup.
- The summary text is the accessible name. Make it specific. "More" or "Click here" is announced out of context in a screen reader's list of controls. "Shipping and returns" or "Show order history" is not.
- The marker (the triangle) is decoration. It isn't part of the name, and the state is exposed without it. You can restyle or replace it freely, as long as the state stays visible to sighted users too.
- You don't need ARIA on the element.
aria-expandedon asummaryis redundant, and it can go out of sync with the realopenstate. Don't add it.
<!-- Redundant and fragile: do not do this -->
<details>
<summary role="button" aria-expanded="false" tabindex="0">Shipping</summary>
<p>Orders ship within two business days.</p>
</details>
<!-- Do this -->
<details>
<summary>Shipping</summary>
<p>Orders ship within two business days.</p>
</details>Overriding the role with role="button" can remove the expanded/collapsed information that the browser would have supplied. A static aria-expanded="false" then tells users the widget is closed after they have opened it.
Content rules that affect accessibility
The summary is the control, so what you put inside it matters.
- Plain text or phrasing content works best. Emphasis, an icon with a text alternative, or a short
spanare fine. - Do not put interactive elements inside
summary. A link or button inside the summary creates a control inside a control. Clicking it activates the link or button instead of toggling the widget, and screen readers may announce the combination confusingly. If you need a "Delete" button next to a disclosure label, place it outside thesummary, as a sibling in a wrapper. - Headings need care.
summarymay contain heading content, so<summary><h3>Billing</h3></summary>is valid. The reverse,<h3><details>…, is not a good structure, and puttingsummaryinside a heading is invalid. Some screen reader and browser combinations drop the heading semantics or the button semantics when a heading sits inside asummary. If your page navigation relies on headings, test with the screen readers your audience uses. Alternatively, put a heading before thedetailsand keep the summary as the control. - Only the first
summarychild counts. Ifsummaryis missing, the browser inserts a default label (usually "Details"). That label is generic and not translated to your page's content. Always supply your own.
Styling without breaking accessibility
Most accessibility problems with details come from CSS. Three rules cover most of them.
Keep a visible focus indicator. Because summary is a keyboard control, it needs a clearly visible focus style. Removing the outline with outline: none and giving nothing in return makes the widget unusable for people who track the focus ring. Use :focus-visible to show a strong ring for keyboard users without showing it after mouse clicks.
Keep the target large enough. WCAG 2.2 (success criterion 2.5.8) expects pointer targets of at least 24 by 24 CSS pixels, with limited exceptions. A summary with padding across the whole row is easy to hit with a finger or a pointer. Put the padding on the summary, not on the details, so that the whole padded area is the click target.
Don't hide the marker without replacing it. Users need to see that the row expands. If you remove the default marker, add your own. Hide the default in both engine families: list-style: none for the standard ::marker, and the WebKit-specific pseudo-element for older Safari.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Accessible disclosure</title>
<style>
details {
border: 1px solid #767676;
border-radius: 4px;
margin-block: 0.5rem;
}
summary {
cursor: pointer;
padding: 0.75rem 1rem;
list-style: none;
}
summary::-webkit-details-marker {
display: none;
}
summary::before {
content: "▸";
content: "▸" / "";
display: inline-block;
margin-right: 0.5rem;
transition: transform 0.15s;
}
details[open] > summary::before {
transform: rotate(90deg);
}
summary:focus-visible {
outline: 3px solid #1a4fd6;
outline-offset: 2px;
}
details > :not(summary) {
padding: 0 1rem 0.75rem;
}
@media (prefers-reduced-motion: reduce) {
summary::before {
transition: none;
}
}
</style>
</head>
<body>
<details>
<summary>Shipping and returns</summary>
<p>Orders ship within two business days. Returns are accepted for 30 days.</p>
<a href="/returns">Start a return</a>
</details>
</body>
</html>Some details of this stylesheet are worth explaining:
content: "▸" / "";is the form ofcontentwith alternative text. The empty string after the slash tells supporting browsers to expose nothing for this generated glyph, so screen readers don't read "black right-pointing small triangle". The line before it is a fallback for browsers that don't understand the slash syntax.border: 1px solid #767676is a gray that meets the 3:1 contrast ratio that WCAG asks for on user interface boundaries when set against white.- Changing
summarytodisplay: flexordisplay: gridremoves the default marker in several browsers, because the marker depends on thelist-itemdisplay type. If you want a flex layout, use the custom marker above. - The
prefers-reduced-motionrule turns off the rotation for users who have asked their operating system to reduce animation.
Finding and linking into closed content
A closed details hides its body. That has side effects for people who search and navigate by URL, and some of them depend on the browser.
- Chromium-based browsers open a closed
detailswhen find-in-page matches text inside it, and when the URL fragment points to an element inside it. Other browsers may not. - You can't rely on that. If a page contains information that users will often search for, such as prices or contact details, consider leaving the widget open by default, or add script that opens the right
detailswhenlocation.hashpoints inside it.
function openForHash() {
if (!location.hash) return;
const target = document.getElementById(decodeURIComponent(location.hash.slice(1)));
if (!target) return;
let ancestor = target.closest('details');
while (ancestor) {
ancestor.open = true;
ancestor = ancestor.parentElement.closest('details');
}
target.scrollIntoView();
}
window.addEventListener('DOMContentLoaded', openForHash);
window.addEventListener('hashchange', openForHash);The loop walks up through nested details elements, because an element inside two closed widgets needs both opened before it can be seen.
Managing focus when script is involved
The native keyboard behavior is complete, so most pages need no focus code at all. Problems start when your script changes open.
When the user toggles the widget from the summary, focus stays on the summary. That is fine. If script closes the widget while focus is on something in the body, such as a "Close" button, the focused element becomes hidden. Browsers then drop focus to the document body, and a keyboard user has to tab from the top of the page again. The fix is to move focus to the summary yourself.
<details id="filters">
<summary>Filters</summary>
<label>Minimum price <input type="number" id="min" min="0"></label>
<button type="button" id="close-filters">Close filters</button>
</details>
<script>
const filters = document.getElementById('filters');
const closeButton = document.getElementById('close-filters');
closeButton.addEventListener('click', () => {
filters.open = false;
filters.querySelector(':scope > summary').focus();
});
</script>The same idea applies in the other direction. If a script opens a widget in response to something else, such as a validation error, don't move focus to the first field inside unless the user asked for it. An unexpected focus jump is disorienting. Opening the widget and leaving focus where it is, or focusing the summary, is a safer default.
Closing with Escape
Escape doesn't close a native details. Some teams add it for widgets that hold a lot of content, such as filter panels. If you add it, keep it small and scoped, and remember that it is an extra feature, not something users will expect.
document.addEventListener('keydown', (event) => {
if (event.key !== 'Escape') return;
const openDetails = event.target.closest('details[open]');
if (!openDetails) return;
openDetails.open = false;
openDetails.querySelector(':scope > summary').focus();
});closest finds the innermost open details that contains the focused element, so Escape closes one level of nesting at a time. If focus is on a summary inside an open widget, closest returns that same widget, which closes it. That matches the native toggle, and it is a reasonable result.
Common mistakes
| Mistake | What goes wrong | Fix |
|---|---|---|
| `outline: none` on `summary` | Keyboard users can't see where focus is | Style `summary:focus-visible` instead |
| Link or button inside `summary` | Clicks and Enter go to the inner control, and the toggle is unreliable | Move the control outside the `summary` |
| `role="button"` or `aria-expanded` on `summary` | Overrides the native state, which can then be wrong | Remove the ARIA attributes |
| Vague summary text such as "More" | Not understandable in a list of controls | Name the content: "Shipping and returns" |
| `tabindex="-1"` on `summary` | Removes the control from keyboard access | Don't set `tabindex` on it |
| Script closes the widget with focus in the body | Focus is lost to the document | Move focus to the `summary` |
| Important content hidden by default | Users who skim or search may miss it | Start with `open`, or open it for matching hashes |
| Custom marker drawn with an image or an unnamed glyph | Screen readers read stray text, or low-vision users can't see the state | Use `content` with alternative text and keep the contrast high |
Testing checklist
Spend five minutes on these checks before you ship:
- Put the mouse away and use only Tab, Shift+Tab, Enter and Space. You should be able to open, read and close every widget.
- Confirm that the focus ring is visible on every
summaryat the zoom levels you support. - Open a closed widget, then Tab. Focus should enter the body. Close it, then Tab. Focus should skip the body.
- Turn on a screen reader. Check that the name, the role, and the expanded or collapsed state are announced, and that the state announcement changes after a toggle.
- Zoom to 200% and confirm that the summary text wraps and the target stays easy to hit.
- Search for a phrase that appears only inside a closed widget. Note what your target browsers do, and add script if they don't open it.
- If you set
prefers-reduced-motion: reducein your operating system, confirm that the marker doesn't animate.
Exercise
A teammate styles a disclosure with summary { outline: none; }, replaces the summary text with a heading that contains a link, and adds role="button" aria-expanded="false" to the summary. Name the three problems, and say what you would change for each.
Show answer
First, removing the outline hides the focus indicator, so keyboard users can't see where they are. Replace it with a summary:focus-visible rule that draws a clear outline. Second, a link inside the summary is a control nested in a control, so Enter and clicks activate the link and not the toggle. Move the link outside the summary, or make the summary plain text. Third, role="button" and a static aria-expanded="false" override the browser's native state, and the attribute will say "collapsed" after the user opens the widget. Remove both attributes and let the browser expose the state.
Exercise
A script closes a details when the user presses a "Close filters" button placed in the body. Afterward, keyboard users report that Tab starts again from the top of the page. Explain the cause and write the fix.
Show answer
The button had focus when the widget closed, so the focused element became hidden and the browser moved focus to the document. After setting open to false, move focus to the summary: filters.open = false; filters.querySelector(':scope > summary').focus();. Focus then stays at the widget, and the next Tab press continues from there.
Exercise
A page uses five details elements that share name="faq". A keyboard user is on the first summary and presses the Down Arrow key. What happens, and how does the user reach the second summary?
Show answer
Nothing happens beyond normal page scrolling, because native details has no arrow-key navigation, even in a named group. The user presses Tab to move focus from the first summary to the next focusable element. If the first widget is open, that is the first focusable element in its body. Otherwise it is the second summary. Enter or Space on a summary opens it and closes the other open member of the group.