How to build a CSS toggle switch
A toggle switch is a checkbox with different paint and a different promise. The paint is a track and a sliding thumb; the promise is that flipping it takes effect right now, with nothing to confirm. Keep the native input underneath so keyboard, form, and screen-reader behaviour come for free.
This generator emits that markup and CSS. Set one width and the height, thumb size, and travel distance are derived from it, so the proportions stay correct at any size.
- Set the width between 36px and 96px. Everything else is derived from it.
- Pick the on colour and the off colour.
- Copy the HTML and CSS snippet and move the styles into your stylesheet.
- Add role="switch" to the input so screen readers announce on and off rather than checked and unchecked.
- Wire the change event to apply the setting immediately, with no Save step.