Sub topic 2
Building one with details and summary
This chapter assumes you can write a basic HTML page (html, head, body, paragraphs, lists) and that you know what a child element is. You don't need CSS or JavaScript yet. Later chapters cover the open attribute, styling, scripting and accessibility in detail. This chapter is about the structure that makes the widget work.
The two elements
A disclosure widget needs two elements.
<details>is the container. It holds everything the widget contains and remembers whether it is expanded or collapsed.<summary>is the visible label. It is always shown, and clicking it toggles the rest of the content.
Everything inside <details> other than the <summary> is the hidden body. The browser shows it only while the widget is expanded.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Return policy</title>
</head>
<body>
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
</details>
</body>
</html>Save this as a file and open it in a browser. You see a small triangle and the text "What is your return policy?". The paragraph is not visible. Click the label and the paragraph appears, and the triangle turns to point down. Click again and the paragraph disappears.
You wrote no CSS and no script. The browser supplies the triangle, the click handling, the keyboard handling and the show-and-hide behavior.
What the browser does for you
The widget has two states: collapsed and expanded. The browser tracks the state with a boolean attribute named open. When open is absent, the widget is collapsed. When open is present, it is expanded. Activating the summary adds or removes that attribute.
"Activate" covers more than a mouse click. Pressing Enter or Space while the summary has keyboard focus does the same thing. The summary is focusable without any tabindex. You will examine this in the keyboard and accessibility chapter. For now, remember that you get it for free because you used the native elements.
You can watch the attribute change. Open the page, right-click the widget, choose Inspect, and click the label. In the element inspector, open appears on <details> when expanded and disappears when collapsed.
Anatomy of the structure
| Part | Element | Role | Visible when collapsed? |
|---|---|---|---|
| Container | `<details>` | Holds the state and all content | Yes, as a whole |
| Label | `<summary>` | Toggle control and title | Yes |
| Body | Any other children of `<details>` | The revealed content | No |
The body can be as large as you like. It can contain several paragraphs, lists, images, tables or other elements.
<details>
<summary>Shipping options</summary>
<p>We offer three delivery speeds:</p>
<ul>
<li>Standard: 5 to 7 business days</li>
<li>Express: 2 to 3 business days</li>
<li>Overnight: next business day</li>
</ul>
<p>Overnight orders must be placed before noon.</p>
</details>All three body elements (the paragraph, the list and the final paragraph) are hidden and shown together. You don't wrap them in a container of your own.
Rules for the structure
The HTML specification describes the content model of <details> as one <summary> element followed by flow content. Flow content means the ordinary elements you put in a page body, such as paragraphs, lists, headings, tables and divisions. Several practical rules follow from this.
Put the summary first
Write <summary> as the first child of <details>. The specification says the first <summary> child represents the label. If you put other content before it, the document is invalid, and browsers may not treat the label the way you expect.
<!-- Avoid: content comes before the summary -->
<details>
<p>You can return items within 30 days of delivery.</p>
<summary>What is your return policy?</summary>
</details>Correct the example by moving the summary above the paragraph, as in the first example in this chapter. A validator such as the W3C Nu HTML Checker reports the wrong order, so it is worth running one on pages that use many widgets.
Use one summary per details
Only the first <summary> child counts as the label. A second <summary> is not a second toggle. It is just extra content that the browser treats as part of the body, and it is invalid markup. If you want two labels, you want two <details> elements.
Know what happens with no summary
A <summary> is optional in the sense that the browser will not fail without one. If you omit it, the browser generates a default label, usually the word "Details" in English. The widget still works, but the label tells the reader nothing about the content.
<details>
<p>This widget has no summary, so the browser shows a generic label.</p>
</details>Treat the generic label as a bug in your page. Always write a summary that describes what the reader will find.
Keep the label short and specific
The label is the only thing readers see before deciding to expand. Good labels are brief and tell the reader what is behind them.
| Weak label | Better label |
|---|---|
| Click here | What is your return policy? |
| More | Shipping options and costs |
| Details | System requirements |
A question works well for FAQ content, and a noun phrase works well for supplementary reference material.
Nesting and repeating
You can place one <details> inside the body of another. The inner widget has its own independent state.
<details>
<summary>Account help</summary>
<p>Choose a topic below.</p>
<details>
<summary>Reset my password</summary>
<p>Use the reset link on the sign-in page.</p>
</details>
<details>
<summary>Change my email</summary>
<p>Open account settings and edit the email field.</p>
</details>
</details>Collapsing the outer widget hides the inner ones, but it does not change their stored state. If the reader had expanded "Reset my password" and then collapses and reopens "Account help", the password answer is still expanded.
Keep nesting shallow. Two levels is usually the practical limit before readers lose track of where they are.
You can also repeat sibling widgets, which is how you build a list of questions. Each <details> is independent by default, so readers can open several at once.
<details>
<summary>How do I reset my password?</summary>
<p>Use the reset link on the sign-in page.</p>
</details>
<details>
<summary>How do I change my email?</summary>
<p>Open account settings and edit the email field.</p>
</details>Opening one leaves the other unchanged. If you want opening one to close the others, you need the name attribute, which a later chapter covers.
How the pieces behave in the page
A few details about the rendering are worth knowing, because they explain behavior you will see in the browser.
- The
<details>element is a block-level box, so it takes the full available width and starts on a new line. - The
<summary>element is displayed like a list item by default. That is why it gets a marker (the triangle) in front of it, the same way a bullet appears in front of a list item. - The body is simply not rendered while the widget is collapsed. It is still in the document, so a script can read it and search tools can often find it.
- In some browsers, the find-in-page feature expands a collapsed widget when it finds a match inside it. Support differs between browsers, so don't depend on it for essential behavior.
Common errors
Most problems with this structure fall into a short list.
- Summary placed after other content. The label is ignored or the markup is invalid. Move
<summary>to be the first child. - No summary at all. The reader sees the generic "Details" label. Always provide one.
- A
<div>or<p>used as the label. Only a real<summary>toggles the widget. A paragraph inside<details>is just body content and is hidden when collapsed, so the widget has no useful label. - A closing tag missing. If you forget
</details>, the rest of the page can end up inside the body of the widget, and everything after it disappears when collapsed. Check indentation and validate. - Using
<details>for content that must always be visible. If readers must see the text, do not hide it. Use a normal section. - Wrapping every paragraph in its own widget. This makes readers click many times to read one document. Use a widget for secondary content, not for the main text.
The third error is common for people who have built disclosure widgets with scripts before. In a scripted version, any element can be the trigger. In the native version, the trigger is specifically <summary>.
A complete example
This page combines the rules above. It has a heading, three independent widgets, and one nested widget.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Help center</title>
</head>
<body>
<h1>Help center</h1>
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
<p>Items must be unused and in their original packaging.</p>
</details>
<details>
<summary>Shipping options</summary>
<ul>
<li>Standard: 5 to 7 business days</li>
<li>Express: 2 to 3 business days</li>
</ul>
<details>
<summary>International orders</summary>
<p>Delivery takes 10 to 15 business days and customs fees may apply.</p>
</details>
</details>
<details>
<summary>How do I contact support?</summary>
<p>Email [email protected] and we reply within one business day.</p>
</details>
</body>
</html>Open the page and check these behaviors:
- All three top-level widgets start collapsed.
- You can open more than one at the same time.
- The "International orders" widget is hidden until you open "Shipping options".
- Tab moves focus from one summary to the next, and Enter or Space toggles the focused one.
Summary of the chapter
<details>is the container and<summary>is the label. Everything else inside<details>is the hidden body.- The browser tracks the state with the
openattribute and toggles it when the reader activates the summary. - Put one
<summary>first in each<details>. Without it the browser shows a generic label. - Nested and repeated widgets each keep their own state.
- Only a real
<summary>can be the trigger. Other elements cannot take its place.
Exercise
A colleague writes the markup below and reports that the widget shows the label "Details" instead of "Warranty terms", and that the "Warranty terms" text is hidden when the widget is collapsed. Name the two problems in the markup and write the corrected version. <details> <p>Products are covered for one year.</p> <p>Warranty terms</p> </details>
Show answer
The first problem is that there is no <summary> element, so the browser supplies its generic default label ("Details"). The second problem is that "Warranty terms" is in a <p>, which is ordinary body content, so it is hidden when the widget is collapsed. Only a <summary> can act as the visible label. The corrected version is:
<details>
<summary>Warranty terms</summary>
<p>Products are covered for one year.</p>
</details>
Exercise
You place two sibling <details> elements in a page, each with its own summary and body, and no other attributes. If a reader opens the first one and then opens the second one, what is the state of the first one? What do you need to change if you want the first to close automatically?
Show answer
The first one stays open, because each <details> keeps its own independent state. To make opening one close the others, you need to give the elements a shared name attribute, which is covered in the chapter on exclusive accordions.