Open any settings page you've shipped. Find the toggle switch next to "Dark mode" or "Email notifications." Now open devtools and count the elements it took to build it.

It's almost never one. It's a <label> wrapping a visually-hidden <input type="checkbox">, a <span class="slider"> drawing the track, a ::before pseudo-element drawing the thumb, a CSS transition moving it, and — if whoever built it remembered — an ARIA role telling assistive tech what it actually is.

That last part is the one that gets skipped. Here's the part that stings: even when it isn't, the browser's own form control can't just be a switch. You have to tell it to pretend.

🎮 Try it yourself

▶️ Open the interactive playground →

Runs right in your browser — poke at it and watch the concept react live.

The costume everyone builds

The standard recipe looks like this, and you've written some version of it:

<label class="switch"> <input type="checkbox" /> <span class="slider"></span> </label> 
.switch input { position: absolute; opacity: 0; } .switch .slider { display: inline-block; width: 44px; height: 24px; background: #ccc; border-radius: 999px; position: relative; transition: background-color 0.2s; } .slider::before { content: ""; position: absolute; width: 20px; height: 20px; left: 2px; top: 2px; border-radius: 50%; background: white; transition: transform 0.2s; } input:checked + .slider { background: #2563eb; } input:checked + .slider::before { transform: translateX(20px); } 

It works. It looks right. Click it, and it slides. Ship it, right?

Here's the question that doesn't get asked before shipping: what does a screen reader say when it lands on that control?

By default, VoiceOver and NVDA announce it as a checkbox — "checked" or "not checked" — because, structurally, that's exactly what it is. A <span> that looks like a switch doesn't change what the underlying <input> reports to the accessibility tree. A sighted user sees a switch flip to blue and "ON." A screen reader user hears "checkbox, checked," with zero indication that this control is binary state, not a list-item selection.

Fixing it means one more line most implementations forget:

<input type="checkbox" role="switch" /> 

role="switch" overrides the implicit ARIA role so the control announces "on" / "off" instead of "checked" / "unchecked" — functionally identical to checkbox semantics, just the right vocabulary for a binary state toggle rather than a selection. It's a one-line fix. It's also the line that's missing from a lot of toggle switches in production, because nothing fails loudly when you skip it. The control still works. It's just wrong for anyone who can't see it slide.

Safari shipped the fix as an attribute

In Safari 17.4, WebKit shipped a native switch control — not a new element, an attribute on the one you already have:

<input type="checkbox" switch /> 

Add switch, and three things happen in supporting browsers, for free:

  • The implicit ARIA role flips from checkbox to switch. VoiceOver announces "on" / "off," not "checked" / "unchecked" — no role= attribute required.
  • It renders as an actual toggle — a track and a thumb, matching the platform's native switch look, not a checkbox with a CSS costume.
  • It drops the indeterminate state. A <input type="checkbox"> can be put into a third, indeterminate visual state via JS (.indeterminate = true) for "some of these are checked." A switch is binary by definition — on or off, nothing between — so that state doesn't apply to it. If your "select all" checkbox relies on indeterminate, that one stays a plain checkbox; the switch attribute is for genuinely binary toggles.

Where Safari doesn't support it — and as of this writing, that's everywhere except Safari — the attribute is simply ignored. The element falls back to being exactly what it already was: a normal, unstyled checkbox. Nothing breaks. You don't get the switch look, but you don't get an error either. That's the detail that makes it safe to ship today, and the detail every single-browser feature on this blog gets tested on: does the unsupported path look broken, or just plain? Here, it's plain.

You can feature-detect it directly, no sniffing required:

const supportsSwitch = "switch" in document.createElement("input"); 

Ship the real thing everywhere, today

Here's the part worth taking away even if you never ship to Safari-only users: you don't have to choose between "wait for every browser" and "build the five-element costume." Combine the native attribute with the manual fix from two sections ago, and every browser gets a correct switch — Safari just gets a nicer-looking one for free:

<input type="checkbox" switch role="switch" /> 
input[type="checkbox"][role="switch"] { appearance: none; width: 44px; height: 24px; background: #ccc; border-radius: 999px; position: relative; transition: background-color 0.2s; cursor: pointer; } input[type="checkbox"][role="switch"]::before { content: ""; position: absolute; width: 20px; height: 20px; left: 2px; top: 2px; border-radius: 50%; background: white; transition: transform 0.2s; } input[type="checkbox"][role="switch"]:checked { background: #2563eb; } input[type="checkbox"][role="switch"]:checked::before { transform: translateX(20px); } 

appearance: none strips the native checkbox box in every browser, Safari included — so your CSS draws the track and thumb everywhere, same costume as before, minus the extra <span> and extra markup, because the ::before pseudo-element now draws directly on the real control. Worth being precise here: adding switch doesn't hand rendering to Safari on its own, because appearance: none opts your own CSS out of native rendering there too, the same as it does in every other engine. What the attribute buys you today, even underneath your own CSS, is the free implicit-role flip and the dropped indeterminate state — the same contract role="switch" already gives you, just enforced by the platform instead of by you. If you actually want Safari's native track and thumb, leave appearance alone there and reach for the experimental, flag-gated ::thumb and ::track pseudo-elements instead — not something to depend on yet. Either way, role="switch" guarantees the accessibility tree is right regardless of which path rendered the control.

One attribute removed the CSS hack's biggest failure mode — and it did it by fixing the markup, not by giving you a better <span> to style.

The question worth asking about your own toggles

Go find the toggle switches already live on something you've shipped. Open devtools, click into the accessibility tree, and check what role is actually attached to the control under that slider.

If it says "checkbox" with no role="switch" next to it, that's not a bug anyone will file — nobody files a bug for a control that visually works. It's just quietly wrong for a user you've never heard from. Fix it today; it's one attribute, and unlike the Safari-only switch keyword, role="switch" works everywhere right now.

What does yours say?

🧠 Test yourself

Think it clicked? Take the 8-question quiz →

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

📚 Read next

🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected: