Sub topic 4
Content rules for summary and the hidden body
This chapter assumes you can write basic HTML documents and know the details and summary structure from the previous chapters. It covers what you may put in the summary and in the rest of the details, and what goes wrong when you break the rules.
The two parts of a details element
A details element has two kinds of content. The first summary child is the label. It is always visible and it is the control the user activates. Everything else is the body, which the browser hides while the widget is closed.
Rules for summary
The summary must be the first child of details. If you put other content first, the browser does not treat the later summary as the label.
If you write more than one summary, only the first one is the label. The others are treated as part of the body.
If you leave out summary, the browser adds its own label, usually the word "Details". That text is not in your markup, so it cannot be translated or styled as content. Always write a summary.
The content model for summary is phrasing content, optionally mixed with heading content. Phrasing content is the inline material such as text, <strong>, <em>, <code> and <span>. This is valid:
<details>
<summary>Can I use <code>details</code> in <em>old</em> browsers?</summary>
<p>Browsers that do not know the element show all of the content.</p>
</details>A heading inside summary is allowed. Use it when the label also names a section of the page:
<details>
<summary><h3>Shipping</h3></summary>
<p>Orders ship within two business days.</p>
</details>Be careful with this pattern. Browsers expose summary to assistive technology as a button-like control, and a button's children are often flattened to plain text. The heading may not appear in a screen reader's heading list. Test with the tools your users have, or keep headings outside the widget.
Interactive content inside summary
Do not put links, buttons, form fields or other interactive elements inside summary. The summary is already an interactive control. Nested controls compete with it for clicks, and focus and keyboard behavior become unpredictable.
<!-- Avoid: two controls in one place -->
<details>
<summary>Pricing <a href="/pricing">(full table)</a></summary>
<p>Plans start at $5 per month.</p>
</details>Move the link into the body, or place it beside the widget.
Rules for the hidden body
The body can hold any flow content: paragraphs, lists, tables, images, forms and other details elements. Text placed directly inside details is also valid, but wrap it in an element such as <p> so you can style and select it.
Nested details elements work as an outline:
<details>
<summary>Account help</summary>
<p>Choose a topic:</p>
<details>
<summary>Billing</summary>
<ul>
<li>Update a card</li>
<li>Download an invoice</li>
</ul>
</details>
</details>Each nested element keeps its own open state. Closing the outer element hides the inner one but does not change its state.
What "hidden" means
While a details element is closed, its body is not rendered. This has practical effects:
| Situation | What happens when closed |
|---|---|
| Keyboard Tab | Focusable elements in the body are skipped |
| Screen readers | The body is not read |
| Images in the body | Browsers may still download them |
| Find in page | Recent browsers open the widget when the search matches its text, but older ones do not |
| `display` on the body | You cannot force the body visible with simple rules on its children |
Do not rely on closed content for security or privacy. It is still in the document, and anyone can view the source.
Keeping the structure style-friendly
Browsers give details a block layout and summary a list-item layout, which is where the marker comes from. If you change summary to display: flex or display: grid, some browsers remove the marker. The next chapter on styling covers how to handle this. For now, keep the content rules above simple: a short text label in summary, and wrapped flow content in the body.
Quick checklist
- Put exactly one
summaryas the first child ofdetails. - Keep
summaryto short phrasing content. - Keep links, buttons and fields out of
summary. - Wrap body text in elements.
- Do not put required form fields in a section that starts closed.
Exercise
A teammate writes <details><p>Terms apply.</p><summary>Offer</summary></details>. What does the browser do, and how do you fix it?
Show answer
The summary is not the first child, so the browser does not treat it as the label. It adds its default label, usually "Details". Move the summary to the first position: <details><summary>Offer</summary><p>Terms apply.</p></details>.
Exercise
Why is <summary>Docs <a href="/docs">open page</a></summary> a poor design, and where should the link go?
Show answer
The summary is already an interactive control that toggles the widget. A link inside it competes for the same click and focus, so users cannot predict whether they will toggle the widget or navigate. Put the link in the body of the details, or beside the widget.