← Back to HTML & CSS, page 7

html-css-0128 · Easy fresher

Build an accordion with a button, aria-expanded and a panel. How does it differ from details?

Put a button inside a heading, set aria-expanded on the button and hide the panel with the hidden attribute, toggling both in a short script. details and summary give the same result with no script and are the first choice for simple cases.

Full answer

An accordion is a list of headings that each open and close a panel. The simple, accessible pattern uses these parts:

  • A heading element for each item, such as h3, so that screen reader users can move by heading.
  • A real button inside the heading. It is focusable and works with Enter and Space, with no extra code.
  • aria-expanded="false" or "true" on the button tells assistive technology the state.
  • aria-controls can point to the panel. Its support is uneven, so the aria-expanded state matters most.
  • The panel is hidden with the hidden attribute when closed. A hidden panel is not focusable and is not read.

A short script toggles the attribute and the hidden state. The CSS can read the attribute, for example to rotate an arrow.

How is it different from details and summary? A details element does the same job with no JavaScript and keeps its state in the browser. It is the first choice for simple cases such as a list of questions. A custom accordion gives more control, such as animating the height, letting only one panel stay open, or reading the state in a framework. If you only need "one open at a time", note that the name attribute on details can group them, so check the support table before writing a script.

Clicking Fees opens its panel and the plus changes to a minus. Clicking it again closes the panel. Each button also works with the keyboard.

Use the simplest tool. If details is enough, use details.

html
<style>
  .acc button { width: 100%; text-align: left; padding: 8px; }
  .acc button[aria-expanded="true"]::after { content: " -"; }
  .acc button[aria-expanded="false"]::after { content: " +"; }
</style>
<div class="acc">
  <h3><button type="button" id="b1" aria-expanded="false" aria-controls="s1">Fees</button></h3>
  <div id="s1" hidden>Pay monthly.</div>
  <h3><button type="button" id="b2" aria-expanded="false" aria-controls="s2">Refunds</button></h3>
  <div id="s2" hidden>Within 7 days.</div>
</div>
<script>
  document.querySelectorAll(".acc button").forEach(b => b.addEventListener("click", () => {
    const open = b.getAttribute("aria-expanded") === "true";
    b.setAttribute("aria-expanded", String(!open));
    document.getElementById(b.getAttribute("aria-controls")).hidden = 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

  • Use a heading with a real button inside.
  • aria-expanded holds the state.
  • The hidden attribute hides the closed panel.
  • CSS can read aria-expanded for icons.
  • details is simpler when it is enough.

Common mistakes

  • Using a div or span as the header, which has no keyboard support.
  • Hiding the panel with CSS only but leaving aria-expanded unchanged.
  • Building a custom accordion where details would do.

Follow-ups

  • Why put the button inside a heading?
  • How would you allow only one panel open at a time?

Tips

  • Say the three parts: heading, button, hidden panel.
  • Compare with details and say when each fits.