Toggle style
Click the preview toggle to flip it on and off.
Preview and CSS
Design a custom toggle switch: size, on and off colors, knob style and animation speed. Click the live preview to flip it, then copy the CSS.
Click the preview toggle to flip it on and off.
The accessible pattern is a button with role="switch" and aria-checked, not a div. Keyboard users can tab to it and flip it with space, and screen readers announce it as a switch. The knob is an absolutely positioned span that slides with a left transition.
The knob sits 4px from each edge, so its travel distance is width minus knob size minus 8px. Getting this formula right keeps the knob perfectly inset at both ends at any size, which is what separates a polished toggle from a janky one.