Course contentsShow
← Web Development
Lesson 1186 of 2,98221. Accessible ComponentsFree lesson

Responsive Patterns: Tabs to Accordion

Converting tabs to accordions on smaller screens and managing ARIA attributes during the transformation.

Responsive Patterns: Tabs to Accordion

You'll learn how to transform a tab interface into an accordion on smaller screens while maintaining accessibility.

Why Transform Tabs to Accordions?

Tabs work great on desktop with horizontal space, but on mobile they become cramped and hard to use. Accordions stack vertically, making them perfect for narrow screens. Think of it like rearranging furniture: the same content, just reorganized to fit the space.

The Core Pattern

When your viewport shrinks (typically below tablet size), you programmatically:

  1. Restructure the DOM or hide/show alternate markup for the different pattern
  2. Switch ARIA attributes to match the new pattern
  3. Update keyboard handlers to match accordion behavior instead of tab behavior

Managing ARIA During Transformation

From Tabs:

  • role="tablist" on container
  • role="tab" on buttons
  • role="tabpanel" on content areas
  • aria-selected="true" on active tab

To Accordion:

  • Remove tablist, tab, and tabpanel roles
  • Add role="button" (or use native <button>)
  • Use aria-expanded="true/false" on each header
  • Connect headers to panels with aria-controls

Keyboard Behavior Changes

Tabs use arrow keys for navigation; accordions use Tab key to move between headers. Your JavaScript must detect the current pattern and attach the appropriate event listeners.

Many developers use a media query listener (matchMedia) to trigger the transformation, calling separate initialization functions for each pattern.

Key Takeaway: When converting tabs to accordions responsively, you must completely swap ARIA roles and keyboard behaviors to match the active pattern—each has its own distinct accessibility contract with assistive technologies.