How it works
This toggle button animation is an on/off switch whose knob springs across the track while the track changes from Off color to On color. It looks like the iOS-style switch, and it is built on a real checkbox, so it behaves like a form control.
The root is a label element containing a visually hidden checkbox input, a track and a round knob. Styles key off the checked state with :has(): when the label contains a checked input, the track background transitions to On color and the knob moves with transform: translateX(Width minus Height px), which is exactly the travel from the left inset to the right one. The knob is 80% of Height with a 10% inset and a soft drop shadow. In Tailwind that becomes has-[:checked]: and group-has-[:checked]: variants; the React version keeps checked in useState.
Parameters: Width, Height, On color, Off color, Knob color, Duration and Easing. Keep Width roughly twice Height. The default back-out easing overshoots slightly at the end, which gives the knob its springy feel; use ease-out for a plainer switch.
Because the hidden input remains focusable, the switch works with Tab and Space, and the CSS output adds a :focus-visible outline on the label. Replace the default aria-label="Toggle" with the setting's name, and consider role="switch" on the input so screen readers say on/off instead of checked.
Can I use this toggle switch without JavaScript?
Yes. The CSS version is driven entirely by the native checkbox and the :has(:checked) selector, so clicking the label toggles the input and the styles follow. Read the value like any checkbox in a form submission. You only need JavaScript if the toggle should trigger something immediately, such as saving a setting.
How is the knob travel distance calculated?
The knob is 80% of the track height and sits 10% from the top and left. To end at the same 10% inset on the right, it must move the track width minus the track height. The tool derives it from Width and Height, so the knob always lands flush with the right edge when you resize.
Does :has() work in all browsers?
The :has() selector works in current versions of Chrome, Edge, Safari and Firefox. For very old browsers you can switch to the sibling pattern: place the input before the track and use input:checked + .track instead. The Tailwind and React outputs are not affected, since React keeps the state in a variable.