Accordion

An accordion allows users to expand and collapse a section of content as needed. It consists of a trigger element and a content panel that is shown or hidden when the trigger is activated.

Accordions can be used to save space and reduce clutter when displaying content immediately is not necessary. They are less suitable for presenting essential information or content that users need to compare side by side. Accordions are often stacked on top of each other to display a set of related topics, such as FAQs.

Accessibility Features

Feature types: Required Recommended
  • Space and Enter expand and collapse the accordion when the trigger element is focused.
  • The expanded/collapsed state must be communicated programmatically to screen readers.

Details Element

The native <details> and <summary> elements handle expand/collapse behavior and communicate state to screen readers automatically. Using them is recommended when possible.

Example

Code Snippet

Custom Accordion

A custom accordion may be necessary, for example, when the content panel does not directly follow the trigger button in the DOM. It requires explicit attributes to communicate semantics:

  • aria-expanded on the trigger button communicates the expanded or collapsed state to screen readers.
  • Collapsed panel content must be hidden from screen readers and the keyboard, for example with the hidden attribute or display: none. If the content remains partly visible, such as a faded preview, inert can be used instead.
  • If the trigger button follows the content panel in DOM order, focus must move to the panel manually when the button is pressed.
  • aria-controls points to the element the trigger controls.

Example

Code Snippet