Expand/Collapse Widget Examples

Using the Native <details> Element

Learn more about native expand/collapse

The HTML <details> element provides an accessible, keyboard-friendly expand/collapse interface out of the box. The <summary> element acts as the toggle, while any additional content inside is hidden until expanded.

Custom Expand/Collapse with JavaScript

This widget uses a button and a content region. The button updates its aria-expanded attribute, and the content region toggles using the hidden attribute. CSS transitions create a smooth animation.

Key accessibility considerations include:

  1. Linking the button and region with aria-controls.
  2. Updating aria-expanded to reflect visibility.
  3. Maintaining focus styles for keyboard users.