Sub topic 1
What a Disclosure Widget Is
A disclosure widget is a control that shows or hides a block of content when the user asks for it. FAQ lists, "Show more" links, and collapsible sections in settings pages are all disclosure widgets. This chapter defines the pattern, shows how it was built before HTML had native support, and introduces the details and summary elements that now provide it. Later chapters cover each feature of those elements in depth.
Prerequisites
This chapter assumes you can do the following:
- Write a basic HTML document with a
doctype,head, andbody, and use common elements such asp,button, andh2. - Read simple CSS rules made of selectors and properties.
- Select an element in JavaScript with
document.querySelectorand attach a listener withaddEventListener.
You do not need to know ARIA (the set of HTML attributes that describe widgets to assistive technology). The chapter explains the few attributes it uses.
The disclosure pattern
A disclosure has two visible parts and one piece of hidden state.
| Part | Role | Example |
|---|---|---|
| Trigger | The control the user activates | The question "What is your return policy?" |
| Panel | The content that appears or disappears | The answer text |
| State | Whether the panel is currently shown | Expanded or collapsed |
The trigger toggles the state. The state decides whether the panel is rendered. That is the whole pattern. Everything else in this tutorial, including styling, scripting, and grouping, builds on these three parts.
The pattern is useful for two reasons.
- It reduces clutter. A page with twenty FAQ answers is easier to scan when only the questions show.
- It puts the user in control. Readers who want the detail open it. Readers who do not skip it.
The pattern also has a cost. Hidden content is easy to miss, and every extra click is friction. Use it for secondary content, not for the main point of the page.
How disclosure differs from similar patterns
Several widgets look alike but behave differently. Choosing the wrong one confuses users and assistive technology.
| Pattern | What it does | Key difference from disclosure |
|---|---|---|
| Disclosure | Toggles one panel in place | Independent. Opening it affects nothing else. |
| Accordion | A stack of disclosures | Often allows only one open at a time. |
| Tabs | Switches between sibling panels | Exactly one panel is always visible. |
| Modal dialog | Overlays the page and blocks the rest | Traps focus and demands a response. |
| Tooltip | Shows a short hint on hover or focus | Not meant to hold interactive or long content. |
| Navigation menu | Reveals a list of links | Needs its own conventions for focus and dismissal. |
An accordion is a group of disclosures with an extra rule about exclusivity. A later chapter in this tutorial covers how to build that rule natively.
The traditional build: a button, a panel, and a script
Before native support, developers built disclosures from a button, a panel, and JavaScript. The following page is complete and runs as shown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Custom disclosure</title>
</head>
<body>
<button type="button" aria-expanded="false" aria-controls="return-policy">
What is your return policy?
</button>
<p id="return-policy" hidden>
You can return items within 30 days of delivery.
</p>
<script>
const button = document.querySelector('button');
const panel = document.getElementById(button.getAttribute('aria-controls'));
button.addEventListener('click', () => {
const expanded = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!expanded));
panel.hidden = expanded;
});
</script>
</body>
</html>Each piece does a specific job:
- A real
buttonelement gives you keyboard focus and activation with Enter and Space for free. Adivwith a click handler would give you neither. aria-expandedtells screen readers whether the panel is open. You must keep it in sync with the real state yourself.aria-controlslinks the button to the panel byid. Support in screen readers is inconsistent, so treat it as a hint.- The
hiddenattribute removes the panel from rendering and from the accessibility tree. - The script is the only thing that connects the trigger to the state.
This works, but it carries a maintenance burden. Three things must stay consistent: the button's attribute, the panel's attribute, and the script. If any one drifts, the widget breaks or lies to screen reader users. If JavaScript fails to load, the panel stays hidden forever and the content is unreachable.
The native build: details and summary
HTML provides the same pattern in two elements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Native disclosure</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>Open this page in a browser with no script and you get a working widget. The mapping to the three parts of the pattern is direct.
| Pattern part | Native element or feature |
|---|---|
| Trigger | The `summary` element |
| Panel | Everything else inside `details` |
| State | The boolean `open` attribute on `details` |
The details element is the container and owns the state. The summary element is its first child and acts as the visible label and the clickable control. The browser renders summary with a small triangle, called the disclosure marker, that points sideways when closed and down when open.
When the user clicks the summary, the browser adds the open attribute to details. Clicking again removes it. You can see this in the browser's developer tools, where the element's markup changes from <details> to <details open> as you click.
What the browser gives you for free
Compared with the custom version, the native elements handle these things without your code:
- The trigger is focusable and responds to Enter and Space.
- Screen readers announce the widget as a disclosure and report whether it is expanded or collapsed. Announcement wording varies by screen reader.
- The open or closed state lives in one place, the
openattribute, so it cannot drift out of sync with a second attribute. - The widget works with JavaScript disabled or failing.
- The default marker and basic layout need no CSS.
The widget as a state machine
A disclosure has exactly two states, and one action moves it between them. Thinking of it this way helps when you later read the state from JavaScript or style each state.
The initial state is Collapsed unless the author adds the open attribute. The next chapter covers that attribute. For now, note that "state" here is not a hidden JavaScript variable. It is an attribute in the document itself, which means CSS can select on it and scripts can read it.
What is and is not rendered when closed
When a details element is closed, the browser renders the summary and skips the rest of the contents. This has practical consequences.
- Layout. Closed content takes up no space and does not affect the layout of the page.
- Focus. Links, buttons, and form fields inside a closed panel cannot receive keyboard focus. Tabbing skips them.
- Accessibility tree. Screen readers do not read the hidden content until the widget opens.
- Find in page and links to fragments. Newer Chromium-based browsers open a closed
detailswhen a find-in-page match or a URL fragment points inside it. Other browsers may not, so do not rely on this for navigation.
The content is still in the document. Search engines and the DOM see it, and textContent returns it. Only rendering and focus are affected.
Common mistakes at this stage
Most beginner problems with disclosures come from a few habits.
- Building a trigger from a non-interactive element. A
divorspanwith a click handler is not focusable and has no role. Usebuttonin a custom build, orsummaryin a native one. - Forgetting to update
aria-expandedin a custom build. The widget looks right to sighted users, but a screen reader reports it as permanently collapsed. - Leaving the panel visible by default in HTML and hiding it with script. Users see a flash of open content on load, and if the script fails the widget never collapses. Start from the correct default in markup.
- Using a disclosure where tabs or a dialog is the right pattern. If exactly one panel must always show, use tabs. If the user must respond before continuing, use a dialog.
- Hiding a long list of primary navigation links. A site menu has focus and dismissal expectations that a plain disclosure does not meet by itself.
- Expecting a smooth open animation by default. The native widget switches instantly. Animation is possible but needs extra CSS or script, and it is outside this chapter.
Where the rest of the tutorial goes
This chapter established the vocabulary: trigger, panel, state. The remaining chapters take each part in turn.
- Building the widget and choosing its initial state with the
openattribute. - What the
summaryand the panel may contain. - Styling the summary, the marker, and the open state.
- Reading and setting the state from JavaScript, and reacting to the
toggleevent. - Grouping widgets into an exclusive accordion with the
nameattribute. - Keyboard and accessibility behavior.
- Deciding when native
detailsis enough and when a custom component is justified.
Quick check
Try these before moving on. Answer from memory first, then compare.
Exercise
Name the three parts of a disclosure widget, and say which native HTML feature plays each part.
Show answer
The trigger, the panel, and the state. The trigger is the summary element. The panel is the content inside details other than the summary. The state is the boolean open attribute on the details element: present means expanded, absent means collapsed.
Exercise
A teammate builds a disclosure from a div with a click handler that toggles the hidden attribute on a paragraph. Give two problems with this build and one way to fix them with native HTML.
Show answer
First, a div is not focusable, so keyboard users cannot reach or activate it, and it exposes no button or disclosure role to screen readers. Second, nothing tells assistive technology whether the panel is expanded, because there is no aria-expanded and no native state. It also stops working if the script fails. The fix is to replace the div, the paragraph, and the script with a details element that contains a summary as its trigger and the paragraph as the panel.
Exercise
You add a details element with a form field in its panel and leave it closed. A user presses Tab repeatedly from the summary. Does focus land on the form field? Why or why not?
Show answer
No. While details is closed, its panel content is not rendered, so elements inside it cannot receive focus and Tab skips them. The field becomes focusable only after the widget opens.
Exercise
Create a page with two separate details elements, each holding a question and an answer. Open the first, then the second. Does opening the second close the first? What does that tell you about plain details elements compared with an accordion?
Show answer
No. Both stay open, because each details element keeps its own independent state. A plain details element is a single disclosure. Making opening one close the others is an extra rule that defines an exclusive accordion, which a later chapter builds with the name attribute.