A real checkbox switch whose knob stretches toward the far side on hover, as if about to flip, inside a soft halo. It still toggles on click, and the stretch works in both states.
Also known as: toggle switch css hover, ios switch hover, material switch hover state.
<label class="hfx-toggle-switch"> <input type="checkbox" role="switch" checked> <span class="hfx-toggle-switch-track" aria-hidden="true"></span> Wi-Fi </label>
.hfx-toggle-switch {
--c: var(--hfx-color, #3b82f6);
font-size: var(--hfx-size, 16px);
position: relative;
display: inline-flex;
align-items: center;
gap: 0.75em;
font-family: system-ui, sans-serif;
font-weight: 600;
cursor: pointer;
}
.hfx-toggle-switch input {
position: absolute;
width: 1px;
height: 1px;
margin: 0;
opacity: 0;
pointer-events: none;
}
.hfx-toggle-switch-track {
position: relative;
width: 3.7em;
height: 2.15em;
border-radius: 99em;
background: color-mix(in srgb, var(--c) 25%, transparent);
transition: background-color 0.25s ease;
}
.hfx-toggle-switch-track::before {
content: "";
position: absolute;
top: 0.225em;
left: 0.225em;
width: 1.7em;
height: 1.7em;
border-radius: 99em;
background: #fff;
box-shadow: 0 0.1em 0.3em rgba(0, 0, 0, 0.3), 0 0 0 0 color-mix(in srgb, var(--c) 22%, transparent);
transition: width 0.3s cubic-bezier(0.3, 0.7, 0.4, 1), transform 0.3s cubic-bezier(0.3, 0.7, 0.4, 1), box-shadow 0.25s ease;
}
.hfx-toggle-switch input:checked + .hfx-toggle-switch-track { background: var(--c); }
.hfx-toggle-switch input:checked + .hfx-toggle-switch-track::before { transform: translateX(1.55em); }
.hfx-toggle-switch input:focus-visible + .hfx-toggle-switch-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
/* hover: the knob stretches (anchored on its resting side) and gains a halo */
.hfx-toggle-switch:hover .hfx-toggle-switch-track::before {
width: 2.15em;
box-shadow: 0 0.1em 0.3em rgba(0, 0, 0, 0.3), 0 0 0 0.45em color-mix(in srgb, var(--c) 22%, transparent);
}
.hfx-toggle-switch:hover input:checked + .hfx-toggle-switch-track::before { transform: translateX(1.1em); }
@media (prefers-reduced-motion: reduce) {
.hfx-toggle-switch, .hfx-toggle-switch *, .hfx-toggle-switch::before, .hfx-toggle-switch::after, .hfx-toggle-switch *::before, .hfx-toggle-switch *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--hfx-color on the element or a parent.
Currently previewing #3b82f6.--hfx-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 16px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.