Layout Beginner

Exclusive accordions without JavaScript

Making only one accordion panel open at a time required JavaScript to listen for toggle events and close all other open details elements. The name attribute on details elements creates a mutually exclusive group — the browser handles closing others automatically.

Modern
4 lines
1<details name="faq">
2  <summary>Question 1</summary> ...
3</details><details name="faq">
4  <summary>Question 2</summary> ...</details>
Old JS required
1// Close all on toggle, reopen the clicked one
2document.querySelectorAll('details').forEach((d) => {
3  d.addEventListener('toggle', () => {
4    if (d.open) others.forEach(o => o.open = false);
5  });
6});
Widely available Since 2025 85% global usage

This feature is well established and works across many devices and browser versions. It has been available across browsers since 2025.

120+
130+
17.2+
120+
open one — the others close automatically
What does the name attribute do?

It groups details elements. The browser ensures only one within the group stays open at a time — identical to how radio inputs with the same name form an exclusive set.

Do I need JavaScript for this?

No. Zero event listeners, zero state management. Open one and the browser closes the others automatically.

Is it keyboard accessible?

Yes. details and summary are semantic HTML with built-in keyboard support. Screen readers announce them correctly without any extra ARIA.

<details name="demo-acc"> — browser enforces exclusivity

Zero JavaScript

The browser closes other open details in the same name group automatically. No toggle listeners, no forEach loops, no state management.

Works like radio buttons for content

details elements with the same name value form an exclusive group — exactly like radio inputs with the same name. Opening one closes the others.

Accessible by default

details and summary are semantic HTML. They are keyboard navigable and understood by screen readers without extra ARIA attributes.

Old Approach
JS toggle listeners
Close all, reopen clicked — per accordion group
Modern Approach
name attribute
Browser handles mutual exclusivity
Lines Saved
6 → 0 JS
No event listeners required

How it works

The <details> element is a native disclosure widget — clicking the <summary> toggles it open and closed. But by default, multiple details elements are independent. Making only one open at a time required attaching toggle event listeners that would close all siblings whenever any one opened.

Adding a name attribute groups details elements together. When one opens, the browser automatically closes any other open details with the same name. The behavior is identical to how radio inputs with the same name form an exclusive selection group. It works with keyboard navigation and screen readers without any extra ARIA.

New CSS drops.

Join 400+ readers who've survived clearfix hacks.

ESC