Skip to content
IndexRequest a UIAboutContact

Toggle Switch

An on/off switch with a sliding thumb for settings that apply immediately.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Create a toggle switch using a real input type="checkbox" with role="switch" visually hidden but focusable, and a label wrapper. The track is 52x32px with a 16px radius; off colour is the surface-2 tone with a border, on colour is the accent. The thumb is a 24px circle at 4px inset that translates 20px on checked, with 220ms cubic-bezier(0.3,1.3,0.5,1). While pressed the thumb widens to 28px. Show a 4px halo on hover and a 2px focus ring (via :focus-visible on the hidden input using sibling selectors). Add a text "On"/"Off" next to the label updated with JS and aria-live not required. Provide a disabled example at 40% opacity. Clicking the label text toggles the switch. Remove transitions under prefers-reduced-motion.

About this pattern

Definition

A binary control that looks like a physical switch. A circular thumb slides along a track, and the track changes colour to show the on state. It is used for settings that take effect immediately.

Purpose & UX principles

  • Use a switch for instant settings ("Notifications on") and a checkbox for choices that are submitted later with a form.
  • The label states the setting, not the action. "Dark mode" is clearer than "Toggle dark mode".
  • Do not rely on colour alone: the thumb position and an optional On/Off text also show the state.

Interaction states

  • Off, on, hover (thumb halo), pressed (thumb stretches slightly), focus-visible ring, disabled.
  • Built on a real input type="checkbox" role="switch" so it is keyboard and screen-reader accessible (Space toggles).

Motion specification

  • Thumb: translateX 220ms cubic-bezier(0.3, 1.3, 0.5, 1).
  • Pressed thumb stretches from 24px to 28px wide.
  • Track colour: 180ms.
  • Reduced motion: no slide, state changes instantly.

Responsive behaviour

  • Track 52×32px so the hit area is comfortable; the whole row including the label is clickable.

Code

HTML
<div class="list">
  <label class="row"><span>Notifications<small id="s1">Off</small></span><input type="checkbox" role="switch" class="sw"><i class="trk"><b></b></i></label>
  <label class="row"><span>Dark mode<small id="s2">On</small></span><input type="checkbox" role="switch" class="sw" checked><i class="trk"><b></b></i></label>
  <label class="row dis"><span>Beta features<small>Unavailable</small></span><input type="checkbox" role="switch" class="sw" disabled><i class="trk"><b></b></i></label>
</div>

Related patterns

Checkbox

↗
CheckboxInputs & Forms

Radio Button

↗
Radio ButtonInputs & Forms

Validation States

↗
Validation StatesInputs & Forms