A switch with square corners and a square knob with grip lines, like a slider on a mixing desk. It snaps across on an ease-in-out curve for a mechanical feel. Tailwind version included.
Also known as: square toggle switch css, rectangular switch, flat square switch.
<label class="tgl-square"> <input type="checkbox" role="switch" aria-label="Snap to grid"> <span class="tgl-square-track" aria-hidden="true"><span class="tgl-square-knob"></span></span> </label>
.tgl-square {
--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;
}
.tgl-square input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-square-track {
position: relative;
display: block;
width: 3.6em;
height: 1.9em;
border-radius: 0.3em;
background: color-mix(in srgb, currentColor 16%, transparent);
transition: background-color 0.25s ease;
}
.tgl-square-knob {
position: absolute;
top: 0.2em;
left: 0.2em;
display: grid;
place-items: center;
width: 1.5em;
height: 1.5em;
border-radius: 0.18em;
background: #fff;
box-shadow: 0 0.08em 0.2em rgb(0 0 0 / 0.25);
transition: transform 0.25s cubic-bezier(0.7, 0, 0.3, 1);
}
.tgl-square-knob::before {
content: "";
width: 0.5em;
height: 0.6em;
background: repeating-linear-gradient(90deg, #cbd5e1 0 0.09em, transparent 0.09em 0.2em);
}
.tgl-square input:checked + .tgl-square-track { background: var(--c); }
.tgl-square input:checked + .tgl-square-track .tgl-square-knob { transform: translateX(1.7em); }
.tgl-square input:focus-visible + .tgl-square-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-square, .tgl-square *, .tgl-square::before, .tgl-square::after, .tgl-square *::before, .tgl-square *::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 cursor-pointer">
<input type="checkbox" role="switch" aria-label="Snap to grid" class="peer sr-only">
<span class="block h-8 w-14 rounded-md 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" aria-hidden="true"></span>
<span class="absolute left-1 top-1 grid h-6 w-6 place-items-center rounded bg-white shadow transition-transform duration-200 ease-[cubic-bezier(.7,0,.3,1)] peer-checked:translate-x-6" aria-hidden="true">
<span class="h-2.5 w-2 bg-[repeating-linear-gradient(90deg,#cbd5e1_0_1.5px,transparent_1.5px_3.5px)]"></span>
</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.