Exploring What The Details And Summary Elements Can Do

Build native expand/collapse widgets without JavaScript. Learn the details and summary elements for accessible, semantic disclosure interfaces.

Every web developer has faced the need to hide content that users can reveal on demand. For years, this required JavaScript event handlers, CSS display toggles, and careful attention to accessibility. The HTML specification introduced native disclosure widgets that change everything. The <details> and <summary> elements provide built-in expand/collapse functionality with semantic meaning, keyboard navigation, and accessibility support--all without writing a single line of JavaScript.

These elements represent a fundamental shift in how we approach interactive content on the web. Instead of hacking together show/hide behavior with div elements and script tags, we now have purpose-built HTML that communicates intent to browsers, search engines, and assistive technologies. Understanding what these elements can do--and where their limitations lie--enables you to build more accessible, maintainable interfaces while reducing the JavaScript your applications depend on. Pairing this approach with responsive HTML templates creates clean, maintainable web interfaces that scale across devices.

Basic Details and Summary Usage
1<details>2 <summary>System Requirements</summary>3 <p>Requires a computer running an operating system. The computer must have some4 memory and ideally some kind of long-term storage. An input device as well5 as some form of output device is recommended.</p>6</details>

What Are Details and Summary Elements

The <details> element creates a disclosure widget, which is a piece of user interface that has a brief summary or heading and a control to expand the UI to show more details. This widget exists in one of two states: closed or open. When closed, only the summary is visible. When open, the full contents are revealed. The <summary> element serves as the caption or legend for the disclosure widget, providing the always-visible label that users click or tap to toggle the expanded state.

The relationship between these elements is hierarchical and semantically meaningful. The <summary> must be the first child of <details>, establishing a clear document structure that browsers and assistive technologies can interpret. The contents of <summary> become the accessible name for the disclosure widget, communicating to screen reader users what information they will find when they expand the content.

This simple markup creates a functional disclosure widget. No JavaScript required. The browser handles all the interaction mechanics, including keyboard navigation. Users can expand and collapse the widget by clicking the summary, pressing Enter or Space when the summary has focus, or using touch gestures on mobile devices. The semantic structure means search engines and other automated systems understand the relationship between the summary and the detailed content.

The elegance of this approach lies in its simplicity. Rather than managing hidden states with JavaScript, adding ARIA attributes to compensate for missing semantics, and ensuring keyboard accessibility through custom event handlers, the <details> and <summary> elements provide all of this built-in. This reduction in boilerplate code translates to fewer opportunities for bugs, better performance through smaller JavaScript bundles, and improved accessibility through standardized browser behavior. For more on building accessible web interfaces, see our guide on accessible font sizing.

The Open Attribute

The open attribute controls the initial state of the disclosure widget. As a boolean attribute, its presence indicates that the widget should be expanded when the page loads, regardless of any value assigned to it. If the attribute is absent, the widget starts in its closed state. This attribute is not merely a static setting--it is added and removed dynamically as users interact with the widget, which means you can use it as the basis for CSS styling that responds to the current state.

The boolean nature of the open attribute has important implications for how you work with it programmatically. Setting open="false" does not close the widget because the attribute is present; only removing the attribute entirely achieves the closed state. When toggling the attribute with JavaScript, you add or remove the entire attribute rather than changing its value.

<!-- Widget starts closed -->
<details>
 <summary>Hidden Content</summary>
 <p>This content is hidden until the user expands the widget.</p>
</details>

<!-- Widget starts open -->
<details open>
 <summary>Visible Content</summary>
 <p>This content is visible when the page loads.</p>
</details>

Using attribute selectors in CSS, you can style the widget differently based on its open or closed state. This enables visual feedback that reinforces the current state and creates a more polished user experience. The ability to style based on state without JavaScript class toggling represents a significant simplification in how we approach conditional styling for disclosure widgets.

Building Accordions with the Name Attribute

Modern browsers support the name attribute on <details> elements, which enables sophisticated accordion behavior without any JavaScript. When multiple <details> elements share the same name value, only one can be open at a time. Opening one automatically closes any others with the same name. This behavior mirrors the accordion pattern that developers have implemented with JavaScript for years, but now it comes built into the browser.

The name attribute creates a grouping mechanism that is independent of the elements' position in the document. Details elements do not need to be adjacent to one another to be part of the same accordion group--they simply need to share the same name value. This flexibility enables interesting architectural patterns where related disclosure widgets can be scattered across a page while still maintaining accordion semantics.

<details name="faq-section">
 <summary>How do I reset my password?</summary>
 <p>Click the "Forgot Password" link on the login page.</p>
</details>

<details name="faq-section">
 <summary>What payment methods do you accept?</summary>
 <p>We accept all major credit cards and PayPal.</p>
</details>

In this example, clicking any question automatically closes any other open question in the same group. This behavior provides an excellent user experience for FAQ pages, where readers typically want to focus on one answer at a time. The accordion pattern reduces visual clutter while keeping all content accessible, and the native implementation means this behavior works for keyboard users and screen reader users without additional effort.

The grouping behavior respects source order when multiple grouped elements have the open attribute. If several accordion sections are marked open in the HTML, only the first one in source order will be rendered open when the page loads. This deterministic behavior prevents ambiguity and ensures consistent rendering across browsers.

Why Use Native Disclosure Widgets

No JavaScript Required

Built-in expand/collapse functionality works without any scripting, reducing bundle size and potential failure points.

Semantic Meaning

The elements communicate intent to browsers, search engines, and assistive technologies through native HTML semantics.

Keyboard Accessible

Users can navigate and activate disclosure widgets using only keyboard input, with no additional ARIA attributes needed.

Accordion Behavior

The name attribute enables exclusive expand/collapse behavior without custom JavaScript implementation.

Styling Possibilities

The <details> and <summary> elements accept standard CSS styling, but they also offer unique styling opportunities through the ::marker pseudo-element and state-based attribute selectors. The default disclosure triangle is provided by the browser's user agent stylesheet and appears at the inline-start side of the summary content. This marker can be customized using the same CSS properties that control list item markers.

Styling the marker opens up significant creative possibilities. You can change the marker to different bullet types, use custom images, or hide the marker entirely to create a cleaner visual design. The list-style-type, list-style-image, and list-style-position properties all apply to the summary's marker. Similar to how HTML forms in emails require careful markup for compatibility, disclosure widgets need thoughtful implementation to ensure consistent cross-browser behavior.

Custom Markers

/* Hide the default marker */
summary {
 list-style: none;
}

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

/* Add a custom marker using pseudo-element */
summary::after {
 content: '+';
 margin-left: 0.5em;
 font-weight: bold;
}

details[open] summary::after {
 content: '-';
}

This approach replaces the default triangle with a custom plus/minus indicator. The details[open] selector targets only open widgets, allowing different styling for the expanded and collapsed states. You can extend this pattern with background colors, borders, padding changes, and other visual treatments that reinforce the interaction state.

Beyond markers, the open attribute enables state-based styling throughout the widget. You might change the background color when expanded, animate the summary's text, or reveal additional visual elements that are hidden in the closed state. These techniques create polished, professional interfaces without requiring JavaScript to manage state transitions.

Animation Approaches

Animating the open and close transitions presents unique challenges because the details element lacks built-in animation support. The content appears and disappears instantly, which can feel abrupt to users. However, several techniques can smooth this transition.

The most common approach involves animating the max-height property. By setting an explicit max-height on the content and transitioning that property, you create the illusion of smooth expansion. This technique requires careful tuning of the max-height value to accommodate the content without limiting it artificially.

details summary ~ * {
 overflow: hidden;
 max-height: 0;
 opacity: 0;
 transition: max-height 0.3s ease, opacity 0.3s ease;
}

details[open] summary ~ * {
 max-height: 500px;
 opacity: 1;
}

This CSS creates a fade-and-slide animation that triggers when the open attribute is added or removed. The max-height value must be large enough to accommodate the tallest possible content while remaining small enough to prevent the animation from feeling sluggish. For more dynamic content, JavaScript can calculate the actual content height and apply it as an inline style, creating perfect animations regardless of content length.

Another approach uses CSS grid to animate the content's appearance without pixel-based transitions. By animating the grid-template-rows property, you can create smooth height transitions that automatically adapt to content. This technique is newer but supported in modern browsers and often produces more natural-feeling animations than max-height transitions.

Accessibility Considerations

The <details> and <summary> elements have semantic meaning that benefits accessibility. The <summary> element functions as a button, receiving keyboard focus and responding to activation events. Screen readers announce the summary with language that indicates it is expandable, such as "button, collapsed" or "disclosure triangle, collapsed" depending on the browser and screen reader combination.

However, accessibility behavior varies across browser and screen reader combinations. Firefox exposes the default disclosure triangle as part of the accessible name, which can result in unusual announcements. Removing the default marker can cause accessibility issues because the triangle's rotation is the only visual indication of state that some screen reader and browser pairings announce consistently.

The name attribute for accordion behavior introduces additional accessibility considerations. When opening one section closes others, screen readers should announce this change, but support for this behavior varies. Testing with actual assistive technology combinations remains essential to ensure your implementation works for all users.

Content Discoverability

A subtle but important behavior affects how content inside closed <details> elements is discovered. In Chromium browsers, using the browser's find-in-page feature will search through closed disclosure content and automatically expand any widget containing the search term. This behavior helps users discover hidden content but may surprise users who expect closed sections to be invisible to search.

Firefox and Safari do not replicate this find-in-page behavior, meaning content in closed details is not discoverable through browser search in these browsers. If content discoverability is critical for your use case, consider whether <details> is the appropriate element or whether alternative approaches better serve your users. Our web development services team can help you implement the right patterns for your specific accessibility requirements.

Frequently Asked Questions

Practical Use Cases

The most common application of <details> and <summary> is the FAQ page pattern. This use case leverages the natural question-and-answer structure that maps directly to the summary-content relationship. FAQ pages benefit from the accordion behavior because readers typically want to focus on one answer at a time, and the clean, minimal interface keeps the page scannable.

Beyond FAQs, <details> elements work well for:

  • Supplementary content: Glossary terms, footnotes, and detailed explanations that not all users need
  • Code examples: Hide implementation details while keeping explanations visible
  • Legal documents: Terms of service and privacy policies that users must access but may not read in full
  • Technical documentation: Detailed specs that different readers need at different depths

Code Examples in Documentation

<details>
 <summary>View Code Example</summary>
 <pre><code>function calculateTotal(items) {
 return items.reduce((sum, item) => sum + item.price, 0);
}</code></pre>
</details>

Technical documentation frequently uses disclosure widgets to show and hide code examples. By default, the code is hidden, keeping the documentation readable. Readers who want to see the implementation can expand the section. This pattern is valuable for our technical documentation services where we help clients create clear, accessible documentation for their products and platforms.

Expandable sections also improve long-form content by breaking it into manageable chunks. Rather than presenting readers with a wall of text, you can organize content into expandable sections that readers can explore based on their interests. This approach works for legal documents, terms of service, and detailed specifications where users need access to all information but may not want to read everything at once. Combined with semantic HTML forms, these patterns create accessible, well-structured web content.

Integration with JavaScript

While <details> and <summary> work without JavaScript, integrating them with scripts enables advanced functionality. The toggle event fires whenever the widget's state changes, providing a hook for custom behavior.

document.querySelectorAll('details').forEach(details => {
 details.addEventListener('toggle', event => {
 if (details.open) {
 console.log('Widget opened');
 // Analytics tracking, content loading, etc.
 }
 });
});

JavaScript can also programmatically control the widget's state by adding or removing the open attribute. This enables scenarios where external events should trigger expansion or collapse, such as synchronizing a disclosure widget with other interface changes or responding to data updates that make certain content more or less relevant.

For applications that require more complex behavior than the native elements provide, you can use JavaScript to enhance rather than replace the native functionality. The <details> element serves as a semantic foundation that ensures basic accessibility and keyboard navigation, while JavaScript adds features like animation, state persistence, or synchronization with other interface components. Our AI & Automation services can help you build intelligent interfaces that leverage these patterns effectively.

Browser Support and Compatibility

The <details> and <summary> elements have been part of the HTML specification since HTML5, but full support across all browsers arrived more recently. The feature is now considered "Baseline Widely Available," meaning it works across the latest devices and browser versions. This status indicates that developers can rely on the elements working for the vast majority of users.

Internet Explorer does not support these elements, and older versions of Edge (pre-Chromium) had limited support. If you need to support these browsers, you can use polyfills that add the necessary behavior with JavaScript. However, given the age of these browsers and their declining usage, many projects can safely use <details> and <summary> without fallback solutions.

The name attribute for accordion behavior has slightly newer support than the basic functionality. All modern browsers support it, but checking current compatibility is worthwhile if you need to support specific browser versions. The progressive enhancement approach works here: browsers that support the name attribute get accordion behavior, while others get independent disclosure widgets.

Build Better Web Interfaces with Semantic HTML

Our AI & Automation services can help you implement modern web patterns that reduce JavaScript dependency while improving accessibility and performance.

Sources

  1. MDN Web Docs - details element - Official documentation for the <details> element
  2. MDN Web Docs - summary element - Official documentation for the <summary> element
  3. web.dev - Details and summary - Google developer documentation with interactive examples
  4. Scott O'Hara - The details and summary elements, again - Leading accessibility expert's comprehensive analysis