FRENVersion française

Input

Toggle switch

Interrupteur

Demo

Loading demo…

A two-state control, on or off, modeled on a physical switch. The change takes effect immediately, with no Save button.

Story

Did you know

The 2007 iPhone made the sliding switch standard on mobile by imitating a physical switch. It took seventeen years for a browser to offer a native one in HTML, and fittingly, Safari got there first.

Timeline

  1. physicalThe toggle switch of electronics and control panels.
  2. 2007The iPhone popularizes the sliding switch on touchscreens.
  3. 2014Material Design brings the switch to Android.
  4. 2024Safari 17.4 ships <input type="checkbox" switch>.

Vocabulary

Names

FR
Interrupteur, bouton bascule, toggle, bouton on/off
EN
Toggle switch, switch, on/off toggle, toggle button

Common uses

  • Settings that apply instantly (Wi-Fi, dark mode)
  • Turning a feature on or off
  • Notification preferences

Often paired with

The components it usually sits with, by page type, in reading order.

Code

<label class="setting">
  Dark mode
  <!-- "switch" = native switch in Safari, plain checkbox elsewhere -->
  <input type="checkbox" switch role="switch" id="dark">
</label>

<script>
  const dark = document.querySelector('#dark')

  // A switch applies right away: no Save button
  dark.addEventListener('change', () => {
    document.documentElement.classList.toggle('dark', dark.checked)
  })
</script>

Good to know

  • Safari 17.4 shipped <input type="checkbox" switch> in 2024. Other browsers ignore the attribute and show a regular checkbox, so adding it is safe.
  • A switch acts immediately. If people have to confirm with a Save button, use a checkbox instead.
  • For styling, start from a real <input> with appearance: none. You keep label clicks, keyboard support and form submission.
  • If the setting is saved on a server, flip the UI right away, then roll back with a message if the request fails.

Accessibility

Must have

  • Use <input type="checkbox" role="switch">, or a <button role="switch" aria-checked="false">. Screen readers then say "on / off" instead of "checked".
  • A visible label tied to the control: a wrapping <label>, for, or aria-labelledby.
  • Space toggles the state. You get it for free with an input or a button, and must wire it yourself on a <div>.

Often forgotten

  • The state must not rely on color alone (WCAG 1.4.1): thumb position, an I/O icon or an "On" label.
  • The label does not change with the state. "Dark mode" stays "Dark mode", otherwise the announcement contradicts itself.
  • Track and thumb need a 3:1 contrast against the background, in both states (WCAG 1.4.11).

Nice to have

  • Make the whole row clickable with a wrapping label, and keep the target at least 24 × 24 px (WCAG 2.5.8).
  • Group related switches in a <fieldset> with a <legend>.

Who uses it