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?
The finished component
Accessibility tree
role="switch"
digest checked=true
sounds checked=false
Press Tab, then Space
Step by step
Step 1: Start from a native button
A
<button>is focusable and activates with Space and Enter out of the box. Always settype="button"so it never submits a surrounding form by accident.Step 2: Add the switch role and its state
role="switch"changes how the control is announced, andaria-checkedcarries the current state. Derive it from your component state so the two can never drift apart.Step 3: Connect the visible label
Point
aria-labelledbyat the visible text, so the label people see is the label assistive technology reads. Usearia-describedbyfor supporting text.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 {
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
- Reach every switch with Tab and confirm the focus ring is clearly visible.
- Toggle with both Space and Enter.
- With a screen reader, hear the label, the role and the state, for example Weekly digest, switch, on.
- Turn on reduced motion in your operating system and confirm the animation stops.
- Zoom the page to 200 percent and confirm the switch and its label still fit side by side or wrap cleanly.
- 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 addrole="switch"andaria-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.