🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

HTML Details and Summary: Disclosure Widgets

Web FoundationsInteractionđŸŸĸ Free Lesson

Details and Summary: Disclosure Widgets

Hiding content behind a toggle is one of the oldest patterns on the web, and for most of that history it meant a button, a container, an aria-expanded attribute, and a click handler that had to be attached to every instance on the page. The details element collapses all of that into markup. The panel is hidden until it is opened, the summary is always visible and always clickable, and the state is reflected in the DOM through the open property rather than in a JavaScript variable you have to keep in sync.

The reason this matters beyond convenience is state persistence and resilience. The open state lives on the element, so it survives a partial re-render, it can be set in the HTML for content that should start expanded, and it round-trips through form-free navigation without any serialization code. If your script fails to load, an accordion built from details still works, because there is nothing to fail.

What you get from the element:

  • a summary that is focusable and activatable with Enter and Space without any key handling
  • a disclosure triangle rendered by the browser, which you may restyle or remove
  • the open state exposed to assistive technology as expanded and collapsed
  • exclusive groups through the name attribute, with no state management of your own
  • semantics that announce the control correctly even when CSS fails to load

Anatomy of the Disclosure

The summary element is the details element's label, and it must be its first child to be recognised. Anything before it is ignored for that purpose. The remaining children form the revealed region, and they are removed from the accessibility tree and the tab order while the panel is closed, which is exactly what makes this pattern safe: a hidden link inside a collapsed panel cannot receive focus, so keyboard users never land on invisible content.

<article>
  <details>
    <summary>How is shipping calculated?</summary>
    <div class="panel">
      <p>Shipping is flat within the continental region and calculated by
         weight elsewhere. Orders above 99.00 ship free.</p>
      <p><a href="/shipping">Read the full policy</a></p>
    </div>
  </details>

  <details open>
    <summary>Returns within 30 days</summary>
    <div class="panel">
      <p>Unopened items can be returned in their original packaging for a
         full refund within 30 days of delivery.</p>
    </div>
  </details>
</article>

Two subtleties are worth internalising. First, the summary itself stays in the tab order even when the panel is closed, because it is the control. Second, the browser draws a marker before the summary text; that marker is a list-item style disc that you can replace with summary::marker or remove entirely and draw your own with ::before.

Single-Open Accordions with the name Attribute

Giving several details elements the same name value puts them in an exclusive group: opening one closes the others, exactly like radio buttons behave for inputs. This arrived in current browsers recently and it removes the last reason to reach for an accordion library in a plain document.

<div class="faq">
  <details name="faq" open>
    <summary>What formats do you support?</summary>
    <p>CSV, JSON Lines and Parquet for exports; CSV and XLSX for imports.</p>
  </details>

  <details name="faq">
    <summary>How long is data retained?</summary>
    <p>Raw events for 90 days, aggregates for 13 months, audit logs for 7 years.</p>
  </details>

  <details name="faq">
    <summary>Can I export everything at once?</summary>
    <p>Yes. A full export is available from the settings page and arrives by email.</p>
  </details>
</div>

<script>
const items = document.querySelectorAll('.faq details');

items.forEach((item) => {
  item.addEventListener('toggle', () => {
    if (item.open) {
      const heading = item.querySelector('summary').textContent.trim();
      console.log(`Opened section: ${heading}`);
    }
  });
});
</script>

The toggle event bubbles from each element independently, so a single delegated listener on the container can drive analytics, close other widgets, or scroll the newly opened section into view. If your target browsers do not support the name attribute yet, the same behaviour costs about ten lines: on toggle, close every sibling whose open state is true.

Animating Without Blocking the Toggle

The one thing details does not give you is a transition on height. The content region snaps between displayed and hidden, and the classic workaround, animating from zero height to a fixed pixel value, breaks as soon as the content wraps differently. The modern approach is to treat the panel as a grid with a single track and transition the track from zero fraction to one fraction, which needs no measured height at all.

.faq details {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  margin-bottom: 8px;
  overflow: hidden;
  background: #fff;
}

.faq summary {
  padding: 14px 16px;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.faq summary::-webkit-details-marker { display: none; }

.faq summary::after {
  content: "+";
  font-size: 1.25rem;
  color: #64748b;
  transition: transform 160ms ease;
}

.faq details[open] summary::after { transform: rotate(45deg); }

.faq .panel {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 200ms ease;
}

.faq details[open] .panel { grid-template-rows: 1fr; }

.faq .panel > * { overflow: hidden; }

The closing direction is the awkward half, because when open is removed the content disappears immediately and the transition never runs. Handle it by keeping the element open during the animation and removing the attribute once the transition ends, or accept an instant collapse, which most users never notice.

âš ī¸Do not animate content that stays focusable

If a panel animates closed while a link inside it still has focus, the focus jumps to the document body mid-transition. Blur the active element when the toggle starts so the keyboard user is not silently relocated.

Native Disclosure Versus a Custom Component

Choosing between the element and a framework accordion is usually a question of how much the panel has to do, not how it looks. The styling above reaches visual parity with any library; the difference shows up in behaviour.

Concerndetails elementCustom component
Keyboard supportBuilt in, including Escape-free activationMust be written and maintained
Exclusive groupsname attributeManual state coordination
Accessibility namesFirst heading inside the panelMust be wired with aria attributes
Content visibilityRemoved from tab order when closedOnly hidden if you hide it correctly
Height animationNeeds the grid trickFree with a measured value
Deep linking to an open itemRequires the open attribute in markupUsually already handled by your router
Works without JavaScriptYesNo

The honest summary is that details wins whenever the panel is pure content, and a custom component earns its cost only when the panel contains data that must be fetched, or when several widgets must coordinate in ways the name attribute cannot express.

Keyboard and Screen Reader Behaviour

A disclosure widget is announced in three parts: the summary's text, the word collapsed or expanded, and the number of items when the group is nested. Users discover this without documentation, which is the mark of a standard control. Contrast that with a div-based toggle, where the same announcement depends on three ARIA attributes being present and consistent at all times.

The failures you will actually encounter are structural rather than semantic. Putting block content directly before the summary means it renders outside the toggled region. Nesting a details inside another details gives you a two-level tree that some screen readers flatten unpredictably. Clicking the summary's inner padding may appear unresponsive if the clickable area is limited to the text node rather than the whole summary box, so keep the summary a block-level flex container with padding rather than an inline span.

Deep Linking and Restoring Open State

A collapsed section cannot be linked to, and that becomes a real problem the moment you ship an FAQ and someone pastes a link to an answer that the recipient never sees. The fix is to make the open attribute part of the URL. Set it from the hash on load, and reflect it back whenever a panel toggles, so the address bar always describes what is on screen.

javascript
function syncFromHash() {
  const target = document.querySelector(location.hash || 'body');
  const section = target && target.closest('details');
  if (section) {
    section.open = true;
    section.scrollIntoView({ behavior: 'smooth', block: 'center' });
  }
}

document.addEventListener('toggle', (event) => {
  const el = event.target;
  if (el instanceof HTMLDetailsElement && el.open) {
    const id = el.querySelector('summary').textContent
      .trim()
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, '-')
      .replace(/(^-|-$)/g, '');
    history.replaceState(null, '', id ? `#${id}` : location.pathname);
  }
}, true);

window.addEventListener('hashchange', syncFromHash);
syncFromHash();

The same pattern handles back and forward navigation, because replaceState keeps history clean while the hash still identifies the open item. It also gives search engines an anchor to index: a page whose answers are all collapsed still exposes the summary text to the crawler, and the fragment lets a result jump straight to the expanded answer. If your content is long enough that every answer should be reachable independently, render each one as its own page instead of leaning harder on fragments.

Common Mistakes

Wrapping the summary in a button so it looks clickable

Fix: Style the summary itself; a button inside a summary makes the native toggle a nested interactive control and double-fires activation.

Animating height with a hard-coded pixel value

Fix: Transition grid-template-rows from 0fr to 1fr so wrapped or dynamically loaded content still animates correctly.

Leaving the name attribute off an FAQ list and managing open state by hand

Fix: Give every item the same name value and let the browser enforce a single-open group.

Hiding an open panel with CSS while leaving the open attribute set

Fix: Keep the DOM state and the visual state in agreement; assistive technology reports the attribute, not your stylesheet.

Key Takeaways

  • The details element owns hidden state, focus behaviour and announcements, so a no-JavaScript accordion is genuinely functional.
  • summary must be the first child to act as the label, and it is the only part that stays in the tab order while collapsed.
  • Matching name values create exclusive groups without a line of script.
  • Use the grid-row transition trick for smooth opening, and decide deliberately whether closing animates.
  • Reach for a custom component only when the panel must fetch data or coordinate with other widgets.
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-12

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement