The classic Android switch: a thin track under a bigger round thumb that overhangs it and turns your accent when on. A soft halo spreads around the thumb while it is pressed or focused.
Also known as: material 1 switch css, thin track toggle switch, android switch css.
<label class="tgl-android"> <input type="checkbox" role="switch" aria-label="Location"> <span class="tgl-android-track" aria-hidden="true"></span> <span class="tgl-android-thumb" aria-hidden="true"></span> </label>
.tgl-android {
--c: var(--tgl-color, #3b82f6);
font-size: var(--tgl-size, 24px);
position: relative;
display: inline-block;
vertical-align: middle;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
width: 2.5em;
height: 1.5em;
}
.tgl-android input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-android-track {
position: absolute;
top: 0.3125em;
left: 0.125em;
right: 0.125em;
height: 0.875em;
border-radius: 0.4375em;
background: color-mix(in srgb, currentColor 32%, transparent);
transition: background-color 0.2s ease;
}
.tgl-android-thumb {
position: absolute;
top: 0.125em;
left: 0;
width: 1.25em;
height: 1.25em;
border-radius: 50%;
background: #f5f5f5;
box-shadow: 0 0.06em 0.2em rgb(0 0 0 / 0.4), 0 0 0 0 color-mix(in srgb, currentColor 14%, transparent);
transition:
transform 0.2s cubic-bezier(0.4, 0, 0.2, 1),
background-color 0.2s ease,
box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.tgl-android input:checked + .tgl-android-track { background: color-mix(in srgb, var(--c) 45%, transparent); }
.tgl-android input:checked ~ .tgl-android-thumb {
background: var(--c);
box-shadow: 0 0.06em 0.2em rgb(0 0 0 / 0.4), 0 0 0 0 color-mix(in srgb, var(--c) 22%, transparent);
transform: translateX(1.25em);
}
.tgl-android:active .tgl-android-thumb,
.tgl-android input:focus-visible ~ .tgl-android-thumb { box-shadow: 0 0.06em 0.2em rgb(0 0 0 / 0.4), 0 0 0 0.6em color-mix(in srgb, currentColor 14%, transparent); }
.tgl-android:active input:checked ~ .tgl-android-thumb,
.tgl-android input:checked:focus-visible ~ .tgl-android-thumb { box-shadow: 0 0.06em 0.2em rgb(0 0 0 / 0.4), 0 0 0 0.6em color-mix(in srgb, var(--c) 22%, transparent); }
@media (prefers-reduced-motion: reduce) {
.tgl-android, .tgl-android *, .tgl-android::before, .tgl-android::after, .tgl-android *::before, .tgl-android *::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-block h-6 w-10 cursor-pointer"> <input type="checkbox" role="switch" aria-label="Location" class="peer sr-only"> <span class="absolute inset-x-0.5 top-1/2 h-3.5 -translate-y-1/2 rounded-full bg-gray-400 transition-colors peer-checked:bg-blue-300" aria-hidden="true"></span> <span class="absolute left-0 top-0.5 h-5 w-5 rounded-full bg-gray-50 shadow-md transition peer-checked:translate-x-5 peer-checked:bg-blue-500 peer-focus-visible:ring-8 peer-focus-visible:ring-blue-500/20" aria-hidden="true"></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.