More HTML answers

by Danny
Published 2 min read

Build an accessible accordion with the details and summary elements and no JavaScript. Add a shared name attribute so only one panel opens at a time.

The short answer

#

Use the details element with a summary inside it. The browser handles opening, closing, keyboard support and screen reader announcements for you. Give several details elements the same name attribute to make an exclusive accordion, where opening one closes the others.


_10
<details name="faq" open>
_10
<summary>What is your returns policy?</summary>
_10
<p>You can return any item within 30 days.</p>
_10
</details>
_10
_10
<details name="faq">
_10
<summary>Do you ship abroad?</summary>
_10
<p>Yes, to most countries in Europe.</p>
_10
</details>

Why it happens

#

Accordions are usually built with buttons, aria-expanded, hidden panels and a click handler. That is a lot of code to get right, and it is easy to miss keyboard focus, the expanded state or the link between the button and its panel.

The details element is a built-in disclosure widget. Its summary is focusable and responds to Enter and Space, assistive technology announces it as expanded or collapsed, and the open attribute reflects its state. Content inside a closed details element can also be found with the browser's find in page, which opens the panel automatically in supporting browsers.

The name attribute adds the one feature people used to need JavaScript for: groups where only one panel can be open. Elements with the same name value form a group, even if they are not siblings. It is supported in Chrome and Edge 120, Safari 17.2 and Firefox 130, so it works in all current major browsers. In older browsers the attribute is ignored and each panel opens independently, which is a safe fallback.

How to fix it

#

Build the basic accordion

The summary must be the first child of details. Anything after it is the panel content. Add open to show a panel by default.


_10
<details>
_10
<summary>Delivery times</summary>
_10
<p>Most orders arrive within three working days.</p>
_10
</details>

Make panels exclusive with name

Give every details in the group the same name. Use a different name for each separate group on the page.


_10
<details name="shipping">...</details>
_10
<details name="shipping">...</details>
_10
<details name="payments">...</details>

Style the marker and open state

The default triangle is a ::marker on the summary. Hide it with list-style: none, or with the WebKit pseudo-element for older Safari, and draw your own. Style the open state with the [open] attribute selector.


_18
summary {
_18
list-style: none;
_18
cursor: pointer;
_18
padding: 0.75rem 1rem;
_18
}
_18
_18
summary::-webkit-details-marker {
_18
display: none;
_18
}
_18
_18
summary::after {
_18
content: '+';
_18
float: right;
_18
}
_18
_18
details[open] > summary::after {
_18
content: '\2212';
_18
}

Animate opening and closing

Newer browsers expose the panel content as the ::details-content pseudo-element. Animating it to auto also needs interpolate-size, which at the time of writing only Chromium browsers support. Other browsers simply open without animation, so this is safe to add as an enhancement.


_13
:root {
_13
interpolate-size: allow-keywords;
_13
}
_13
_13
details::details-content {
_13
block-size: 0;
_13
overflow: hidden;
_13
transition: block-size 250ms, content-visibility 250ms allow-discrete;
_13
}
_13
_13
details[open]::details-content {
_13
block-size: auto;
_13
}

React to changes in JavaScript

If you need to track which panel is open, listen for the toggle event. It fires after the state changes.


_10
document.querySelectorAll('details').forEach((panel) => {
_10
panel.addEventListener('toggle', () => {
_10
console.log(panel.open ? 'opened' : 'closed');
_10
});
_10
});

Common pitfalls

#
  • Putting a heading around the details and expecting the summary to act as that heading. If headings matter for navigation, put the heading inside the summary.
  • Adding buttons or links inside summary. Interactive content there is confusing for keyboard and screen reader users.
  • Changing display on summary to flex and losing the marker. That is expected; draw your own indicator.
  • Using a non-unique name across unrelated groups. Every details with the same name on the page is one group.
  • Relying on name for content users need to compare side by side. Exclusive accordions hide one answer when the next opens.
#

Was this resource helpful?