Details and Summary: Accordions, Disclosure Widgets, and the Name Attribute

Details and summary build accordions in HTML: the open attribute, toggle event, exclusive groups with name, styling the marker, accessibility, find-in-page.

Executive Summary: <details> and <summary> give you a working disclosure widget with keyboard support, an expanded state for screen readers and a toggle event, without JavaScript. Several of them make an accordion. Giving them the same name makes an exclusive accordion where opening one closes the others, and with Firefox 130 shipping it in September 2024, that now works in every major browser. Style the marker, keep interactive elements out of the summary, and think about printing and deep links before you hide content by default.
Three accordion rows with the first one expanded

FAQ sections, “show more” panels, filter groups and settings sections all use the same pattern: a heading you click to reveal content. Most implementations are a div with a click handler that toggles a class, plus whatever accessibility work the developer remembered to do. Frequently that means no keyboard support, no expanded state and a page full of hidden answers that the browser’s Find feature cannot see.

The <details> element is a native disclosure widget: content that is hidden until the user opens it. Its first child, <summary>, is the always-visible label that toggles it. The open attribute reflects the current state. Since 2024, the name attribute groups several details elements so only one can be open at a time.

How do details and summary work?

<details>
  <summary>What payment methods do you accept?</summary>
  <p>UPI, credit and debit cards, net banking and bank transfer for annual plans.</p>
</details>

Without a line of script, this is focusable, opens and closes with Enter, Space or a click, announces itself to screen readers as a collapsed or expanded control, and shows a disclosure triangle that rotates. Add the open attribute to start expanded.

Reacting to changes takes one event:

details.addEventListener("toggle", () => {
  if (details.open) analytics.track("faq_opened", { question: details.querySelector("summary").textContent });
});

The toggle event fires after the state changes, whether the user clicked or your code set details.open = true.

How does the name attribute make an accordion?

A list of details elements is already an accordion in which any number of panels can be open. Many designs want an exclusive accordion instead: opening one panel closes the one that was open. That used to need a script listening for toggle and closing the siblings. Now it is an attribute:

<details name="pricing-faq" open>
  <summary>Can I change plans later?</summary>
  <p>Yes. Upgrades apply immediately; downgrades apply at the next billing date.</p>
</details>
<details name="pricing-faq">
  <summary>Is there a free trial?</summary>
  <p>Every paid plan has a 14-day trial, no card required.</p>
</details>
<details name="pricing-faq">
  <summary>Do you offer discounts for non-profits?</summary>
  <p>Yes, 50% off any plan. Contact support with your registration details.</p>
</details>

All details elements with the same name form a group. They do not need to be siblings; the name alone groups them, and you can have several independent groups on one page by using different names. Chrome 120 and Safari 17.2 shipped this in December 2023, and Firefox 130 completed support in September 2024. Older browsers simply ignore the attribute and let several panels open at once, which is a harmless fallback.

Should the accordion be exclusive?

Often not. Exclusive accordions stop users from comparing two answers side by side, and when one panel closes as another opens, the content above the user’s position shrinks and the page jumps. They fit best when panels are long and only one is relevant at a time, such as steps in a checkout or sections of a settings page. For FAQs, letting people open as many as they like is usually kinder. Note too that an exclusive group cannot force one panel to stay open; the user can still close the open one.

Styling

The summary element has display: list-item by default, which is where the triangle marker comes from. Replace it with your own icon:

summary {
  list-style: none;            /* remove the marker in Chrome and Firefox */
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 0;
  font-weight: 600;
}

summary::-webkit-details-marker {
  display: none;               /* Safari still needs this */
}

summary::after {
  content: "";
  width: 0.6rem;
  height: 0.6rem;
  border-right: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: rotate(45deg);
  transition: transform 150ms;
}

details[open] > summary::after {
  transform: rotate(-135deg);
}

summary:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

Alternatively, keep the built-in marker and restyle it with summary::marker, which accepts color, font size and content in Chrome and Firefox. Safari’s marker support is more limited, which is why many teams hide it and draw their own. Pseudo-elements in practice covers ::marker and ::details-content in more detail.

Animating the content

Animating the height of a details element as it opens and closes is still awkward. The hidden content is not rendered at all while closed, so there is no height to transition from, and the close happens instantly. Two pragmatic approaches work today: a short fade-and-slide @keyframes animation on the content when details[open] applies, which covers opening; or a small script that intercepts the summary click and animates the height before toggling open. Newer CSS features for animating to intrinsic sizes are arriving in Chromium, but they are not cross-browser yet. Whatever you choose, respect prefers-reduced-motion.

How accessible are details and summary?

Browsers expose the summary as a button-like control with an expanded or collapsed state, which is exactly what assistive technology expects from a disclosure widget. A few rules keep it that way:

  • Keep interactive elements out of the summary. A link or button inside it creates a control inside a control; clicks and keyboard behavior conflict, and screen readers announce a confusing name.
  • Headings inside the summary are allowed by the specification, and on a long FAQ they let users navigate by heading. Some screen readers do not expose the heading role inside a summary, though, so test with the ones your users rely on.
  • Write summaries that make sense alone. Users browsing a list of collapsed panels hear only the summaries. “More info” eight times is useless.
  • Do not use details for navigation tabs or menus. Tabs have their own keyboard pattern; a disclosure is fine for a mobile “Menu” button that reveals a list of links, but not as a replacement for an ARIA tablist.

Can search engines read hidden details content?

Find in page

A long-standing complaint about collapsed content is that Ctrl+F cannot find text inside it. Chromium-based browsers now search inside closed details elements and open them automatically when a match is found, which is a real advantage over custom accordions that hide content with display: none. Other browsers have not yet adopted this behavior, so do not rely on it for critical information.

If a support article links to /faq#refunds and the answer sits inside a closed panel, the browser scrolls to the closed panel and the user still has to click. Open it on load:

function openTargetDetails() {
  const target = document.getElementById(location.hash.slice(1));
  const details = target?.closest("details");
  if (details) details.open = true;
}

addEventListener("DOMContentLoaded", openTargetDetails);
addEventListener("hashchange", openTargetDetails);

Printing

Closed panels print closed, so a printed FAQ or policy page shows only questions. Open everything before printing:

addEventListener("beforeprint", () => {
  document.querySelectorAll("details:not([open])").forEach((d) => {
    d.dataset.printOpened = "true";
    d.open = true;
  });
});

addEventListener("afterprint", () => {
  document.querySelectorAll("details[data-print-opened]").forEach((d) => {
    d.open = false;
    delete d.dataset.printOpened;
  });
});

SEO

Content inside closed details is in the HTML, so search engines can read and index it. Google has said it treats content hidden in tabs and accordions as normal content for indexing on mobile-first sites. Do not hide the core content of a page by default, though; if most visitors need it, show it.

Frequently Asked Questions

How do I make an accordion where only one item is open?

Give every details element in the group the same name attribute, such as name=”faq”. Opening one automatically closes the others in that group. This works in all major browsers since Firefox 130 in September 2024; older browsers allow several to be open, which still works.

Are details and summary accessible?

Yes. The summary is keyboard operable with Enter and Space and is announced as an expandable control with its expanded or collapsed state. Avoid putting links or buttons inside the summary, and write summary text that makes sense on its own.

How do I remove the default triangle from summary?

Set list-style: none on summary for Chrome and Firefox, and add summary::-webkit-details-marker { display: none; } for Safari. Then draw your own icon with a pseudo-element and rotate it when the details element has the open attribute.

Can search engines index content inside details?

Yes. The content is part of the HTML even when the panel is closed, so crawlers can read it. Keep the page’s essential content visible by default anyway, because users skim what they can see.

Native first, script second

For disclosures and accordions, details and summary already do the parts that custom components most often get wrong: keyboard support, state, focus and semantics. Add name only when exclusivity truly helps, style the marker to match your design, and add the few lines of script for deep links and printing. That is a smaller, sturdier accordion than most libraries ship. For the full reference, see the MDN details element reference and the Chrome guide to exclusive accordions. Related reading on this site: the Popover API and the dialog element.

Originally posted on 17 September 2024 and post last modified on 17 September 2024

Accessibility Accordion Details Element Front-End Development HTML5

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *