Expand/Collapse Widget Examples

Examples of both a native HTML widget and an ARIA-controlled custom widget. Each widget is wrapped in an element with class example, and the controlled content uses the class details.

Example 1: Native HTML details/summary

This uses the built-in <details> element. Click the summary to expand.

What is an expand/collapse widget?

An expand/collapse widget allows users to reveal or hide additional information on demand. It keeps pages concise while still offering details when needed.

Tip: The native element manages focus, keyboard, and accessibility out of the box.

Example 2: Button-controlled details (ARIA)

This version uses a <button> with aria-expanded and aria-controls to toggle the content container.

Example 3: Multiple collapsible sections

Each button controls its own details container.

This section starts expanded. Use the button to collapse or expand it again.

You can add any markup here, including lists, images, and code samples.

Example 4: Nested details

You can nest collapsibles. Both containers use the class details.

Top-level topic

This panel contains another collapsible section:

Nested topic

Nested content inside a second <details> element.

  • Useful for progressive disclosure.
  • Avoid nesting too deeply for usability.