Switch
On or off, for a setting that takes effect at once. Screen readers hear it as a switch, and the word beside it says the state.
Try it
Your team keeps backups on.
Click or tab to a switch and press Space or Enter. The word beside it says On or Off, so the state never rests on color.
Switch or checkbox
- Use a switch for a setting that takes effect at once, like turning on email updates.
- Use a checkbox when the choice is sent later with a form, like agreeing to terms before Submit.
- Name the setting, not the action: Email updates, not Turn on email updates.
Code
Plain HTML for any site, or React. Both use the same styles, so the switch looks the same wherever it lands.
ReadyAny website, Shopify, WordPress, Square, plain hosting
<link rel="stylesheet" href="tokens.css">
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="forms.css">
<!-- Switch: for a setting that takes effect at once. For choices sent with a form, use a checkbox.
The label sits inside, so the whole row is the target. Screen readers say "switch, on" or "off". -->
<button type="button" class="prism-switch" role="switch" aria-checked="true">
<span class="prism-switch__track" aria-hidden="true"><span class="prism-switch__thumb"></span></span>
<span class="prism-switch__label">Email updates</span>
<span class="prism-switch__state" aria-hidden="true">On</span>
</button>
<script>
document.querySelectorAll(".prism-switch").forEach((sw) => {
sw.addEventListener("click", () => {
const on = sw.getAttribute("aria-checked") !== "true";
sw.setAttribute("aria-checked", String(on));
sw.querySelector(".prism-switch__state").textContent = on ? "On" : "Off";
});
});
</script>Accessibility
- A button with role switch, so screen readers say the name, switch, and on or off.
- The label sits inside the button, so the whole row is the target, at least 44 pixels tall.
- On and Off are written beside the track, so the state is clear without color.
- The off track has a 3 to 1 edge, so it is visible on any background.
- The thumb slides; in the calm version it moves at once.
- In Windows high contrast mode, on uses the system highlight color.