SolutionsCase StudiesAI ToolsResourcesAboutContact
Get Your Free AI Automation Audit Book a Free 30-Minute Strategy Call
Playground~/frontend Open editor

~/css/tailwind-toggles

6 Tailwind Toggle Switches

6 TailwindPublished 2026-10-04Free · no sign-up

Six toggle switches written only with Tailwind utility classes. Each uses a real checkbox or radio, hidden with sr-only, and Tailwind's peer and group-has variants to style the visible track, so they need no custom CSS and no JavaScript.

01. Tailwind Peer Switch

TailwindPeer

A switch made with Tailwind's peer modifier: the real checkbox is visually hidden with sr-only and marked as peer, and the track and thumb that follow it react with peer-checked: and peer-focus-visible: classes, so no custom CSS is needed.

View code
<label class="inline-flex items-center gap-3 cursor-pointer">
  <input type="checkbox" role="switch" class="peer sr-only" checked>
  <span class="relative h-7 w-12 rounded-full bg-slate-600 transition-colors peer-checked:bg-blue-500 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-300 peer-focus-visible:ring-offset-2 peer-focus-visible:ring-offset-slate-900 after:absolute after:left-1 after:top-1 after:h-5 after:w-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5"></span>
  <span class="font-semibold text-slate-100">Auto backups</span>
</label>

02. Tailwind Switch with Icons

TailwindIcon

A switch whose thumb shows a cross when off and a tick when on. Both icons are inline SVGs inside the thumb, and group-has-[:checked]: swaps which one is visible, which lets the parent react to the checkbox inside it without any JavaScript.

View code
<label class="group inline-flex items-center gap-3 cursor-pointer">
  <input type="checkbox" role="switch" class="peer sr-only">
  <span class="relative flex h-8 w-14 items-center rounded-full bg-slate-300 p-1 transition-colors peer-checked:bg-emerald-500 peer-focus-visible:ring-2 peer-focus-visible:ring-emerald-400 peer-focus-visible:ring-offset-2">
    <span class="grid h-6 w-6 place-items-center rounded-full bg-white shadow transition-transform group-has-[:checked]:translate-x-6">
      <svg class="h-3.5 w-3.5 text-slate-500 group-has-[:checked]:hidden" viewBox="0 0 16 16" aria-hidden="true"><path d="M4 4l8 8M12 4l-8 8" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
      <svg class="hidden h-3.5 w-3.5 text-emerald-600 group-has-[:checked]:block" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8.5l3 3 7-7" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
    </span>
  </span>
  <span class="font-semibold text-slate-800">Public profile</span>
</label>

03. Tailwind Settings Switch List

TailwindSettings

A stacked settings card with three rows, each a title, a description and a switch. divide-y draws the lines between rows, and each switch is described by its own text with aria-describedby so the explanation is read aloud.

View code
<div class="w-full max-w-sm divide-y divide-slate-100 rounded-2xl bg-white px-5 shadow-lg shadow-slate-900/10">
  <label class="flex cursor-pointer items-center justify-between gap-4 py-4">
    <span><span class="block text-sm font-semibold text-slate-900">Order emails</span><span id="tw-d1" class="block text-sm text-slate-500">Confirmations and shipping updates.</span></span>
    <input type="checkbox" role="switch" aria-describedby="tw-d1" class="peer sr-only" checked>
    <span class="relative h-6 w-11 shrink-0 rounded-full bg-slate-300 transition-colors peer-checked:bg-violet-600 peer-focus-visible:ring-2 peer-focus-visible:ring-violet-400 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5"></span>
  </label>
  <label class="flex cursor-pointer items-center justify-between gap-4 py-4">
    <span><span class="block text-sm font-semibold text-slate-900">Weekly digest</span><span id="tw-d2" class="block text-sm text-slate-500">Sales summary every Monday.</span></span>
    <input type="checkbox" role="switch" aria-describedby="tw-d2" class="peer sr-only">
    <span class="relative h-6 w-11 shrink-0 rounded-full bg-slate-300 transition-colors peer-checked:bg-violet-600 peer-focus-visible:ring-2 peer-focus-visible:ring-violet-400 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5"></span>
  </label>
  <label class="flex cursor-pointer items-center justify-between gap-4 py-4">
    <span><span class="block text-sm font-semibold text-slate-900">Stock alerts</span><span id="tw-d3" class="block text-sm text-slate-500">When an item drops below 5.</span></span>
    <input type="checkbox" role="switch" aria-describedby="tw-d3" class="peer sr-only" checked>
    <span class="relative h-6 w-11 shrink-0 rounded-full bg-slate-300 transition-colors peer-checked:bg-violet-600 peer-focus-visible:ring-2 peer-focus-visible:ring-violet-400 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5"></span>
  </label>
</div>

04. Tailwind On / Off Label Switch

TailwindLabelled

A wide switch that prints OFF and ON inside the track so the state is never colour-only. The words are two spans positioned at each end; group-has-[:checked]: utilities on the label swap their opacity while the thumb slides across with translate-x.

View code
<label class="group inline-flex cursor-pointer items-center gap-3">
  <input type="checkbox" role="switch" class="peer sr-only" checked>
  <span class="relative h-8 w-20 rounded-full bg-slate-700 text-[11px] font-extrabold tracking-wider transition-colors peer-checked:bg-amber-400 peer-focus-visible:ring-2 peer-focus-visible:ring-amber-200 [&>i]:absolute [&>i]:top-1/2 [&>i]:-translate-y-1/2 [&>i]:not-italic after:absolute after:left-1 after:top-1 after:h-6 after:w-6 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-12">
    <i class="right-3 text-slate-300 group-has-[:checked]:opacity-0">OFF</i><i class="left-3 text-amber-950 opacity-0 group-has-[:checked]:opacity-100">ON</i>
  </span>
  <span class="font-semibold text-slate-100">Night shipping</span>
</label>

05. Tailwind Billing Period Toggle

TailwindPricing

A Monthly and Yearly segmented toggle built from two radio inputs inside a fieldset. Each label uses has-[:checked]: to become the raised white segment, so the selected option is styled from its own input without peer ordering tricks.

View code
<fieldset class="inline-flex rounded-full bg-slate-200 p-1 text-sm font-bold">
  <legend class="sr-only">Billing period</legend>
  <label class="cursor-pointer rounded-full px-5 py-2.5 text-slate-600 has-[:checked]:bg-white has-[:checked]:text-slate-900 has-[:checked]:shadow has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-blue-500">
    <input type="radio" name="tw-bill" class="sr-only" checked> Monthly
  </label>
  <label class="cursor-pointer rounded-full px-5 py-2.5 text-slate-600 has-[:checked]:bg-white has-[:checked]:text-slate-900 has-[:checked]:shadow has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-blue-500">
    <input type="radio" name="tw-bill" class="sr-only"> Yearly <span class="text-emerald-700">-20%</span>
  </label>
</fieldset>

06. Tailwind Switch Sizes

TailwindSizes

Small, medium and large switches side by side, showing which utilities change between sizes: the track height and width, the thumb size and the translate distance. Keep these three values in step and any size looks right.

View code
<div class="grid gap-4 text-sm font-semibold text-slate-200">
  <label class="inline-flex cursor-pointer items-center gap-3"><input type="checkbox" role="switch" class="peer sr-only" checked><span class="relative h-5 w-9 rounded-full bg-slate-600 peer-checked:bg-pink-500 after:absolute after:left-0.5 after:top-0.5 after:h-4 after:w-4 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-4 peer-focus-visible:ring-2 peer-focus-visible:ring-pink-300"></span>Small</label>
  <label class="inline-flex cursor-pointer items-center gap-3"><input type="checkbox" role="switch" class="peer sr-only" checked><span class="relative h-6 w-11 rounded-full bg-slate-600 peer-checked:bg-pink-500 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-5 peer-focus-visible:ring-2 peer-focus-visible:ring-pink-300"></span>Medium</label>
  <label class="inline-flex cursor-pointer items-center gap-3"><input type="checkbox" role="switch" class="peer sr-only"><span class="relative h-8 w-14 rounded-full bg-slate-600 peer-checked:bg-pink-500 after:absolute after:left-1 after:top-1 after:h-6 after:w-6 after:rounded-full after:bg-white after:transition-transform peer-checked:after:translate-x-6 peer-focus-visible:ring-2 peer-focus-visible:ring-pink-300"></span>Large</label>
</div>

About these Tailwind Toggle Switches

peer and peer-checked

Mark the hidden input with peer, then any element after it can react with peer-checked: and peer-focus-visible:. The thumb is drawn with after: utilities on the track and moved with peer-checked:after:translate-x-5.

When peer cannot reach

peer only styles later siblings. To style something nested inside a sibling, such as an icon in the thumb or a word in the track, put group on the label and use group-has-[:checked]:, which reacts to a checked input anywhere inside the group.

Keep the focus ring

Because the input is sr-only, the focus ring has to be drawn on the visible track with peer-focus-visible:ring-2. Without it, keyboard users cannot see which switch is focused.

Production builds

The previews load Tailwind's Play CDN. In a project, install Tailwind with your build tool so only the classes you use are shipped; these classes work in Tailwind v3.4 and later.

Frequently asked questions

Which Tailwind version do these need?

Tailwind v3.4 or later, for the has-[] and group-has-[] variants.

Do they need JavaScript?

No. All six are pure HTML and Tailwind classes.

Is there a plain CSS version?

Yes, see the CSS toggle switches collection for the same ideas without Tailwind.