← Back to HTML & CSS, page 4

html-css-0068 · Easy fresher

How do you build an accordion with details and summary, and what do you get for free?

details and summary form a built-in disclosure widget with keyboard support, an open attribute, a toggle event and correct announcements. Elements sharing a name attribute can behave as an exclusive accordion in browsers that support it.

Full answer

The details element is a built-in show and hide widget. Its first child summary is the always-visible label. Everything else inside details is shown only when the widget is open. Clicking the summary, or pressing Enter or Space when it has focus, toggles it.

What you get with no JavaScript:

  • It is reachable with the keyboard, and screen readers announce it as expandable with its open or closed state.
  • The open attribute shows whether it is open. Add open in the HTML to start expanded.
  • A toggle event fires on change, so scripts can react.

To make an exclusive accordion where opening one item closes the others, give the details elements the same name attribute. This is newer, so check the support table. Without it, you need a small script.

You can style the marker with summary::marker, or hide it with list-style: none on summary and add your own icon. Animating the open and close needs extra work, because the content jumps between display states.

The first item starts open. Because both share name="faq", opening the second closes the first and fires the toggle event, which updates the log line.

Use details for FAQs and simple disclosures. For navigation menus with complex behaviour, such as arrow-key movement, you still need script and ARIA.

html
<style>
  details { border: 1px solid #999; border-radius: 6px; padding: 0.5rem 1rem; margin-bottom: 0.5rem; }
  summary { cursor: pointer; font-weight: bold; }
  summary:focus-visible { outline: 3px solid #0a6cff; outline-offset: 2px; }
  details[open] summary { margin-bottom: 0.5rem; }
</style>
<details name="faq" id="q1" open>
  <summary>How long is the course?</summary>
  <p>Twelve weeks, with two live sessions each week.</p>
</details>
<details name="faq" id="q2">
  <summary>Do I get a certificate?</summary>
  <p>Yes, after you finish the final project.</p>
</details>
<p id="log"></p>
<script>
  document.getElementById("q2").addEventListener("toggle", e => {
    document.getElementById("log").textContent = "q2 open: " + e.target.open;
  });
</script>

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

  • summary is the visible label; the rest of details is the hidden content.
  • The open attribute shows and sets state; the toggle event reports changes.
  • Keyboard and screen reader support are built in.
  • Elements with the same name form an exclusive group, where supported.
  • Style the marker with summary::marker or replace it with your own icon.

Common mistakes

  • Putting something other than summary as the first child. The browser then adds a default label, such as Details.
  • Removing the summary focus outline and leaving keyboard users without a visible focus state.
  • Rebuilding the widget with divs and scripts when details already provides the behaviour and accessibility.

Follow-ups

  • How do you make only one details open at a time?
  • How do you detect when a details element is opened by script?

Tips

  • Mention what you do not need to write: no script, no ARIA state, no key handling.
  • Say that the name attribute for exclusive groups is newer and check support before depending on it.