Sub topic 1
What a disclosure widget is
Prerequisites
This chapter assumes you know three things:
- Basic HTML document structure and common elements such as
p,ulanda. - Basic CSS selectors and properties.
- Basic JavaScript: selecting an element and adding an event listener.
You do not need to know anything about accessibility or ARIA yet. Later chapters cover those.
The pattern
A disclosure widget is a control that shows or hides a block of content. The user sees a short label. When they activate it, the rest of the content appears. When they activate it again, the content is hidden.
You have seen this pattern in several places:
- FAQ lists, where the question is the label and the answer is hidden.
- "Show more" links under long product descriptions.
- Collapsible sections in documentation, such as a long stack trace or a table of optional settings.
The pattern has two parts and two states.
| Part | Role |
|---|---|
| Label | Always visible. The user activates it to change the state. |
| Body | The content that is hidden while the widget is collapsed. |
| State | What the user sees |
|---|---|
| Collapsed | The label only. |
| Expanded | The label and the body. |
A widget like this is called a disclosure because activating the label discloses content that was already on the page.
Building one the old way
Before HTML had a native element for this, developers built disclosures from several pieces. You needed a button for the label, a container for the body, a click handler to toggle a class, and CSS to hide the container when the class was missing. For screen readers, you also had to keep an aria-expanded attribute in sync with the visual state and link the button to the body with aria-controls.
That approach works, but each piece is a place to make a mistake. If the script fails to load, the content stays hidden and nobody can read it. If you forget aria-expanded, assistive technology reports nothing useful about the state. If you use a div instead of a button, keyboard users cannot reach the control.
The native version
HTML now has this pattern built in, using the details and summary elements. The summary element is the label. The details element wraps the label and the body.
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
</details>This is a complete, working widget. The page needs no CSS and no JavaScript. The browser does the following for you:
- It shows the
summarytext with a small triangle beside it, called the marker. - It hides every other child of
details. - When the user clicks the summary, it reveals the hidden children and rotates the marker.
- When the user clicks again, it hides them.
The browser stores the current state in an attribute on the details element. You will study that attribute in detail in a later chapter.
What you get without writing code
The native elements give you behavior that you would otherwise write yourself.
- Toggling. The click handler is built in.
- Keyboard support. The summary can receive focus with the Tab key, and Enter or Space toggles it.
- Semantics. Browsers expose the summary to assistive technology as a control that is expanded or collapsed.
- Resilience. The widget does not depend on a script, so it still works if JavaScript fails to load.
- Find in page. In current browsers, searching the page with Ctrl+F or Cmd+F can find text inside a collapsed body and expand the widget to show the match. Support varies, so check the browsers you target.
A common mistake
The most frequent error is putting the label in the wrong place. The browser looks for a summary that is the first child of details. If you leave it out, the browser makes up a default label, usually the word "Details". If you place it after other content, it is not treated as the label.
<!-- Wrong: the paragraph comes first, so summary is not the label -->
<details>
<p>You can return items within 30 days of delivery.</p>
<summary>What is your return policy?</summary>
</details>Always make summary the first child.
What comes next
The remaining chapters build on this structure. You will set the starting state, style the widget, control it from JavaScript, react to changes, group several widgets into an accordion, and decide when a custom component is the better choice.
Exercise
A teammate builds a collapsible section from a div, a button and a click handler that toggles a CSS class. Name two problems this has compared with details and summary, and show the markup you would use instead for a section labelled "Shipping information" containing the text "Orders ship within two business days."
Show answer
Possible problems: the content stays hidden if the script fails to load; the author must keep aria-expanded in sync by hand; a non-button element would not be reachable by keyboard; the toggle logic must be written and maintained. Replacement markup: <details><summary>Shipping information</summary><p>Orders ship within two business days.</p></details>
Exercise
In the following markup, what does the user see when the page loads, and what is wrong with it? <details><p>Hello</p><summary>Greeting</summary></details>
Show answer
The widget loads collapsed. Because summary is not the first child, the browser does not use it as the label and shows a default label such as "Details" instead. Move summary to be the first child of details.