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

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