Intermediate7 min read

HTML <details> and <summary>

7 min read
792 words
31 sections5 code blocks

Introduction

Have you ever wanted to create collapsible content on your website without using complex CSS or JavaScript? The HTML <details> and <summary> elements provide a simple, built-in solution for creating interactive, expandable content sections. These semantic HTML5 elements are perfect for creating FAQ sections, dropdown menus, and any content that users might want to show or hide.

In this article, you'll learn how to use these powerful elements to enhance user experience and organize content more effectively on your web pages.

What are Details and Summary Elements?

The <details> and <summary> elements work together to create interactive disclosure widgets. Think of them as a built-in accordion or collapsible panel that browsers understand natively.

  • <details>: A container element that holds content that can be shown or hidden
  • <summary>: Provides a clickable heading or label for the details element

When users click the summary, the details content expands or collapses automatically. This behavior is built into HTML5, making it incredibly simple to implement interactive content without additional scripting.

Key Features and Characteristics

Native Browser Support

The details and summary elements are supported by all modern browsers and provide consistent behavior across different platforms.

Accessibility Built-in

These elements come with built-in accessibility features:

  • Screen readers automatically understand the expand/collapse functionality
  • Keyboard navigation works by default
  • Proper ARIA attributes are applied automatically

Semantic Meaning

Unlike generic div elements, details and summary convey meaning about the content structure to both browsers and assistive technologies.

Basic Syntax and Structure

Here's the fundamental structure of details and summary elements:

JavaScript
<details>
  <summary>Click to expand</summary>
  <p>This content is hidden by default and shows when expanded.</p>
</details>

Essential Rules

  1. The <summary> element must be the first child of <details>
  2. Only one <summary> element is allowed per <details> element
  3. If no <summary> is provided, browsers display a default label (usually "Details")

Practical Examples

Basic FAQ Section

JavaScript
<details>
  <summary>What is HTML?</summary>
  <p>HTML (HyperText Markup Language) is the standard markup language for creating web pages. It describes the structure and content of web documents using elements and tags.</p>
</details>

<details>
  <summary>How do I learn HTML?</summary>
  <p>Start with basic tags, practice regularly, build simple projects, and gradually learn more advanced concepts. Online tutorials and courses can be very helpful.</p>
</details>

Content with Multiple Elements

JavaScript
<details>
  <summary>Course Information</summary>
  <h3>HTML Fundamentals</h3>
  <p>Duration: 4 weeks</p>
  <p>Level: Beginner to Intermediate</p>
  <ul>
    <li>Basic HTML structure</li>
    <li>Semantic elements</li>
    <li>Forms and tables</li>
  </ul>
</details>

Pre-opened Details

JavaScript
<details open>
  <summary>Important Notice</summary>
  <p>This section is expanded by default because of the 'open' attribute.</p>
</details>

Use Cases and Applications

When to Use Details and Summary

FAQ Sections: Perfect for organizing frequently asked questions where users can expand only the questions they're interested in.

Product Specifications: Great for e-commerce sites to show detailed product information without cluttering the main page.

Navigation Menus: Useful for creating collapsible menu sections, especially on mobile devices.

Documentation: Ideal for organizing help content or technical documentation with expandable sections.

Settings Panels: Excellent for grouping related configuration options that users can expand as needed.

Advantages and Benefits

Improved User Experience

Users can focus on content that interests them while keeping other sections collapsed, reducing visual clutter and improving page scannability.

No JavaScript Required

Unlike custom accordion implementations, details and summary work without any additional scripting, making them lightweight and reliable.

Mobile-Friendly

These elements work excellently on touch devices, providing intuitive tap-to-expand functionality.

SEO Benefits

Search engines can crawl and index all content within details elements, even when collapsed, ensuring your content remains discoverable.

Performance

Since the functionality is built into the browser, there's no additional CSS or JavaScript to download, improving page load times.

Limitations and Considerations

Limited Styling Options

While you can style these elements with CSS, the default disclosure triangle (arrow) can be challenging to customize consistently across all browsers.

Animation Constraints

The default expand/collapse behavior doesn't include smooth animations. Adding animations requires CSS, which might be beyond basic HTML knowledge.

Browser Variations

While widely supported, some older browsers might display these elements differently or not support them at all.

Nested Details Complexity

Creating deeply nested details structures can become confusing for users and may not be the best design choice.

Best Practices

Writing Effective Summaries

  • Keep summary text concise and descriptive
  • Use action words that indicate what happens when clicked
  • Make the purpose of the hidden content clear
JavaScript
<!-- Good -->
<summary>View shipping options and costs</summary>

<!-- Less clear -->
<summary>More info</summary>

Content Organization

  • Group related information logically
  • Don't hide critical information that users need immediately
  • Consider the user's workflow when deciding what to collapse

Accessibility Considerations

  • Ensure summary text is meaningful on its own
  • Test with keyboard navigation
  • Verify screen reader compatibility

Semantic Usage

Use details and summary for content that genuinely benefits from being collapsible, not just for visual effects.

Conclusion

The HTML details and summary elements provide a simple yet powerful way to create interactive, collapsible content without requiring additional technologies. They're perfect for organizing information, improving user experience, and maintaining semantic meaning in your HTML documents.

These elements are particularly valuable for intermediate HTML developers because they demonstrate how semantic markup can provide both functionality and meaning. As you continue building websites, consider using details and summary elements whenever you need to present optional or supplementary content that users might want to show or hide.

Start experimenting with these elements in your projects, and you'll quickly discover how they can make your websites more organized and user-friendly. Remember to keep your summary text clear and descriptive, and always test the user experience across different devices and browsers.