Full answer
Start with the right element. Two good options:
- A
buttonwithrole="switch"andaria-checked="true"or"false". A script flips the value on click. A button is focusable and works with Enter and Space, with no extra code. - A real checkbox, styled to look like a switch. This is the simplest way when the control is part of a form, because the browser keeps the state, and
:checkedstyles it. Keep the label connected withforor by wrapping.
A common mistake is a div or span styled as a switch. It cannot be reached by the keyboard, and screen readers do not know it is a control.
For the button version:
- The visible text or
aria-labelis the name, for example "Email alerts". The name should not change when the state changes, because the state is announced separately. Hide any extra "On" or "Off" text from screen readers witharia-hidden="true". - CSS reads the state with an attribute selector:
[role="switch"][aria-checked="true"]. This keeps the visual state and the announced state in one place. - Add
:focus-visiblestyles, and make the target at least about 24 to 44 CSS pixels so it is easy to hit. - Do not show state with colour only. The text "On" or "Off", or the position of the knob, helps users who cannot tell the colours apart.
Clicking the button or pressing Space or Enter while it is focused turns it on. The knob moves, the colours change and the small text changes to "On". The name stays "Email alerts", and the small text is hidden from screen readers, because the switch role already announces the on or off state.
Test with the keyboard and with a screen reader before using this widely.
<style>
.sw { display: inline-flex; align-items: center; gap: 8px; padding: 4px 8px; border: 1px solid #666; border-radius: 16px; background: #fff; font: 16px sans-serif; cursor: pointer; }
.sw .knob { width: 16px; height: 16px; border-radius: 50%; background: #666; transition: transform 0.15s; }
.sw[aria-checked="true"] { background: #e6f4ea; border-color: #137333; }
.sw[aria-checked="true"] .knob { background: #137333; transform: translateX(8px); }
.sw:focus-visible { outline: 3px solid #0a6cff; outline-offset: 2px; }
</style>
<button type="button" class="sw" id="sw" role="switch" aria-checked="false">
<span class="knob"></span><span>Email alerts</span><span id="state" aria-hidden="true">Off</span>
</button>
<script>
const sw = document.getElementById("sw");
sw.addEventListener("click", () => {
const on = sw.getAttribute("aria-checked") === "true";
sw.setAttribute("aria-checked", String(!on));
document.getElementById("state").textContent = on ? "Off" : "On";
});
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Use a button with role=switch, or a styled checkbox.
- aria-checked holds the state for assistive technology.
- CSS can style [aria-checked="true"] so the visual state matches.
- Keep :focus-visible styles and a large target.
- Do not use colour as the only state clue.
Common mistakes
- Building the switch from a div with no keyboard access or role.
- Changing the accessible name when the state changes, so a screen reader announces the state twice.
- Showing the state with colour alone.
Follow-ups
- Why is a real button better than a div here?
- When would you use a styled checkbox instead?
Tips
- Name both options: button with role=switch, and a styled checkbox. Say when each fits.
- Say that CSS should read the ARIA attribute, so the visual and the announced state cannot drift apart.