The everyday switch: a rounded track, a white knob drawn with ::after and a text label beside it. Short CSS that is easy to restyle, plus a matching Tailwind version built on peer-checked.
Also known as: simple toggle switch css, tailwind toggle switch, basic css switch.
<label class="tgl-simple"> <input type="checkbox" role="switch"> <span class="tgl-simple-track" aria-hidden="true"></span> Notifications </label>
.tgl-simple {
--c: var(--tgl-color, #3b82f6);
font-size: var(--tgl-size, 24px);
position: relative;
display: inline-flex;
vertical-align: middle;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
align-items: center;
gap: 0.75em;
font-family: system-ui, sans-serif;
font-weight: 500;
white-space: nowrap;
}
.tgl-simple input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-simple-track {
position: relative;
width: 2.75em;
height: 1.5em;
border-radius: 0.75em;
background: color-mix(in srgb, currentColor 22%, transparent);
transition: background-color 0.2s ease;
}
.tgl-simple-track::after {
content: "";
position: absolute;
top: 0.125em;
left: 0.125em;
width: 1.25em;
height: 1.25em;
border-radius: 50%;
background: #fff;
box-shadow: 0 0.05em 0.15em rgb(0 0 0 / 0.3);
transition: transform 0.2s ease;
}
.tgl-simple input:checked + .tgl-simple-track { background: var(--c); }
.tgl-simple input:checked + .tgl-simple-track::after { transform: translateX(1.25em); }
.tgl-simple input:focus-visible + .tgl-simple-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-simple, .tgl-simple *, .tgl-simple::before, .tgl-simple::after, .tgl-simple *::before, .tgl-simple *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}The same effect using Tailwind utility classes. No custom CSS or config needed.
<label class="relative inline-flex cursor-pointer items-center gap-3"> <input type="checkbox" role="switch" class="peer sr-only"> <span class="relative h-6 w-11 rounded-full bg-gray-300 transition-colors peer-checked:bg-blue-500 peer-focus-visible:ring-2 peer-focus-visible:ring-blue-500 peer-focus-visible:ring-offset-2 after:absolute after:left-0.5 after:top-0.5 after:h-5 after:w-5 after:rounded-full after:bg-white after:shadow after:transition-transform after:content-[''] peer-checked:after:translate-x-5" aria-hidden="true"></span> <span class="font-medium">Notifications</span> </label>
--tgl-color on the element or a parent.
Currently previewing #3b82f6.--tgl-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 24px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.