Full answer
Basic use of details is easy, but some behaviours surprise people.
- The first child that matters is
summary. If there is nosummary, the browser makes a default label such as "Details". Only the firstsummarychild is used as the label. - The content of
summaryis treated as phrasing content. Putting a heading in it is allowed in current HTML, but it changes how assistive tools announce it, and interactive elements such as a link or a button inside asummaryare a poor idea because the wholesummaryis already a button. - State: the
openattribute is the state. Settingdetails.open = truefrom a script opens it, and removing the attribute closes it. Thetoggleevent fires after the state changes and is asynchronous, so it can fire later than the code that setopen. It fires for changes by the user and by script. Abeforetoggleevent exists in newer browsers for popovers and dialogs, but not fordetails. - Keyboard:
summaryis focusable and Enter or Space toggles it. - Find in page: in current Chromium browsers, text inside a closed
detailscan be found by the browser's search, and thedetailsopens to show the match. Support differs by browser, so do not rely on it for important content. - Hiding the arrow: use
summary { list-style: none; }, orsummary::marker { content: ""; }, and draw your own indicator with CSS usingdetails[open]. Keep a clear sign that the element can be opened. - Animation: the hidden content is not rendered, so a simple height transition does not run. The grid row technique or the newer pseudo-element for the content part can help, but check the support table.
- Nesting is allowed, and each details keeps its own state. The
nameattribute groups elements so that only one is open in supported browsers. - Printing: closed content is normally not printed. Open the elements for print with a script on
beforeprintif all content should appear.
Closed, the paragraph takes no space. A click on the summary opens it and fires one toggle event. Pressing Enter on the focused summary also toggles it. The plus sign changes to a minus without any script.
Test the find-in-page and printing behaviour in the browsers you support.
html
<style>
summary { cursor: pointer; list-style: none; }
summary::before { content: "+ "; }
details[open] > summary::before { content: "- "; }
</style>
<details id="d"><summary id="s">More</summary><p id="p">Hidden text</p></details>
<script>
window.events = 0;
d.addEventListener("toggle", () => { events++; });
</script>Checked with a mouse click and a real Enter key press in Chrome. Find in page and printing were not tested. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Only the first summary is the label.
- open is the state; toggle is asynchronous.
- Do not put links or buttons in summary.
- Closed content is not rendered, so height transitions need another technique.
- Test find-in-page and print.
Common mistakes
- Putting links or buttons inside summary.
- Expecting the toggle event to fire synchronously after setting open.
- Hiding the marker and leaving no sign that the content can be opened.
Follow-ups
- Is the toggle event synchronous?
- How do you show all details content in print?
Tips
- Pick three or four edge cases: toggle timing, find in page, animation, print.
- Mention that the whole summary acts as a button.