Accordion pattern and accessibility: a comprehensive note

The accordion pattern is a common user interface (UI) design element used to organize and present content in a compact and collapsible format. It allows users to interact with headers to reveal or hide sections of related information, making it particularly useful in scenarios where space is limited, such as FAQ pages, dashboards, or mobile layouts.

While the accordion pattern improves visual aesthetics and usability for many users, without proper implementation, it can present significant barriers for individuals with disabilities, including those using screen readers, keyboard navigation, or other assistive technologies.

  1. Keyboard Navigation:
    • Users who cannot use a mouse rely on keyboards to navigate and interact with web elements.
    • If accordion sections are not focusable or operable via the keyboard, they become inaccessible.
    • Tab navigation must allow users to focus on each accordion header, and the Enter or Space keys should toggle the visibility of the corresponding content.
  2. Screen Reader Compatibility:
    • Screen reader users need to understand the context of the accordion and its dynamic behavior (expanded or collapsed state).
    • Without proper labeling, assistive technologies cannot communicate whether a section is expanded or collapsed.
    • Each section must have accessible roles and attributes to describe its current state and relationship with the associated content.
  3. Dynamic Content Updates:
    • When users expand or collapse accordion sections, the content should update dynamically and announce changes for screen reader users.
    • Missing dynamic announcements can leave screen reader users unaware of changes, causing confusion.
  4. Semantic Structure:
    • Accordions should use semantic HTML, such as <button> for toggling headers and <section> or <div> to group content.
    • Non-semantic elements like <div> without proper roles or attributes fail to provide meaningful navigation for assistive technologies.
  5. Focus Management:
    • Users must be able to move focus logically between accordion headers and content.
    • If an accordion header is expanded, focus should not be trapped or lost, and it should not disrupt the user’s navigation flow.
  6. Visual Indicators:
    • The accordion must visually indicate the expanded or collapsed state of sections (e.g., arrows or plus/minus icons) for users with cognitive disabilities or low vision.
    • Visual indicators should also have sufficient contrast to ensure visibility.
  7. Error Prevention:
    • Accidental actions, such as collapsing all sections or expanding multiple sections at once, should be avoided or easily reversible.
    • Provide clear labels and predictable behaviors to minimize errors.

ARIA roles and attributes enhance the accessibility of interactive components like accordions, especially when semantic HTML alone cannot achieve the desired functionality.

Key ARIA attributes for accordions:

  • aria-expanded: Indicates whether a section is expanded (true) or collapsed (false). This attribute is placed on the accordion header element (e.g., <button>).
  • aria-controls: Establishes a relationship between the accordion header and its associated content. The value of aria-controls should match the id of the corresponding content container.
  • aria-hidden: Used on the content container to indicate whether it is visible (false) or hidden (true) to assistive technologies.
  • role="region": Adds semantic meaning to the accordion content, making it easier for screen reader users to navigate.
  1. Inclusive Design: Ensures that all users, regardless of ability, can interact with and understand accordion content.
  2. Improved Usability: Enhances the experience for keyboard-only users, screen reader users, and users with cognitive or motor impairments.
  3. Compliance with WCAG Standards: Following accessibility best practices helps meet Web Content Accessibility Guidelines (WCAG) 2.1 Level AA requirements.
  4. Enhanced User Experience: A well-designed accordion improves the overall flow and organization of content, benefiting all users.

Accessible accordions can be implemented in a variety of contexts, including:

  • FAQ Sections: Allowing users to expand answers to specific questions.
  • Mobile Layouts: Presenting collapsible navigation menus or compact content areas.
  • Dashboards: Displaying user settings or grouped content in an organized way.
  • Forms: Grouping sections of a form to reduce cognitive overload and improve navigation.
  1. Missing ARIA Attributes: Failing to include attributes like aria-expanded or aria-controls leaves screen reader users unaware of the accordion’s behavior.
  2. Non-Keyboard-Operable Elements: Using non-interactive elements like <div> for headers prevents keyboard navigation.
  3. Focus Trapping or Loss: Improper focus management disrupts the user’s ability to navigate logically between accordion sections.
  4. Unclear Visual Indicators: Lack of sufficient visual contrast or icons makes it difficult for users to identify expanded or collapsed sections.
  5. Non-Semantic Markup: Using <div> elements without roles or labels results in poor accessibility for assistive technologies.
  6. Dynamic Updates Not Announced: Changes in content visibility are not communicated to screen reader users, causing confusion.
  1. Use semantic HTML for headers (<button> or <h2> elements) and content sections (<div> or <section>).
  2. Implement ARIA attributes (aria-expanded, aria-controls, aria-hidden) to convey state and relationships.
  3. Ensure keyboard accessibility, allowing users to navigate and toggle sections using Tab, Enter, and Space.
  4. Include visual indicators with sufficient contrast to communicate expanded/collapsed states.
  5. Provide dynamic feedback for screen reader users, announcing changes in state or content visibility.
  6. Test the accordion with assistive technologies to identify and resolve any usability barriers.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Accessible Accordion</title>
  <style>
    .accordion {
      font-family: Arial, sans-serif;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    .accordion button {
      width: 100%;
      text-align: left;
      padding: 15px;
      font-size: 16px;
      border: none;
      background: #f7f7f7;
      cursor: pointer;
    }
    .accordion button:focus {
      outline: 3px solid #007BFF;
    }
    .accordion button[aria-expanded="true"] {
      background: #e7e7e7;
      font-weight: bold;
    }
    .accordion .content {
      display: none;
      padding: 15px;
      border-top: 1px solid #ccc;
      background: #f9f9f9;
    }
    .accordion .content[aria-hidden="false"] {
      display: block;
    }
  </style>
</head>
<body>
  <div class="accordion">
    <h2>
      <button id="accordion1" aria-expanded="false" aria-controls="section1">Section 1</button>
    </h2>
    <div id="section1" class="content" role="region" aria-hidden="true">
      <p>This is the content of Section 1.</p>
    </div>
    <h2>
      <button id="accordion2" aria-expanded="false" aria-controls="section2">Section 2</button>
    </h2>
    <div id="section2" class="content" role="region" aria-hidden="true">
      <p>This is the content of Section 2.</p>
    </div>
  </div>
  <script>
    const buttons = document.querySelectorAll('.accordion button');
    buttons.forEach(button => {
      button.addEventListener('click', () => {
        const expanded = button.getAttribute('aria-expanded') === 'true';
        button.setAttribute('aria-expanded', !expanded);
        const content = document.getElementById(button.getAttribute('aria-controls'));
        content.setAttribute('aria-hidden', expanded);
      });
    });
  </script>
</body>
</html>
  1. Semantic Markup:
    • Uses <button> elements for headers, which are focusable and interactive by default.
    • Adds <h2> for headings and <div> with role="region" for content to provide clear structure.
  2. ARIA Attributes:
    • aria-expanded dynamically updates the state of the accordion (expanded/collapsed).
    • aria-controls links each button to its corresponding content.
    • aria-hidden indicates whether the content is visible to assistive technologies.
  3. Keyboard Accessibility:
    • Buttons can be focused using Tab and toggled using Enter or Space.
  4. Visual Feedback:
    • Clear styling changes (e.g., background color, bold text) indicate expanded sections.
&lt;!DOCTYPE html>
&lt;html lang="en">
&lt;head>
  &lt;meta charset="UTF-8">
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
  &lt;title>Inaccessible Accordion&lt;/title>
  &lt;style>
    .accordion {
      font-family: Arial, sans-serif;
      border: 1px solid #ccc;
      border-radius: 5px;
    }
    .accordion div {
      padding: 15px;
      cursor: pointer;
      background: #f7f7f7;
    }
    .accordion div:hover {
      background: #e7e7e7;
    }
    .accordion .content {
      display: none;
      padding: 15px;
      border-top: 1px solid #ccc;
      background: #f9f9f9;
    }
  &lt;/style>
&lt;/head>
&lt;body>
  &lt;div class="accordion">
    &lt;div>Section 1&lt;/div>
    &lt;div class="content">
      &lt;p>This is the content of Section 1.&lt;/p>
    &lt;/div>
    &lt;div>Section 2&lt;/div>
    &lt;div class="content">
      &lt;p>This is the content of Section 2.&lt;/p>
    &lt;/div>
  &lt;/div>
  &lt;script>
    const headers = document.querySelectorAll('.accordion div:nth-child(odd)');
    headers.forEach(header => {
      header.addEventListener('click', () => {
        const content = header.nextElementSibling;
        content.style.display = content.style.display === 'block' ? 'none' : 'block';
      });
    });
  &lt;/script>
&lt;/body>
&lt;/html>
  1. Non-Semantic Markup:
    • Uses <div> for headers instead of <button>, making the headers non-focusable and inaccessible to keyboard users.
  2. No ARIA Attributes:
    • Missing aria-expanded, aria-controls, and aria-hidden attributes, leaving screen reader users unaware of the accordion’s state.
  3. Keyboard Inaccessibility:
    • Users cannot navigate or toggle sections using the keyboard.
  4. No Dynamic Feedback:
    • State changes (expanded/collapsed) are not announced to assistive technologies.
  5. Poor Visual Feedback:
    • Visual styling does not adequately indicate which section is expanded or collapsed.
  1. Use semantic HTML (e.g., <button>, <h2>) for headers and content.
  2. Implement ARIA attributes (aria-expanded, aria-controls, aria-hidden) to convey state and relationships.
  3. Ensure keyboard operability so users can interact with the accordion using Tab, Enter, and Space.
  4. Provide dynamic announcements for screen readers to communicate state changes.
  5. Include clear visual indicators to represent expanded and collapsed states.

What are your feelings

Updated on June 15, 2025