Understanding the Accordion Pattern #
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.
The Accessibility Challenges of Accordions #
- 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
EnterorSpacekeys should toggle the visibility of the corresponding content.
- 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.
- 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.
- 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.
- Accordions should use semantic HTML, such as
- 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.
- 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.
- 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.
The Role of ARIA (Accessible Rich Internet Applications) #
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 ofaria-controlsshould match theidof 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.
Benefits of Accessible Accordions #
- Inclusive Design: Ensures that all users, regardless of ability, can interact with and understand accordion content.
- Improved Usability: Enhances the experience for keyboard-only users, screen reader users, and users with cognitive or motor impairments.
- Compliance with WCAG Standards: Following accessibility best practices helps meet Web Content Accessibility Guidelines (WCAG) 2.1 Level AA requirements.
- Enhanced User Experience: A well-designed accordion improves the overall flow and organization of content, benefiting all users.
Few use cases for accordions #
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.
Common Accessibility Mistakes in Accordions #
- Missing ARIA Attributes: Failing to include attributes like
aria-expandedoraria-controlsleaves screen reader users unaware of the accordion’s behavior. - Non-Keyboard-Operable Elements: Using non-interactive elements like
<div>for headers prevents keyboard navigation. - Focus Trapping or Loss: Improper focus management disrupts the user’s ability to navigate logically between accordion sections.
- Unclear Visual Indicators: Lack of sufficient visual contrast or icons makes it difficult for users to identify expanded or collapsed sections.
- Non-Semantic Markup: Using
<div>elements without roles or labels results in poor accessibility for assistive technologies. - Dynamic Updates Not Announced: Changes in content visibility are not communicated to screen reader users, causing confusion.
Key Best Practices for Accessible Accordions #
- Use semantic HTML for headers (
<button>or<h2>elements) and content sections (<div>or<section>). - Implement ARIA attributes (
aria-expanded,aria-controls,aria-hidden) to convey state and relationships. - Ensure keyboard accessibility, allowing users to navigate and toggle sections using
Tab,Enter, andSpace. - Include visual indicators with sufficient contrast to communicate expanded/collapsed states.
- Provide dynamic feedback for screen reader users, announcing changes in state or content visibility.
- Test the accordion with assistive technologies to identify and resolve any usability barriers.
Accessible Accordion Examples #
Example – Good Practice #
<!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>
Key Features of the Good Practice Example #
- Semantic Markup:
- Uses
<button>elements for headers, which are focusable and interactive by default. - Adds
<h2>for headings and<div>withrole="region"for content to provide clear structure.
- Uses
- ARIA Attributes:
aria-expandeddynamically updates the state of the accordion (expanded/collapsed).aria-controlslinks each button to its corresponding content.aria-hiddenindicates whether the content is visible to assistive technologies.
- Keyboard Accessibility:
- Buttons can be focused using
Taband toggled usingEnterorSpace.
- Buttons can be focused using
- Visual Feedback:
- Clear styling changes (e.g., background color, bold text) indicate expanded sections.
Example – Bad Practice #
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inaccessible Accordion</title>
<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;
}
</style>
</head>
<body>
<div class="accordion">
<div>Section 1</div>
<div class="content">
<p>This is the content of Section 1.</p>
</div>
<div>Section 2</div>
<div class="content">
<p>This is the content of Section 2.</p>
</div>
</div>
<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';
});
});
</script>
</body>
</html>
Issues in the Bad Practice Example #
- Non-Semantic Markup:
- Uses
<div>for headers instead of<button>, making the headers non-focusable and inaccessible to keyboard users.
- Uses
- No ARIA Attributes:
- Missing
aria-expanded,aria-controls, andaria-hiddenattributes, leaving screen reader users unaware of the accordion’s state.
- Missing
- Keyboard Inaccessibility:
- Users cannot navigate or toggle sections using the keyboard.
- No Dynamic Feedback:
- State changes (expanded/collapsed) are not announced to assistive technologies.
- Poor Visual Feedback:
- Visual styling does not adequately indicate which section is expanded or collapsed.
Key Takeaways #
- Use semantic HTML (e.g.,
<button>,<h2>) for headers and content. - Implement ARIA attributes (
aria-expanded,aria-controls,aria-hidden) to convey state and relationships. - Ensure keyboard operability so users can interact with the accordion using
Tab,Enter, andSpace. - Provide dynamic announcements for screen readers to communicate state changes.
- Include clear visual indicators to represent expanded and collapsed states.