Sub topic 1
Disclosure Widgets with `details` and `summary`
Prerequisites
This chapter assumes basic HTML document structure and common elements. You should also understand basic CSS and JavaScript, although this chapter builds the widget without JavaScript.
What a disclosure widget is
A disclosure widget is an interface control that shows or hides related content. A user activates a visible label to reveal additional information. Common examples include FAQs, shipping details, and help text.
HTML provides a native disclosure widget through the <details> and <summary> elements. “Native” means the browser supplies the interaction and basic keyboard behavior. You do not need to build the state management yourself.
Building one with `details` and `summary`
Place the visible control in a <summary> element inside <details>. Put the content that can be revealed after the summary. The summary should be the first child of the details element.
<!doctype html>
<html lang="en">
<body>
<details>
<summary>What is your return policy?</summary>
<p>You can return items within 30 days of delivery.</p>
</details>
</body>
</html>When the page loads, the paragraph is hidden. Activating the summary expands the <details> element. Activating it again collapses the element.
The summary should identify the hidden content clearly. Keep the label short enough to scan, and place the full explanation in the body. A details element can contain paragraphs, lists, links, and other suitable flow content.
Starting open or closed with the `open` attribute
A details element starts collapsed unless it has the boolean open attribute. A boolean attribute is enabled by its presence. Its value does not need to be "true".
<details open>
<summary>Shipping information</summary>
<p>Orders ship within two business days.</p>
</details>This example starts expanded. The browser adds or removes the open attribute as the user toggles the widget. Later chapters show how JavaScript reads and changes this state.
Exercise
How do you make a disclosure widget start expanded?
Show answer
Add the boolean open attribute to its details element.