Switch Toggle

Accessible switch built on a checkbox. No JavaScript needed for the visual.

Tailwind CSS
<label class="inline-flex items-center cursor-pointer">
  <input type="checkbox" class="peer sr-only" />
  <span class="w-11 h-6 rounded-full bg-slate-300 peer-checked:bg-indigo-600 relative transition-colors after:content-[''] after:absolute after:top-0.5 after:left-0.5 after:w-5 after:h-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5"></span>
  <span class="ml-3 text-sm font-medium text-slate-700 dark:text-slate-200">Notifications</span>
</label>

Free to use in personal and commercial projects. No attribution required.

Variants

Same component, different looks. Preview each one live and copy the code you like.

Small

Compact size for dense settings lists and table rows.

Tailwind CSS

Emerald

Green on state for eco brands or success themed settings.

Tailwind CSS

Pre checked

Pre checked example like a dark mode toggle that starts enabled.

Tailwind CSS

About this switch toggle

The switch toggle is the binary setting control: notifications on/off, dark mode, privacy settings. This one is built on a native checkbox with peer-checked, so it is keyboard accessible and works without JavaScript for the visual state.

The knob is an after: pseudo-element that translates 20px (translate-x-5) when checked. The track transitions from gray to indigo. The sr-only checkbox keeps it accessible while the styled span provides the visuals.

Always pair toggles with clear labels. A lone switch means nothing; 'Notifications' next to it means everything. For settings that take effect immediately, no save button is needed. For forms, the toggle is just a styled checkbox.

Settings pages full of checkboxes feel like tax forms. The switch borrows a metaphor everyone already understands from the physical world: the light switch on the wall. Nobody needs to be taught what on looks like. That instant comprehension is why toggles replaced checkboxes for immediate-effect settings across phones and the web: the control explains itself.

A label wraps everything: an sr-only native checkbox acting as the peer, and a styled span as the track (w-11 h-6 rounded-full bg-slate-300). The knob is an after: pseudo-element (after:w-5 after:h-5 after:rounded-full after:bg-white) that slides with peer-checked:after:translate-x-5 while the track flips to peer-checked:bg-indigo-600. Because the real input is a checkbox, keyboard focus, spacebar toggling, and form submission all work with zero JavaScript.

Size the track to its context: w-14 h-7 for primary settings, w-9 h-5 for dense lists (and scale the knob math to match). Use emerald for the on-state when on is good news, like notifications; keep a neutral indigo for neutral settings like layout options. For critical toggles (privacy, billing), add explicit On/Off text so the state never depends on color alone.

The mistake is using a toggle for something that is not a persistent state. A switch labeled Send invite that fires a one-time action and stays flipped on is nonsense: there is no on to persist. Toggles are for states (on/off, enabled/disabled); one-shot actions get buttons. Match the control to the semantics or the interface starts lying.

Frequently asked questions

How do I make a toggle switch in Tailwind?

Use a hidden checkbox with peer, then style a sibling span with peer-checked: variants. The knob is an after: pseudo-element that translates when checked.

Toggle vs checkbox, when to use which?

Toggles for immediate-effect settings (notifications, dark mode). Checkboxes for form options that submit together (interests, agreements).

Are CSS-only toggles accessible?

Yes, when built on a real checkbox. The sr-only input keeps keyboard and screen reader support while the span provides visuals.

Can I build a toggle switch with pure CSS and no JavaScript?

Yes. A visually hidden checkbox plus the peer-checked pattern drives the knob and track with pure CSS, so the visual state needs no JavaScript. You only need JS when the toggle must save the choice or trigger app logic.

Why does my toggle lose keyboard focus visibility?

The native checkbox is visually hidden, so the browser's default focus ring disappears with it. Add a focus style to the visible track using peer-focus-visible:ring-2 or :focus-within on the label. Keyboard users need to see which toggle is focused before they press space.

Should a toggle switch apply immediately or need a save button?

Toggles should apply immediately: flipping a notifications or dark-mode switch should take effect at once. That is exactly what separates a switch from a checkbox, which collects a choice for a later form submit. If the setting needs confirmation, use a checkbox with a save button instead.

Where to use it

  • ✓ Settings panels
  • ✓ Notification preferences
  • ✓ Dark mode switches
  • ✓ Privacy controls

Design notes

When to use

Use it for binary settings that take effect immediately: notifications on or off, dark mode, and privacy controls. Built on a native checkbox, it is keyboard accessible with no JavaScript needed for the visual state. Always pair it with a clear text label.

What to avoid

Do not use a lone switch with no label; a switch by itself means nothing to the user. Do not use it inside a form that needs a save button without making clear whether the toggle applies instantly or on submit.

Accessibility

The native checkbox input gives keyboard and screen reader support for free, so never replace it with a div. Keep the label programmatically associated and ensure a visible focus indicator on the control.

Customization tips

Size with w-14/h-7 for larger or w-9/h-5 for compact. Change the active color to emerald for 'on' semantics. Add labels on both sides for explicit on/off.

Customize it with our tools