Skip to content
A11Y-03Intermediate5 min readNew

Build an accessible switch

Turn a styled toggle into a real switch with the right role, state, label and keyboard behaviour.

Updated

A switch is a two state control for settings that take effect immediately, such as turning on notifications. It looks simple, which is exactly why so many switches on the web are broken for keyboard and screen reader users. In this lesson you will build one that works for everyone.

Switch, checkbox or toggle button?

ControlUse it whenAnnounced as
SwitchA setting turns on or off and applies right awayswitch, on or off
CheckboxAn option is chosen as part of a form that is submitted latercheckbox, checked or not checked
Toggle buttonA button has a pressed state, such as Bold in a text toolbartoggle button, pressed or not pressed

The finished component

Weekly digestA summary of new lessons every Monday
Sound effectsPlay a sound when you finish a lesson

Accessibility tree

role="switch"

digest checked=true

sounds checked=false

Press Tab, then Space

Accessible switchPress Tab to reach a switch and Space or Enter to toggle it. The panel shows what assistive technology receives. The demo also reports which key you pressed; that bookkeeping is left out of the code.

Step by step

  1. Step 1: Start from a native button

    A <button> is focusable and activates with Space and Enter out of the box. Always set type="button" so it never submits a surrounding form by accident.

  2. Step 2: Add the switch role and its state

    role="switch" changes how the control is announced, and aria-checked carries the current state. Derive it from your component state so the two can never drift apart.

  3. Step 3: Connect the visible label

    Point aria-labelledby at the visible text, so the label people see is the label assistive technology reads. Use aria-describedby for supporting text.

  4. Step 4: Hide the decoration

    The thumb is purely visual. Mark it aria-hidden="true" so it never appears in the accessibility tree.

Styling from the state

Style the switch from its ARIA attribute instead of a separate class. The visuals can then never disagree with what is announced, and a missing attribute becomes an obvious visual bug instead of a silent accessibility bug.

switch.css
.switch {
  position: relative;
  width: 2.75rem;
  height: 1.5rem;
  border-radius: 999px;
  background: var(--track-off);
  transition: background-color 200ms ease;
}

.switch[aria-checked='true'] {
  background: var(--track-on);
}

.switch .thumb {
  position: absolute;
  inset-block: 0.125rem;
  left: 0.125rem;
  width: 1.25rem;
  border-radius: 50%;
  background: white;
  transition: transform 200ms ease;
}

.switch[aria-checked='true'] .thumb {
  transform: translateX(1.25rem);
}

.switch:focus-visible {
  outline: 2px solid var(--focus-color);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .switch,
  .switch .thumb {
    transition: none;
  }
}

Testing checklist

  1. Reach every switch with Tab and confirm the focus ring is clearly visible.
  2. Toggle with both Space and Enter.
  3. With a screen reader, hear the label, the role and the state, for example Weekly digest, switch, on.
  4. Turn on reduced motion in your operating system and confirm the animation stops.
  5. Zoom the page to 200 percent and confirm the switch and its label still fit side by side or wrap cleanly.
  6. Switch to dark mode and check that the off state still has enough contrast against the background.

Summary

  • Use a switch for settings that apply immediately, a checkbox inside forms.
  • Start from <button> and add role="switch" and aria-checked.
  • Label it with the visible text through aria-labelledby.
  • Style from the ARIA state so appearance and announcements always match.

Finished this lesson?

Mark it complete to track your progress across every track in the curriculum.