Collapsible content with details and summary
Wrap content in <details> and put a <summary> first. The summary stays visible; everything else is hidden until the reader clicks it:
Click the question to open it, and again to close it. The browser adds the triangle marker, makes the summary focusable, toggles it with Enter or Space, and tells screen readers whether it is expanded or collapsed. No JavaScript is involved.
If you leave out <summary>, the browser shows a default label ("Details" in English) instead.
Open by default
The boolean open attribute shows the content from the start. The browser adds and removes open as the reader toggles, so it is also the hook for styling:
The open box has a blue border from details[open]. Open "Returns" and it turns blue too; both can be open at the same time.
Accordion: only one open at a time
Give several <details> elements the same name and they become an exclusive group: opening one closes the others. This used to take JavaScript and now works with the attribute alone in current Chrome, Edge, Firefox and Safari:
Open the second question and the first one closes. Remove the name attributes and they go back to opening independently. An exclusive accordion hides content the reader might want to compare, so use it when sections are alternatives, and plain <details> when people may want several open.
Styling the marker
The triangle is a list marker on the summary. list-style: none removes it (the ::-webkit-details-marker rule covers Safari), and a pseudo-element can draw your own indicator:
The plus sign rotates 45 degrees into a cross when the section opens. Keep a visible focus style like the :focus-visible outline here: once you remove the default marker, the focus ring is what keyboard users rely on.
The toggle event and the open property
JavaScript can read and set details.open, and the toggle event fires after each change, whether a click, a key or a script caused it:
"Expand all" sets open on both, and the log shows the last toggle event. The event does not bubble, so listen on each <details> element rather than on a parent.
What goes in a summary
A summary holds text, inline elements, or a heading when the section title belongs in the page outline:
<details>
<summary><h3>Refund policy</h3></summary>
<p>...</p>
</details>
Avoid links, buttons and form fields inside <summary>: the summary is already a control, so a control inside it is easy to miss or hit by mistake, and screen readers announce nested controls poorly. Put interactive content in the body of the <details> instead.
Common mistakes
- Building a toggle with a
<div>and JavaScript.<details>already handles clicks, keys and screen reader state. When you do need a custom toggle, use a button witharia-expanded. - Putting
<summary>anywhere but first. It must be the first child of<details>. - Hiding the marker without a replacement. Give readers some sign that the summary opens, like the plus sign above.
- Using
displayto hide the content. Toggleopeninstead; CSS that forces the content visible or hidden fights the browser's state. - Exclusive groups for content people compare. Use
nameonly when one open section at a time makes sense.
Frequently Asked Questions
What do details and summary do in HTML?
<details> creates a disclosure widget: a section that can be opened and closed. Its first child, <summary>, is the always-visible label; clicking it (or pressing Enter or Space when it has focus) shows or hides everything else inside <details>.
How do I make a details element open by default?
Add the boolean open attribute: <details open>. The browser adds and removes that attribute as the user toggles it, so CSS can style details[open].
How do I make an accordion where only one section is open?
Give every <details> in the group the same name attribute, for example name="faq". Opening one closes the others in that group, with no JavaScript.
How do I remove or change the arrow in a details summary?
Set list-style: none on summary and add summary::-webkit-details-marker { display: none; } for Safari. Then draw your own indicator, for example with summary::after, and change it with details[open] summary::after.
Can I detect when a details element opens?
Yes. Listen for the toggle event on the <details> element and read its open property. Setting details.open = true in JavaScript opens it.