The knob moves like a drop of liquid: its leading edge races ahead and the trailing edge catches up, so it stretches into a pill mid-travel and pulls back round. Two staggered transitions do it.
Also known as: gooey toggle switch, stretchy knob switch, liquid toggle css.
<label class="tgl-liquid-stretch"> <input type="checkbox" role="switch" aria-label="Live updates"> <span class="tgl-liquid-stretch-track" aria-hidden="true"></span> </label>
.tgl-liquid-stretch {
--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-liquid-stretch input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-liquid-stretch-track {
position: relative;
display: block;
width: 4em;
height: 2em;
border-radius: 1em;
background: color-mix(in srgb, var(--c) 14%, transparent);
transition: background-color 0.3s ease;
}
/* going left the left edge leads; going right the right edge leads */
.tgl-liquid-stretch-track::before {
content: "";
position: absolute;
top: 0.25em;
bottom: 0.25em;
left: 0.25em;
right: 2.25em;
border-radius: 0.75em;
background: color-mix(in srgb, var(--c) 60%, transparent);
transition:
left 0.32s cubic-bezier(0.6, 0, 0.2, 1),
right 0.32s cubic-bezier(0.6, 0, 0.2, 1) 0.1s,
background-color 0.3s ease;
}
.tgl-liquid-stretch input:checked + .tgl-liquid-stretch-track { background: color-mix(in srgb, var(--c) 24%, transparent); }
.tgl-liquid-stretch input:checked + .tgl-liquid-stretch-track::before {
left: 2.25em;
right: 0.25em;
background: var(--c);
transition:
right 0.32s cubic-bezier(0.6, 0, 0.2, 1),
left 0.32s cubic-bezier(0.6, 0, 0.2, 1) 0.1s,
background-color 0.3s ease;
}
.tgl-liquid-stretch input:focus-visible + .tgl-liquid-stretch-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-liquid-stretch, .tgl-liquid-stretch *, .tgl-liquid-stretch::before, .tgl-liquid-stretch::after, .tgl-liquid-stretch *::before, .tgl-liquid-stretch *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--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.