DEV Community

Jitsumi
Jitsumi

Posted on

HTML details and summary: Give Optional Context a Useful Label

An open native details disclosure with a descriptive summary, followed by a closed disclosure.

A settings page does not need a custom accordion library to hide an explanation of an advanced option. HTML already has a disclosure control: <details>, with a <summary> that names what the reader can reveal. Use it when the hidden material is optional context. Keep information needed to make the immediate decision visible.

That last choice matters more than the triangle. A disclosure can make a page easier to scan, but it can also turn the answer into a guessing game. The useful question is not “Can this paragraph collapse?” It is “Can someone safely continue without reading it?”

Write the closed state first

Suppose a small export tool offers a choice between including and excluding source files. The checkbox label should explain the setting. A disclosure can then answer a narrower question:

<label>
  <input type="checkbox" name="includeSources">
  Include editable source files
</label>
<details>
  <summary>Which files count as sources?</summary>
  <p>The original project and linked images are included.
     Temporary previews are excluded.</p>
</details>
Enter fullscreen mode Exit fullscreen mode

The summary makes sense before the reader opens anything. “More” would make them inspect the content to discover whether it matters. “Which files count as sources?” lets them decide from the closed state.

The HTML standard defines details as a disclosure widget, with its first summary child providing the summary. Put that child first. Do not use the pattern as a substitute for a menu, a tab interface, or the selected step in a wizard; those controls have different interaction expectations.

Start with browser behaviour

In a supporting browser, activating the summary changes the disclosure state without JavaScript. The open attribute controls the initial state. It is Boolean: open="false" still means open because the attribute is present. Remove the attribute, or set the DOM property to false, to close it.

const explanation = document.querySelector('details');
explanation.open = false;
Enter fullscreen mode Exit fullscreen mode

This is a useful boundary for enhancement. The browser owns opening, closing, and the control's basic interaction. Your code can observe state when another part of the interface genuinely needs it. Avoid adding a second click handler that manually toggles open on the summary: it can conflict with the native activation you already have.

The toggle event reports state changes. It is not a promise that every rapid intermediate state will produce a separately useful analytics event; changes can be coalesced. Read details.open when handling it. Counting disclosure clicks as evidence that someone understood the text would also be an editorial inference, not a browser fact.

Decide whether sections should compete

A shared name groups disclosures so opening one closes another in browsers that implement this feature. MDN documents the grouping behaviour. It can suit a compact set of alternatives, but it is a product decision rather than an automatic improvement.

If readers compare two explanations, forcing the first closed makes comparison harder. Independent disclosures are better for that task. If the page contains a long list where one answer usually suffices, exclusive opening may reduce the amount of expanded material.

Treat grouping as progressive enhancement if your supported browser set includes implementations without it. The ungrouped page should remain understandable. Do not promise a particular accordion interaction across older browsers merely because basic details support is present.

Style the invitation, preserve its meaning

Give the summary a visible focus style, adequate spacing, and a label that remains recognisable when it wraps. Keeping the native marker is often the simplest starting point. If you replace it, make the open state visually distinct without relying solely on colour.

Do not turn the summary into a crowded toolbar. A link inside an activation surface creates another interaction the reader must distinguish. Put related actions in the revealed content when possible. Also test the actual browser and assistive technology combinations your audience uses: semantic HTML is a strong foundation, but custom styling can still obscure focus or meaning.

A useful review has three passes. With everything closed, can the reader identify the relevant explanation? With everything open, does the prose still form a coherent page? With only a keyboard, can they open, read, close, and continue without losing their place? These checks reveal different failures.

Let important information stay outside

A required fee, destructive consequence, or prerequisite belongs next to the action it affects. Hiding it behind “Details” is a content problem even when the markup is perfectly valid. Disclosures work best when they let readers choose depth, not when they conceal the terms of a decision.

For a small component, the finished implementation may therefore be almost entirely HTML. That is an advantage worth preserving: the control remains useful before any optional script runs, and the interface has fewer competing state owners.

Published by Jitsumi, covering practical software and developer craft.

Top comments (0)