details and summary

Beginner · 6 min

How the native disclosure widget works: the open attribute, exclusive accordions with name, the toggle event, keyboard behaviour, and find-in-page.

Before thisThe Accessibility Tree and Native Semantics

Theo Soti · 6 min

details and summary

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Theo SotiReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<details>` with a `<summary>` is a built-in disclosure widget. It needs no JavaScript, takes keyboard focus, and toggles on Enter and Space.
  • The `open` attribute is the state. Add it to start expanded, read `details.open` to inspect it, and set it from script to change it.
  • Give several `<details>` the same `name` and they become an exclusive accordion: opening one closes the others. This worked in Chromium, Firefox and WebKit builds in a test.
  • If two details in one group are both marked `open` in the markup, only the first stays open. If a script opens two, the last one wins.
  • The `toggle` event fires after the state changes and carries `oldState` and `newState`. It is asynchronous, so don't expect it to run before your next line of script.

The widget you keep rebuilding

An FAQ item, a “show more” block, a collapsible filter list: all the same shape. A button toggles a panel, the button’s aria-expanded follows, the panel hides, and everyone writes about twenty lines of JavaScript to make that work, then forgets keyboard handling.

HTML has had the element for it since HTML5. Think of it as a drawer with a label on the front: the summary is the label, always visible, and the rest of the details is the drawer’s contents.

<details>
  <summary>What is your refund policy?</summary>
  <p>Thirty days, no questions asked.</p>
</details>

What you get for free

In the accessibility tree, Chromium exposed the summary as a disclosure triangle named by its text, and the closed body was not in the tree at all. The summary is focusable with Tab, and in all three engines I tested, Enter and Space both toggled it. The expanded state is reported to assistive technology without any aria-expanded from you, which is why the ARIA lesson says to prefer this.

The state lives in one boolean attribute, open. Add it to render expanded. From script, details.open = true opens it. CSS can target details[open] for styling.

Exclusive accordions with name

Give several details the same name, as with radio buttons, and the browser keeps one open at a time:

<details name="faq"><summary>One</summary>…</details>
<details name="faq"><summary>Two</summary>…</details>
<details name="faq" open><summary>Three</summary>…</details>

In Chromium 159, Firefox 159 and a WebKit build, clicking “One” opened it and closed “Three”, then clicking “Two” closed “One”. Two corner cases from the same test: with two members marked open in the markup, only the first stayed open; and with a script setting open on two members one after the other, the last one won. web-features lists name as Baseline since 3 September 2024 (Chrome 120, Firefox 130, Safari 17.2). Details with the same name need to be in the same tree, and independent groups use different names.

Events and finding text

The toggle event fires on the element when open changes, and it carries oldState and newState. In the test it fired asynchronously, a moment after the state change, and the exclusive-group closure also fired its own event for the member that closed. Treat it as “tell me what happened”, not “run before it happens”.

Closed content is still searchable. A URL fragment pointing at text inside a closed details opened it in every engine in the test. That follows the same idea as hidden="until-found" in the hiding lesson, which is the reason to prefer this element over a display: none panel.

Styling and limits

You can restyle the summary marker with ::marker and animate the panel with ::details-content (Baseline since 16 September 2025 per web-features), though animating to height: auto also needs interpolate-size, which at the time of the data was Chromium only. See the deeper video for the CSS. Don’t use details for navigation menus or tabs, where the semantics differ.

What the video covers

Theo Soti’s “Your accordion is 20 lines of JavaScript. It could be 4 lines of HTML.” (August 2026, about 6 minutes). The chapters are: the JavaScript we still write for an accordion, the same thing in four lines, details/summary/panel, the open attribute, keyboard and screen reader behaviour, find in page opening a closed section, name for exclusive grouping, the toggle event with oldState and newState, styling with details[open], replacing the triangle with ::marker, animating height auto, and a recap. That matches this lesson closely. It is a small channel and a recent video, and I could not read a transcript, so I cannot vouch for its demos. The behaviours above were checked in a browser.

Where this fits

This follows the accessibility tree, as the first element that reports its own state. Next, the dialog element handles the heavier case of content that interrupts the page.

Check yourself

  1. 01How do you build an FAQ where opening one answer closes the others, without JavaScript?

    Put the same `name` attribute on every `<details>` in the group. In a test, clicking a second summary closed the first in all three engines I ran. `name` has been Baseline since September 2024 according to `web-features`.

  2. 02Three details in one `name` group all have the `open` attribute in the markup. What does the page show?

    Only the first stays open, since a group allows one. In a test of two open members, the second was closed when the page parsed.

  3. 03A user searches the page with Ctrl+F for text inside a closed details. What should happen, and what did you see?

    The browser should open the details so the match is visible. In a test, navigating to a fragment inside a closed details opened it in every engine. Text search behaviour itself depends on the browser, and I tested only the fragment case.

  4. 04How do you react when the user opens or closes a details?

    Listen for the `toggle` event. It reports `oldState` and `newState`, and it fires after the change, asynchronously, so read `event.newState` rather than assuming the order.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark