← Back to HTML & CSS, page 9

html-css-0179 · Medium mid

What edge cases should you know about the details element, beyond opening and closing?

The open attribute is the state, and the toggle event fires asynchronously after changes by user or script. summary should not contain interactive content, closed content is not rendered, so animation needs a different technique, and find-in-page and print behaviour must be tested.

Full answer

Basic use of details is easy, but some behaviours surprise people.

  • The first child that matters is summary. If there is no summary, the browser makes a default label such as "Details". Only the first summary child is used as the label.
  • The content of summary is 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 a summary are a poor idea because the whole summary is already a button.
  • State: the open attribute is the state. Setting details.open = true from a script opens it, and removing the attribute closes it. The toggle event fires after the state changes and is asynchronous, so it can fire later than the code that set open. It fires for changes by the user and by script. A beforetoggle event exists in newer browsers for popovers and dialogs, but not for details.
  • Keyboard: summary is focusable and Enter or Space toggles it.
  • Find in page: in current Chromium browsers, text inside a closed details can be found by the browser's search, and the details opens 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; }, or summary::marker { content: ""; }, and draw your own indicator with CSS using details[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 name attribute 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 beforeprint if 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.