Sub topic 10
When native details is enough
This chapter assumes you can write basic HTML, CSS selectors and properties, and JavaScript that selects elements and adds event listeners. It also assumes you have seen details, summary, the open attribute, the toggle event and the name attribute from earlier chapters. Here the goal is a decision: when the native elements are enough, and when a custom component is worth its cost.
What the native elements give you
A details element with a summary already provides several behaviors that a custom widget must build by hand:
- A focusable control that responds to Enter and Space.
- State stored in the DOM as the
openattribute. - Content that is hidden from view and from the accessibility tree while collapsed.
- A
toggleevent when the state changes. - Exclusive groups through a shared
namevalue. - Operation without JavaScript.
Each of these is a place where a custom version can go wrong. That is the main argument for starting native.
A decision flow
Walk through these questions in order. Stop at the first one that rules out the native elements.
Cases where native details fits
Native details is a good choice when the content is supplementary and the interaction is a plain toggle.
- FAQ lists. Each question is a
summaryand each answer is the body. Addingnamemakes it an accordion. - Optional detail. Technical specifications, terms, changelogs or "show more" text that most readers can skip.
- Form helpers. A collapsed "Advanced options" block inside a form.
- Documentation. Collapsible code samples or long output.
All of these need one state per item, a visible label and a body. The browser handles that fully.
Cases that call for something else
Native details has limits. Check your requirement against this table.
| Requirement | Native details | What to do |
|---|---|---|
| Animated height transition | Limited. The body is hidden by the browser, so a plain CSS height transition does not run by default. | Use newer CSS features where supported, or a custom component. |
| Tabs, where one panel is always visible | Not a fit. A group can have all items closed. | Use a tabs pattern with the `tablist`, `tab` and `tabpanel` roles. |
| Menus, tooltips or popups | Not intended for this. Disclosure is not the same as a menu. | Use the Popover API or a dialog. |
| Interactive content inside `summary` | Poor. The `summary` is itself the toggle control. | Move buttons and links to the body. |
| Custom keyboard model, such as arrow keys between items | Not provided. Items are separate tab stops. | Build a custom accordion. |
| Rendering older browsers without `name` grouping | The group still works as independent items. | Accept the fallback, or add a script. |
The last row shows a useful point. In a browser that does not support exclusive groups, each details simply toggles on its own. The content stays reachable, so this is a safe degradation for most pages. It would not be safe if your design depends on only one item being open.
Check a requirement with the toggle event
Sometimes native fits once you add a little script. The toggle event lets you load content on demand, record that a user read a section, or sync other UI, without replacing the element.
<details id="changelog">
<summary>Changelog</summary>
<div id="changelog-body">Loading is deferred until you open this.</div>
</details>
<script>
const changelog = document.querySelector('#changelog');
const body = document.querySelector('#changelog-body');
let loaded = false;
changelog.addEventListener('toggle', () => {
if (changelog.open && !loaded) {
loaded = true;
body.textContent = 'Version 2.1: fixed the sign-in redirect.';
}
});
</script>The script reads open and acts once. The element still works if the script fails, because the body is present and the toggle is native. Prefer this pattern, where script adds to native behavior, over one where script is required for the widget to work at all.
Common mistakes when deciding
- Choosing a custom component for the sake of styling. The marker, the summary and the open state are all styleable with CSS, as covered earlier. Check this first.
- Using details for navigation menus. A menu has different semantics and keyboard expectations than a disclosure.
- Putting a button inside
summary. Interactive content nested in the toggle control creates conflicting actions and unclear behavior for assistive technology. - Hiding essential content. Anything a user must read to complete a task should not sit behind a collapsed item by default. Use
openor show it directly. - Assuming search and links reach hidden content. Behavior for find-in-page and fragment links to collapsed content varies by browser. Test the browsers your users have.
Summary of the criteria
Use native details when the content is optional, each item toggles independently or as a simple exclusive group, and CSS plus the toggle event can cover the rest. Use a custom component when you need a different keyboard model, always-one-visible behavior, fully controlled animation, or interactive content in the heading.
Exercise
A page has a "Shipping details" section that most visitors do not need. The team also wants a row of three panels where exactly one panel is always visible. For each, say whether native details is enough and why.
Show answer
The shipping section is a good fit for native details. It is optional, supplementary content with one state and a plain toggle. The row of three panels is not, because a details group (shared name) can have every item closed, while the design requires one panel always visible. That is a tabs pattern, so use a custom component with tab roles and keyboard handling.
Exercise
Your accordion uses details elements with the same name. In a browser without support for name grouping, what happens, and is that acceptable?
Show answer
Each details element toggles independently, so several can be open at once. The content is still reachable. This is acceptable when "only one open" is a convenience, and not acceptable when the layout or logic requires exactly one open item.