Sub topic 3
Starting open or closed with the open attribute
This chapter assumes you know basic HTML structure, CSS selectors, and how to select an element and add an event listener in JavaScript. You have already seen that a details element with a summary child hides its content until the user activates the summary. Here you will learn how the widget decides whether it starts expanded or collapsed, and how that state is stored.
The open attribute is the state
A details element has exactly one piece of state: whether it is open. The browser stores that state in a boolean attribute named open.
- If the
openattribute is present, the content is shown. - If the attribute is absent, only the
summaryis shown.
A boolean attribute has no meaningful value. Its presence means true and its absence means false. You write it without a value.
<details open>
<summary>Shipping information</summary>
<p>Orders ship within two business days.</p>
</details>This widget loads expanded. Remove the word open and it loads collapsed, which is the default.
The browser edits the attribute for you
The attribute is not only an initial setting. When the user clicks the summary, the browser adds or removes open on the element. The attribute always reflects the current state.
You can see this in your browser's developer tools. Load the page, open the Elements panel, and click the summary. The markup changes from <details> to <details open> and back as you toggle it.
Common mistakes with boolean attributes
Because the attribute works by presence, some habits from other attributes cause bugs.
- Writing
open="false"does not close the widget. The attribute is still present, so the widget is open. To start closed, leaveopenout. - Any value still counts.
open,open="", andopen="open"all mean open. - Do not set the state by changing a class. The browser only looks at the
openattribute. A class such as.is-openhas no effect on whether the content is shown unless you write extra code.
Choosing the initial state
Decide the starting state based on what the reader needs first.
| Situation | Start state |
|---|---|
| Optional detail, such as an FAQ answer | Closed (no `open`) |
| The most important section of a settings panel | Open |
| Content that must be visible when JavaScript or styles fail | Open |
| A long list the reader may skip | Closed |
Closed is the default because the widget exists to hide secondary content. Use open sparingly, for the item most readers will want first.
Setting the state from the server or a template
Since the state is an attribute, any templating system can set it. For example, a server can emit open only for the section the reader last viewed. This works without any JavaScript, because the browser reads the attribute while parsing the HTML.
<details open>
<summary>Account details</summary>
<p>Name, email, and password settings.</p>
</details>
<details>
<summary>Notification preferences</summary>
<p>Choose which emails you receive.</p>
</details>Here the first widget starts expanded and the second starts collapsed. Each widget keeps its own state, so opening one does not affect the other. Grouping widgets so that they do affect each other is covered in a later chapter.
Seeing the attribute from CSS
Because open is a real attribute, CSS can select on it with an attribute selector. This lets you style the expanded state without any script.
details[open] {
background: #f6f8fa;
}
details:not([open]) summary {
color: #555;
}The first rule colors the background only while the widget is expanded. The second rule dims the summary text while it is collapsed. A later chapter covers styling in more detail. For now, note that the selector works because the browser keeps the attribute in sync with what the user sees.
Checking the state in JavaScript
You can also read the attribute from a script. The hasAttribute method returns true when open is present.
const details = document.querySelector('details');
console.log(details.hasAttribute('open')); // true or falseThis works, but the element also exposes an open property that is easier to use and can be set as well. That property is the subject of the chapter on controlling the state from JavaScript, so this chapter stops at reading the attribute.
Putting it together
Follow these steps to confirm the behavior yourself.
- Create an HTML file with two
detailselements, one withopenand one without. - Open it in a browser and check that the first is expanded and the second is collapsed.
- Open developer tools and click both summaries. Watch the
openattribute appear and disappear. - Change one of them to
open="false"and reload. Confirm it still starts expanded.
Exercise
A developer writes <details open="false"> and expects the widget to start collapsed. What happens, and how should they fix it?
Show answer
The widget starts expanded, because open is a boolean attribute and is true whenever it is present, whatever its value. To start collapsed, remove the open attribute entirely.
Exercise
A user clicks the summary of a closed details element. What changes in the DOM, and which CSS selector matches the element afterward?
Show answer
The browser adds the open attribute to the details element. The selector details[open] matches it.