Explain it simply
Explain it simply
<details> is like a folded note. At first you only see its title, the <summary>; click it and the note unfolds to show the answer inside, and click again to fold it back.
The best part is that the browser provides all of this behaviour itself. You do not write a single line of JavaScript. It works with a mouse click, and from the keyboard you reach it with Tab and open it with Enter or Space. Screen readers also announce whether it is expanded or collapsed.
Problem → Why → Solution
The problem: an FAQ that only works with a mouse
An online shop builds its FAQ from two <div>s and an onclick handler. When the developer tests with a mouse, the answer opens and closes nicely.
A customer who uses the keyboard presses Tab and skips right past the question, because a <div> cannot receive focus. A screen reader reads the question as plain text and never says it can be activated or that an answer is hidden inside. The question about delivery costs is already answered, yet the customer ends up emailing support.
<!-- Not focusable, no expanded/collapsed state for screen readers -->
<div class="faq-question"
onclick="this.nextElementSibling.hidden = !this.nextElementSibling.hidden">
Do you deliver to Mandalay?
</div>
<div class="faq-answer" hidden>Yes, in 2-3 working days.</div>Why this happens
A <div> has no role, cannot take focus and ignores keyboard activation. You would have to rebuild all of that yourself with tabindex, a role, aria-expanded and key handlers. <summary> ships with every one of those built in and tested in every browser.
Core concept: a disclosure widget the browser builds for you
A disclosure widget is a control that shows or hides a piece of content. HTML builds it from two elements: <details> is the container around everything, and <summary> is the always-visible label that also acts as the toggle. Here is how it works, step by step.
1. <summary> must be the first child
The browser uses the first <summary> child of <details> as the toggle and hides all the remaining content while closed. Without a <summary>, the browser supplies a default label such as "Details", so a meaningless word appears where your question should be.
2. The open attribute is the state
When the widget opens, the browser adds the open attribute to <details>, and it removes the attribute when the widget closes. Writing open in your HTML makes it start expanded. In CSS you can style the expanded item with details[open], and in JavaScript you can use the element.open property and the toggle event.
3. Keyboard and screen reader support come free
<summary> takes focus and toggles with Enter or Space, and assistive technology announces it like a button with an expanded or collapsed state. Keep the summary text short and clear, because all of it becomes the button's name.
4. name turns a group into an accordion
Give several <details> the same name and only one of them can be open at a time: opening one closes the others automatically. Current browsers support this attribute, and older ones simply ignore it and let several items stay open, which is a harmless fallback.
| You need | Use |
|---|---|
| FAQ, optional settings, extra detail | <details> / <summary> |
| One panel of many, chosen with arrow keys | Tabs (ARIA tabs pattern) |
| A box that blocks the page behind it | <dialog> |
- <summary> is the folded title
- Click, Enter or Space toggles it
- The browser flips the open attribute
- Same name: only one stays open
Closed content still exists, it is just not shown, so never hide information people must see to continue.
General pattern
<details name="group-name" open>
<summary>Short label that toggles</summary>
Any content: paragraphs, lists, images, links
</details><summary> comes first. open (optional) starts the item expanded, and name (optional) links items with the same name into an accordion.
A single FAQ item
The code below is one complete FAQ question. It has no CSS and no JavaScript, yet in the browser it is already a working widget. After trying it with the mouse, try it with Tab and Enter as well.
<details>
<summary>Is HTML difficult?</summary>
<p>At first there seem to be many tags, but practice makes them fit together like building blocks.</p>
</details>Code breakdown
- The container
<details>There is no open attribute, so it starts collapsed. Change it to <details open> and the answer is visible as soon as the page loads.
- The toggle and its label
<summary>As the first child it becomes the toggle, and the browser draws the ▶ marker. The question text becomes the button's name, which is what a screen reader reads out.
- The hidden content
<p>Everything after <summary> is the collapsible content. It can be more than one paragraph: lists, images and links are fine too.
- Closing the widget
</details>This ends the widget. Every further FAQ question gets its own new <details> element.
At first you see one line: a small ▶ triangle and "Is HTML difficult?". Clicking it (or pressing Enter or Space while it has focus) turns the triangle into ▼ and reveals the answer paragraph below; clicking again hides it.Styling the marker and the open state
By default <summary> is displayed as a list item, so the ▶ is a list marker. You can recolour it with summary::marker or remove it with list-style: none; older Safari versions also need ::-webkit-details-marker hidden. The details[open] selector matches only expanded items, so you can add a border or bold text to show which one is open. If you remove the marker, add your own indicator such as "+", or people will not know it can be clicked.
<style>
summary { cursor: pointer; font-weight: 600; }
summary::marker { color: #2563eb; }
details[open] { border-left: 3px solid #2563eb; padding-left: 8px; }
</style>
<details>
<summary>What payment methods do you accept?</summary>
<p>KBZPay, WavePay and cash on delivery.</p>
</details>In real projects
In real projects
In GitHub README files you often see long logs or installation details folded away inside <details>. The "Size guide" and "Shipping and returns" sections on shop product pages, and "Show advanced options" in documentation sites, use the same pattern. Because no JavaScript is involved, it works before scripts load and even when a script has crashed.
The main question teams have to answer is whether the content should be hidden at all. Prices, form error messages and terms people must agree to get skipped when they are folded away, so fold only optional detail. Also check printing: closed content may not appear on paper, and when that matters teams add open with a little JavaScript before printing.
Common mistakes
Common mistakes
Putting a link or button inside <summary>
<summary> is already a button, so a link inside it nests one interactive element in another. A click may toggle or navigate, and screen readers get confused about what the control is. Move the link into the revealed content.
Hiding information people need
When the delivery fee hides behind "More info", shoppers are surprised at checkout and abandon the cart. Information people need for a decision should always be visible.
Using it as tabs or a navigation menu
Tabs have their own keyboard contract, such as arrow keys and exactly one visible panel. Styling <details> to look like tabs breaks the behaviour people expect, so choose the matching pattern instead.
Tracking the state with a separate class
If JavaScript adds an .is-open class without touching the open attribute, the visual state and the real state drift apart. Use element.open, and select details[open] in CSS.
Best practices
Best practices
- Write each summary as the question a visitor would actually ask; words like "Click here" or "More" do not say what will open
- In a long FAQ, open the most common question by default and keep the rest closed
- Write answers as complete sentences so they still make sense with CSS switched off
Mini challenge
A delivery FAQ accordion
Build three delivery FAQ questions for an online shop. Only one answer may be open at a time, and the first one starts open.
- Give all three <details> the same name="delivery-faq"
- Add the open attribute to the first item
- Put the link to the rates page inside an answer, not inside <summary>
- Use details[open] summary in CSS to colour the expanded item
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Delivery FAQ</title>
<style>
/* TODO: colour the summary of the open item */
</style>
</head>
<body>
<h1>Delivery questions</h1>
<!-- TODO: Q1 "Where do you deliver?" - starts open -->
<!-- TODO: Q2 "How much does delivery cost?" - answer links to rates.html -->
<!-- TODO: Q3 "Can I change my address?" -->
<!-- All three share one name so only one stays open -->
</body>
</html>Expected: On load the first question is open with a blue summary and the other two are closed. Clicking the second question opens it and the first one closes automatically. The "delivery rates" link appears inside the second answer.
Show hint
open and name go on <details>, not on <summary>. Open the second question and check that the first one closes by itself.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Delivery FAQ</title>
<style>
summary { cursor: pointer; }
details[open] summary { color: #1d4ed8; font-weight: 600; }
</style>
</head>
<body>
<h1>Delivery questions</h1>
<details name="delivery-faq" open>
<summary>Where do you deliver?</summary>
<p>Yangon, Mandalay and Naypyitaw, plus most towns by courier.</p>
</details>
<details name="delivery-faq">
<summary>How much does delivery cost?</summary>
<p>It depends on the town. See our <a href="rates.html">delivery rates</a>.</p>
</details>
<details name="delivery-faq">
<summary>Can I change my address?</summary>
<p>Yes, until the order is packed. Reply to your confirmation email.</p>
</details>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- <details> is the container and its first child <summary> is the always-visible toggle
- The browser provides keyboard, focus and expanded/collapsed state without JavaScript
- open is the state: style it with details[open] and control it with element.open
- A shared name makes an accordion; keep links and buttons out of <summary>