Sub topic 4
Content rules for summary and the hidden body
This chapter covers what you can put inside <summary> and inside the rest of <details>, and what the browser does with content that breaks the rules. It assumes you can already write a basic <details> element with a <summary> and know how the open attribute works. You should also be comfortable with HTML content categories such as phrasing content and flow content. Those two terms come up often below.
The two regions of a details element
A <details> element has two regions:
- The summary region is the label that is always visible. It is also the control that toggles the widget.
- The body region is everything else. It is rendered only while the element has the
openattribute.
The HTML specification describes the content model of <details> as one <summary> element followed by flow content. Flow content is almost everything you would put in a <div>. The content model of <summary> is narrower: phrasing content, optionally intermixed with heading content.
<details>
<summary>Delivery times</summary>
<p>Standard delivery takes three to five business days.</p>
<ul>
<li>Express: next business day</li>
<li>Standard: three to five business days</li>
</ul>
</details>The <summary> is the first child. The paragraph and list make up the body.
How the browser picks the summary
The browser looks for the first <summary> element that is a direct child of <details>. That element becomes the toggle label. The rules are:
- A
<summary>nested deeper, for example inside a<div>within the<details>, is not the summary. It is an unknown-purpose element with no toggle behavior. - If a
<details>has several<summary>children, only the first one acts as the summary. The others are ordinary body content and do nothing when clicked. - A
<summary>that is not a child of<details>is not a summary at all. It is a generic element and does not toggle anything. - If there is no
<summary>child, the browser generates a default label, usually the word "Details" in the user's language.
The default label is a fallback. It is the same for every widget on the page, it is not styled to match your design, and it tells users nothing about the content. Always write a <summary>.
What can go inside summary
The summary's content model is phrasing content, optionally mixed with heading content. Phrasing content is the inline-level markup: text, <em>, <strong>, <code>, <span>, <abbr>, <time>, <img>, and so on. Heading content is <h1> to <h6> and <hgroup>.
That gives you three useful patterns.
Plain text with inline emphasis
<details>
<summary>Why does <code>fetch()</code> reject on network errors only?</summary>
<p>
An HTTP error status such as 404 still counts as a completed request,
so the promise resolves. Check <code>response.ok</code> yourself.
</p>
</details>A heading inside the summary
Using a heading inside <summary> is valid, and it is useful when the widget is a section of a longer page. It lets the section appear in the document outline.
<details>
<summary><h3>Refund policy</h3></summary>
<p>Refunds are issued to the original payment method within 10 days.</p>
</details>Two things to check when you do this.
- Browsers give headings a block layout and margins by default. The heading can look detached from the marker, so you will likely need CSS such as
summary h3 { display: inline; margin: 0; }. The next chapter covers marker and layout styling in detail. - Assistive technology support for headings inside a summary varies. A summary is exposed as an interactive control, and some browser and screen reader combinations do not announce the heading level inside it. Test with a real screen reader before depending on this for navigation.
If you need dependable heading navigation, put the heading outside the widget and let the <summary> repeat or shorten its text.
Short images or icons
An <img> or inline <svg> is acceptable in a summary. If the image carries meaning, give it an alt attribute. If it is decorative, use alt="", so the accessible name of the summary does not include a filename.
What should not go inside summary
Phrasing content technically includes interactive elements such as <a>, <button>, and <input>. The specification does not forbid them in a summary. In practice they cause problems.
- A click on an interactive descendant runs that element's own behavior. It does not toggle the widget. A user who clicks a link in the summary navigates away instead of expanding it.
- The summary is itself exposed to assistive technology as an interactive control, and interactive controls inside another control are a known accessibility problem. Screen reader users may not be able to reach the nested control, or may hear a confusing announcement.
- Keyboard behavior becomes ambiguous. Both the summary and its nested control want the Enter and Space keys.
The safe rule is that a summary contains a label and nothing the user is meant to click on separately. If you need an action next to the label, put it in the body or outside the <details>.
Block elements such as <p>, <div>, <ul>, and <table> are not allowed in <summary>. Browsers will usually render them, but the markup is invalid, and styling and accessibility become unpredictable. Use <span> for a styled inline part, or move the block content into the body.
| Content in summary | Valid by spec | Recommendation |
|---|---|---|
| Text, `<em>`, `<code>`, `<abbr>` | Yes | Use freely |
| `<h1>` to `<h6>` | Yes | Use with care and test with a screen reader |
| `<img>`, inline `<svg>` | Yes | Provide `alt` or use `alt=""` |
| `<a>`, `<button>`, `<input>` | Yes (phrasing) | Avoid; clicks do not toggle and it confuses assistive technology |
| `<p>`, `<div>`, `<ul>`, `<table>` | No | Move to the body or use `<span>` |
| Another `<details>` | No | Put it in the body |
Writing a good summary
A summary is the only text a collapsed widget shows, so it has to carry the meaning on its own.
- Describe the content, not the action. "Shipping and returns" is better than "Click to expand".
- Make each label unique on the page. A list of five widgets all labeled "More" gives screen reader users nothing to tell them apart.
- Keep it short enough to fit on one or two lines at narrow widths.
- Do not change the label text when the widget opens. The marker and
aria-expanded-style state (exposed by the browser automatically) already communicate the state.
What can go inside the body
Anything after the summary is flow content. That includes paragraphs, lists, tables, images, figures, forms, code blocks, and other <details> elements. There is no required wrapper. Loose text placed directly inside <details> after the summary is rendered as part of the body.
<details>
<summary>Order a sample</summary>
<p>Fill in the form and we will post a sample within a week.</p>
<form action="/samples" method="post">
<label>
Email
<input type="email" name="email" required>
</label>
<button type="submit">Request sample</button>
</form>
</details>Nesting
You can nest one <details> inside the body of another. Each has its own state, and closing the outer one hides the inner one without changing the inner one's open attribute.
<details open>
<summary>Account settings</summary>
<details>
<summary>Change password</summary>
<p>Use at least 12 characters.</p>
</details>
<details>
<summary>Delete account</summary>
<p>This cannot be undone.</p>
</details>
</details>Deep nesting makes keyboard navigation and screen reader announcements harder to follow. Two levels is usually the practical limit.
Where details itself may not go
<details> is interactive content in the HTML categories. That means it cannot be a descendant of another interactive element, such as <a> or <button>. Wrapping a <details> in a link is invalid.
What "hidden" means for the body
When the widget is closed, the body is not rendered. This has consequences that surprise people who expect the content to behave like display: none applied by their own CSS.
- The content stays in the DOM.
querySelector,textContent, and mutation observers still see it, and scripts can still modify it. - Closed content is not focusable and is skipped by the tab order. Screen readers do not read it.
- Form controls inside a closed
<details>are still part of the form. A text field the user never saw is submitted with its value, unless it isdisabled. - Validation can fail on a control the user cannot see. If an
<input required>in a closed widget is empty, the browser needs to show an error on it but cannot focus a hidden control. Some browsers open the widget for you. Others block the submission with a console message such as "An invalid form control is not focusable". Do not depend on either behavior. Either avoidrequiredcontrols in collapsed sections, or open the widget with script when validation fails. - Find-in-page and links to fragments such as
#deliverycan reach text inside a closed widget in some browsers, and those browsers open the widget to show the match. Support is not uniform, so do not treat a closed widget as a reliable way to keep content out of search results. - Images in the body may still be fetched. Add
loading="lazy"to images in closed widgets that are large or numerous.
Because the browser removes the body from rendering, you cannot use CSS display to override this on the <details> children in a reliable way. Control visibility through the open attribute. The styling chapter covers the supported hooks for the body.
Common mistakes
- No summary at all. The widget shows "Details", which is useless as a label.
- A summary that is not the first child. Wrapping the summary in a
<div>or placing a comment-like text node first breaks the structure. - Block elements in the summary. A
<div>or<p>in a summary invalidates the markup and breaks the intended layout. - A link or button in the summary. The click does not toggle, and the control is hard to reach with assistive technology.
- Required inputs in a collapsed body. Submission fails on a field the user cannot see.
- Reusing one generic label. Many widgets labeled "Read more" are indistinguishable in a screen reader's list of controls.
Putting it together
The widget below follows every rule in this chapter. It uses an unwrapped summary with phrasing content, a body of flow content, a nested widget, and an optional field instead of a required one.
<details>
<summary>Shipping to <abbr title="United Kingdom">UK</abbr> addresses</summary>
<p>We ship with tracked delivery. Prices depend on weight.</p>
<table>
<thead>
<tr><th>Weight</th><th>Price</th></tr>
</thead>
<tbody>
<tr><td>Up to 1 kg</td><td>£3.50</td></tr>
<tr><td>1 to 5 kg</td><td>£6.00</td></tr>
</tbody>
</table>
<details>
<summary>Remote postcodes</summary>
<p>Highlands and islands add two business days.</p>
</details>
</details>Exercise
A teammate writes <details><div><summary>Pricing</summary></div><p>Plans start at $5.</p></details>. What does the browser show for the label, and how do you fix the markup?
Show answer
The <summary> is wrapped in a <div>, so it is not a direct child of <details> and is not treated as the summary. The browser shows its default label (usually "Details"), and the "Pricing" text sits in the body area and does not toggle anything. Remove the <div> so that <summary> is the first direct child: <details><summary>Pricing</summary><p>Plans start at $5.</p></details>.
Exercise
Why is <summary><a href="/faq">Frequently asked questions</a></summary> a poor choice, and what would you do instead?
Show answer
A click on the link follows the link instead of toggling the widget, and a control nested inside the summary (which is itself exposed as a control) is hard to use with assistive technology. Use plain text in the summary, such as <summary>Frequently asked questions</summary>, and put the link in the body, for example <p>See the <a href="/faq">full FAQ page</a>.</p>.
Exercise
A closed <details> contains <input type="email" required> that is empty. The user submits the surrounding form. Name the problem and one way to handle it.
Show answer
The browser cannot focus or show an error on a control inside a closed widget, so submission may be blocked without a visible message (some browsers open the widget, others do not). Handle it by avoiding required on controls in collapsed sections, or by listening for the form's invalid event on the control and setting details.open = true so the user can see the field.