Sub topic 2
Building one with details and summary
This chapter shows how to build a working disclosure widget with <details> and <summary>. It assumes you know basic HTML document structure and common elements. Later chapters cover the open attribute in depth, styling, JavaScript control, and accordions, so this chapter stays on structure and behavior.
The two elements
A disclosure widget needs two elements:
<details>is the container. It holds all the content of the widget and tracks whether it is expanded or collapsed.<summary>is the visible label. It is the control the user activates to expand or collapse the widget.
The browser supplies the toggle behavior, a disclosure triangle, and keyboard support. You write no script.
<!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 an .html file and open it in a browser. You see only the summary text with a triangle beside it. Clicking the summary reveals the paragraph. Clicking it again hides the paragraph.
How the browser renders it
The browser treats the first <summary> child of <details> as the label. Everything else inside <details> is the hidden body. When the widget is collapsed, the browser does not render the body. When it is expanded, the body appears below the summary.
In the collapsed state only the summary is visible. In the expanded state the summary stays visible and the body follows it.
Structure rules
Three rules keep the widget working.
- Put
<summary>as the first child of<details>. The browser only treats the first<summary>child as the label. A<summary>placed later is not used as the control. - Use only one
<summary>per<details>. Extra ones add nothing. - Put the hidden content after the summary, inside the same
<details>.
A body with more than one element
The hidden body can hold any number of elements, not just one paragraph. You do not need a wrapper element.
<details>
<summary>What does the warranty cover?</summary>
<p>The warranty covers manufacturing defects for one year.</p>
<ul>
<li>Battery failure</li>
<li>Screen faults</li>
<li>Charging port faults</li>
</ul>
<p>It does not cover accidental damage.</p>
</details>All three siblings after the summary are hidden and shown together.
Several widgets on one page
Each <details> keeps its own state. By default, opening one does not close the others. This is useful for a list of independent questions.
<details>
<summary>Do you ship internationally?</summary>
<p>Yes, to most countries.</p>
</details>
<details>
<summary>Can I change my order after paying?</summary>
<p>You can change it within one hour of checkout.</p>
</details>A reader can open both at once. If you want opening one to close the others, you need the name attribute, which a later chapter covers.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| No `<summary>` | Browser shows a generic default label | Add a `<summary>` as the first child |
| `<summary>` not first | The label is not recognized as the control | Move it to the top of `<details>` |
| Putting `<summary>` outside `<details>` | It is just an ordinary element with no toggle behavior | Nest it inside `<details>` |
| Expecting the body to be searchable while closed | Behavior varies by browser | Test in your target browsers |
Try it yourself
Build a small FAQ with three <details> elements, each with its own summary and a body of at least one paragraph. Open the page, then check three things:
- Each widget toggles by itself.
- You can open all three at the same time.
- Pressing Tab moves focus to a summary, and pressing Enter or Space toggles it.
The last check confirms that the browser provides keyboard support without extra code. A later chapter covers this in more detail.
Exercise
A developer writes a <details> element with a <p> as its first child and the <summary> as its second child. What problem does this cause, and how do you fix it?
Show answer
The browser only uses the first <summary> child as the label, so a <summary> placed after other content is not treated as the control. Move the <summary> so it is the first child of <details>, and leave the <p> after it.
Exercise
You build a page with four <details> elements and no extra attributes. A user opens the first two. How many widgets are open, and why?
Show answer
Two are open. Each <details> keeps its own state, and without a shared name attribute, opening one does not close the others.