Toggle Switch

Create an accessible toggle switch using a checkbox or button with proper ARIA roles and states.

intermediate
2.1.1 Keyboard (Level A)4.1.2 Name, Role, Value (Level A)1.4.11 Non-text Contrast (Level AA)
❌

The Bad (Inaccessible)

<div class="toggle-container">
  <span>Dark Mode</span>
  <div class="toggle" onclick="toggleDarkMode()">
    <div class="toggle-thumb"></div>
  </div>
</div>
βœ…

Accessibility-Ready Code

<label class="toggle-switch">
  <input
    type="checkbox"
    role="switch"
    aria-checked="false"
    id="dark-mode-toggle"
  >
  <span class="toggle-track">
    <span class="toggle-thumb"></span>
  </span>
  <span class="toggle-label">Dark Mode</span>
</label>

The Standard

A toggle switch is a specialized control that represents an on/off state. Unlike checkboxes (which are for selection), switches are for immediate actions. Use role="switch" and aria-checked to communicate the state.

WCAG Criteria

  • 2.1.1 Keyboard (Level A): Toggles must be operable via keyboard.
  • 4.1.2 Name, Role, Value (Level A): The switch role and checked state must be announced.
  • 1.4.11 Non-text Contrast (Level AA): The track/thumb must have 3:1 contrast.

❌ The Bad (Inaccessible)

What’s Wrong?

  1. No Keyboard Access: A <div> with onclick cannot receive focus.
  2. No Role: Screen readers don’t know this is a toggle control.
  3. No State: Users cannot tell if it’s currently on or off.
  4. No Label Association: The β€œDark Mode” text is not connected to the control.

βœ… The Good (Accessibility-Ready Code)

Why This Works

  1. Checkbox Foundation: Uses a real <input type="checkbox"> for native keyboard support.
  2. Switch Role: role="switch" tells screen readers this is a toggle, not a checkbox.
  3. State Exposed: aria-checked announces the current on/off state.
  4. Label Wrapping: The <label> element connects the text to the control.

Accessibility Checklist

  • Use a real <input type="checkbox"> as the foundation.
  • Add role="switch" to indicate toggle behavior.
  • Include aria-checked reflecting the current state.
  • Wrap in a <label> or use aria-labelledby.
  • Ensure the track has sufficient color contrast (3:1 minimum).
  • Provide visible focus styles on the control.
πŸ“ Perfect Implementation Reference
<label>
  <input type="checkbox" role="switch" aria-checked="true">
  <span>Allow Cookies</span>
</label>

Technical Deep Dive

The Accessibility Tree

Role: switch
Name: "Dark Mode"
State: checked / unchecked

Screen Reader Announcements

  • VoiceOver (Mac): β€œDark Mode, switch, off”
  • NVDA: β€œDark Mode, switch, not pressed”

Focus Flow

  1. Tab Key: Captures focus on the hidden checkbox.
  2. Space Key: Toggles the checked state.
  3. State Sync: aria-checked updates before visual animation completes.

CSS Technique

The visual toggle is created using CSS on the .toggle-track and .toggle-thumb elements, while the actual <input> is visually hidden but remains in the accessibility tree.

πŸ§ͺ

Interactive Behavioral Lab

πŸ’»

Interactive Sandbox

LIVE PREVIEW

πŸ”¬ Technical Internals

Understand how this component is processed by the browser and Assistive Technology (AT). This section bridges the gap between visual code and the hidden logic that powers accessibility.

🌲 Accessibility Tree

The data structure used by screen readers to "see" your page. It translates HTML roles and attributes into standardized objects.

βš™οΈ Event Logic

Expected behavioral standards for keyboard navigation and state transitions. Crucial for users who don't use a mouse.

  • Focus: Highlights via :focus-visible
  • Activation: Responds to Enter and Space
  • Role: Identified as Switch Pattern

🌐 Browser & Screen Reader Compatibility

Browser
Screen Reader
Status
🍎 Safari
VoiceOver
βœ“ Safe
πŸͺŸ Chrome
NVDA
βœ“ Safe
πŸͺŸ Edge
JAWS
βœ“ Safe
🦊 Firefox
NVDA
βœ“ Safe
πŸ“± iOS Safari
VoiceOver
βœ“ Safe
πŸ€– Chrome Android
TalkBack
βœ“ Safe