Sub topic 10
When native details is enough
This chapter closes the tutorial. You already know how to build, style, script, and group details elements. Here you decide when that toolkit is the right choice and when a custom component is justified.
It assumes you are comfortable with these points from earlier chapters:
- The
detailsandsummarystructure, and theopenattribute and property. - The
toggleevent. - The
nameattribute for exclusive groups. - CSS selectors such as
details[open]andsummary::marker.
Start from what the browser gives you
A native details element already provides these behaviors:
- A focusable control (the
summary) with an implicit button role. - Activation with Enter and Space, and with a mouse click or tap.
- Expanded and collapsed state exposed to assistive technology.
- State stored in one place, the
openattribute, which also works from script. - Optional exclusive grouping through
name. - No JavaScript, so it works while scripts are loading or if they fail.
- In current browsers, find-in-page and fragment navigation open a collapsed
detailsthat contains the match.
A custom accordion has to rebuild all of this: button elements, aria-expanded, aria-controls, hidden regions, keyboard handling, and state synchronization. Each piece is a chance for a bug. The default position is therefore to use details unless a specific requirement rules it out.
Cases where native details fits well
Question-and-answer lists
An FAQ is the textbook case. Each item has a short label and a longer answer, and readers usually want one or two answers. Add a shared name if you want only one open at a time.
<section aria-labelledby="faq-title">
<h2 id="faq-title">Frequently asked questions</h2>
<details name="faq">
<summary>Can I change my delivery address?</summary>
<p>Yes, until the order status changes to "Shipped".</p>
</details>
<details name="faq">
<summary>Do you ship internationally?</summary>
<p>We ship to 30 countries. Duties are paid by the buyer.</p>
</details>
</section>Secondary or advanced options
Forms often have settings most people never touch. Wrapping them in details keeps the form short. The fields inside are still submitted when the section is collapsed, because collapsing hides content but does not disable controls.
<form action="/search" method="get">
<label>Query <input type="search" name="q"></label>
<details>
<summary>Advanced filters</summary>
<label>Minimum price <input type="number" name="min" min="0"></label>
<label>Maximum price <input type="number" name="max" min="0"></label>
</details>
<button type="submit">Search</button>
</form>Long supporting material
Transcripts, changelogs, stack traces, license text, and "show the full table" content suit details. The main reading path stays short, and the extra content is one activation away.
Spoilers and hints
Quiz answers and puzzle hints work well as details, since the reader wants to reveal them on demand and nothing else needs to happen.
Content that needs a light reaction
If the only requirement is "do something when the state changes", the toggle event is enough. The example below loads content the first time the section opens.
<details id="shipping-details">
<summary>Shipping details</summary>
<div id="shipping-body">Loading...</div>
</details>
<script>
const details = document.getElementById('shipping-details');
const body = document.getElementById('shipping-body');
let loaded = false;
async function loadShipping() {
// Replace this with a real fetch() in your application.
await new Promise((resolve) => setTimeout(resolve, 300));
return '<p>Standard delivery takes 3 to 5 business days.</p>';
}
details.addEventListener('toggle', async () => {
if (!details.open || loaded) return;
loaded = true;
body.innerHTML = await loadShipping();
});
</script>The toggle event fires on both opening and closing, so the handler checks details.open first. The loaded flag prevents repeat requests.
Cases where native details is not enough
Some requirements push beyond what the element offers. The table lists the common ones and what to do about each.
| Requirement | Why `details` struggles | Better approach |
|---|---|---|
| Rich animation on every browser | Animating height needs newer CSS features | Progressive enhancement, or a custom component |
| Several groups with cross-group rules | `name` only enforces one open item per group | Script on `toggle`, or a custom component |
| Interactive controls in the header (buttons, links, menus) | `summary` is itself the control; nested interactive content is unreliable | A `button` with `aria-expanded` plus a separate region |
| Heading with a guaranteed accessible name and level | A heading inside `summary` has mixed screen reader support | A heading that contains a `button` |
| Tabs | Tabs show one panel and need arrow-key navigation | The tabs pattern with `role="tablist"` |
| Navigation menus and dropdowns | They need light dismiss, positioning, and focus management | The `popover` attribute, or a menu pattern |
| Modal dialogs | They need focus trapping and a backdrop | The `dialog` element |
| Tree views | They need nested arrow-key navigation | The tree pattern with ARIA roles |
| Closed content visible when printing | Closed `details` print collapsed | A print stylesheet or script before printing |
Animation
Closing and opening a details is instant by default. Animating it used to require JavaScript because the browser removed the hidden content from rendering and height: auto cannot be transitioned. Newer browsers expose the hidden content through the ::details-content pseudo-element and support interpolate-size, which together allow a CSS-only animation:
:root {
interpolate-size: allow-keywords;
}
details::details-content {
block-size: 0;
overflow: hidden;
transition: block-size 250ms ease, content-visibility 250ms allow-discrete;
}
details[open]::details-content {
block-size: auto;
}
@media (prefers-reduced-motion: reduce) {
details::details-content {
transition: none;
}
}Treat this as progressive enhancement. Browsers without support ignore the rules, and the widget opens and closes without animation. That is a perfectly acceptable fallback. If your design requires the same animation everywhere, either accept the difference or move to a custom component.
Always respect prefers-reduced-motion, as shown above.
Interactive content in the summary
The summary is the toggle control. Placing a link, button, or form field inside it creates two conflicting controls in one place. Clicks may toggle the widget, trigger the inner control, or both, and screen readers may flatten the inner control into the summary's name.
If you need an action next to the label (for example a "Delete" button for each row), put the action outside the summary:
<details>
<summary>Invoice 1042</summary>
<p>Issued 2026-11-03. Total: 120.00 EUR.</p>
<button type="button">Download PDF</button>
</details>The button lives in the body, so it appears only after the section opens. If it must be visible while collapsed, details is the wrong element.
Headings
Many pages organize content by headings, and an FAQ item is often a subheading. The HTML specification allows heading content inside summary, but assistive technology support varies, so test with the screen readers your audience uses. Do not assume the heading appears in a screen reader's heading list.
When heading navigation matters, such as on a long documentation page, a custom pattern is safer:
<h3>
<button type="button" aria-expanded="false" aria-controls="panel-1">
Account settings
</button>
</h3>
<div id="panel-1" hidden>
<p>Change your name, email, and password.</p>
</div>You then write the script that toggles aria-expanded and hidden. The cost is real code, which is the trade you accept for control over the heading.
Check support before relying on newer features
Core details and summary work everywhere you are likely to ship. The newer features have their own support story:
- The
nameattribute for exclusive groups is a recent addition. In a browser that lacks it, the items become independentdetailselements, so several can be open at once. That is a graceful fallback for most FAQs. ::details-contentandinterpolate-sizeare newer still.
You can feature-detect both in script if the difference matters:
const supportsExclusiveGroups = 'name' in document.createElement('details');
const supportsDetailsContent = CSS.supports('selector(::details-content)');
console.log({ supportsExclusiveGroups, supportsDetailsContent });
if (!supportsExclusiveGroups) {
// Fallback: close the other items in the same group when one opens.
document.addEventListener(
'toggle',
(event) => {
const target = event.target;
if (!(target instanceof HTMLDetailsElement) || !target.open || !target.getAttribute('name')) {
return;
}
const group = target.getAttribute('name');
document
.querySelectorAll('details[name]')
.forEach((other) => {
if (other !== target && other.getAttribute('name') === group) {
other.open = false;
}
});
},
true
);
}The toggle event does not bubble, so the listener is registered on document with the capture flag (true) to catch it from any details element. Setting other.open = false fires another toggle event on that element, but its handler returns early because open is false.
In practice, many teams skip this fallback and accept independent items in older browsers. Write it only if "exactly one open" is a hard requirement.
A decision flow
The following diagram summarizes the questions to ask, in order.
Common mistakes when choosing
- Using
detailsfor navigation menus. A menu needs closing on outside clicks, Escape handling, and positioning.detailsprovides none of that. Thepopoverattribute covers the common cases. - Hiding critical information. Content in a closed
detailsis easy to miss. Do not hide error messages, legal notices the user must accept, or the primary call to action. - Collapsing everything by default. If readers nearly always open an item, start it open with the
openattribute, or do not hide it at all. - Replacing the whole control to fix a style. Most visual complaints, such as the triangle's color or position, are solved with
summary::marker,list-style, or a custom icon. Check that before writing a custom component. - Ignoring the closed-state summary. The label is all readers see while collapsed. Make it specific: "Refund policy" beats "More info".
- Relying on script for the baseline. If your widget stops working when JavaScript fails, it is probably over-engineered. Let the native behavior carry the base case, and use script only for extras.
Review of the whole tutorial
You can now do the following:
- Build a disclosure widget with
detailsandsummary. - Set the starting state with
open. - Read and change that state with the
openproperty. - React to changes with the
toggleevent. - Create exclusive accordions with a shared
name. - Style the summary, marker, and open state with CSS.
- Judge whether native
detailsis enough for a given requirement.
The last skill matters most in day-to-day work. A short check against the table and flowchart above takes a minute and often saves a custom component.
Exercise
A product page has a list of 12 specification groups (Dimensions, Battery, Ports, and so on). Each group has a short label and a table of values. Designers want only one group open at a time, a smooth open animation in browsers that support it, and nothing broken in older browsers. Can native details meet this, and which three features would you use?
Show answer
Yes. Use one details per group with a shared name attribute for the exclusive behavior, put the table inside each body, and add ::details-content with interpolate-size for the animation. Older browsers that lack name let several groups stay open, and browsers that lack ::details-content open instantly. Both fallbacks are acceptable, and wrap the transition in a prefers-reduced-motion check.
Exercise
A teammate puts a "Remove item" button and a quantity input inside the summary of each shopping cart row so both are visible when collapsed. Explain the problem and propose a fix that keeps the controls visible.
Show answer
The summary is itself the toggle control, so nested interactive elements conflict with it. Clicks may toggle the row, run the button, or both, and screen readers may not announce the inner controls reliably. Put the button and input outside the summary, for example in a row layout beside a details that holds only the secondary information, or use a button with aria-expanded for the toggle and leave the other controls as siblings.
Exercise
Your site has a documentation page where readers navigate by the screen reader's heading list. Each section is currently a details element with an h3 inside the summary. What risk does this carry, and what is a safer pattern?
Show answer
Screen reader support for headings inside summary varies, so the sections may not appear in the heading list. Test with the screen readers your audience uses. A safer pattern is an h3 that contains a button with aria-expanded and aria-controls, followed by a region that uses the hidden attribute, with a small script that toggles both.