HTML Details and Summary
HTML <details> Element
The HTML <details> element creates a box that the user can open and close.
The <summary> element is the visible title of the box.
The user clicks the title to show or hide the rest of the content.
The browser does all the work, so you do not need any JavaScript.
Details boxes are often used for:
- questions and answers (FAQ)
- extra information that not every reader needs
- long content that you want to keep short at first
Example
A details box with a title. Click the title to open and close it:
<details>
<summary>What is HTML?</summary>
<p>HTML is the standard language for creating web pages.</p>
</details>
Try it Yourself »
How it Works
- The
<details>element holds all the content. Only the title shows at first - The
<summary>element must be the first child of<details> - The browser adds a small triangle in front of the title. It points down when the box is open
Note: If you leave out the <summary> element, the browser shows the word "Details" as the title.
Open by Default
A details box is closed when the page loads.
Add the open attribute to show the content from the start:
Example
<details open>
<summary>What is HTML?</summary>
<p>HTML is the standard language for creating web pages.</p>
</details>
Try it Yourself »
The user can still close the box.
Create an Accordion
An accordion is a list of boxes where only one box is open at a time.
Give several <details> elements the same name attribute.
When the user opens one box, the browser closes the others:
Example
<details name="faq">
<summary>What is HTML?</summary>
<p>HTML is the standard language for creating web pages.</p>
</details>
<details name="faq">
<summary>What is CSS?</summary>
<p>CSS describes how HTML elements look on the screen.</p>
</details>
<details name="faq">
<summary>What is JavaScript?</summary>
<p>JavaScript makes web pages interactive.</p>
</details>
Try it Yourself »
Note: The name attribute on <details> is new.
In older browsers, the boxes still work, but more than one box can be open at the same time.
Styling Details and Summary
You can style <details> and <summary> with CSS, just like any other element.
The details[open] selector styles the box only when it is open:
Example
details {
border: 1px solid #cccccc;
border-radius: 5px;
padding: 8px 16px;
margin-bottom: 8px;
}
summary {
font-weight: bold;
cursor: pointer;
}
details[open] {
background-color: #f1f1f1;
}
Try it Yourself »
Style the Triangle
The triangle in front of the title is a marker, like the bullet in a list.
Use the CSS ::marker pseudo-element to change its color:
Accessibility
A details box works with the keyboard by itself.
Users can move to the title with Tab, and open or close the box with Enter or Space.
Screen readers tell the user if the box is open or closed.
Keep the title short and clear, so users know what is inside before they open it.
Tip: Do not put links or buttons inside <summary>.
A click on them would also open or close the box, which confuses users.
Use <details> or <dialog>?
Both elements show and hide content, but they are made for different jobs.
The dialog element is explained in the HTML Dialog chapter.
| <details> | <dialog> | |
|---|---|---|
| Where the content shows | In the page, right under the title | In a box on top of the page |
| Needs JavaScript | No | Optional |
| Blocks the page | No | Yes, when opened with showModal() |
| Good for | FAQs, extra information, long content | Questions the user must answer, forms, warnings |
Summary
<details>creates a box that the user can open and close<summary>is the title of the box, and must come first- The
openattribute shows the content when the page loads - Boxes with the same
namework as an accordion
HTML Tag Reference
| Tag | Description |
|---|---|
| <details> | Creates a box that the user can open and close |
| <summary> | Defines the visible title of a <details> element |
For a complete list of all available HTML tags, visit our HTML Tag Reference.