Developer and Text Tools

Custom Toggle Switches and Checkboxes in CSS That Stay Accessible

Style switches, checkboxes and radio buttons with appearance none while keeping keyboard, form and screen reader support, plus contrast checks.

Saizul Amin
Saizul Amin5 Nov 2026 · 4 min read
Custom Toggle Switches and Checkboxes in CSS That Stay Accessible

Default form controls look different in Chrome, Firefox and Safari, and they rarely match a design system. So designers draw a pill-shaped switch or a rounded checkbox, and developers rebuild the control. Too often the rebuild is a div with a click handler, and suddenly the control cannot be reached with the Tab key, does not send its value with the form and is announced by screen readers as nothing in particular. There is a better way that keeps every built-in behaviour.

Style the input, do not replace it

Modern browsers let you remove the default look of a checkbox or radio with appearance: none. What is left is an ordinary box you can size, colour and round like any element, while it remains a real input. It still receives focus, toggles with Space, works with its label, submits with the form and reports checked or not checked to assistive technology. Pseudo-elements draw the rest: a ::before element becomes the switch knob or the radio dot, and a background image draws the checkbox tick.

The CSS uses the input's own states:

  • :checked for the on look, such as a coloured track with the knob moved to the right.
  • :focus-visible for a clear focus ring when someone arrives with the keyboard.
  • :disabled for a faded, not-allowed look.
  • :indeterminate for the dash on a select-all checkbox when only some items are chosen.

Switch, checkbox or radio?

Use a switch for a setting that takes effect immediately, such as notifications or dark mode. Add role="switch" to the input so screen readers say on or off. Use a checkbox for choices that are submitted together with a form, such as accepting terms or picking toppings. Use radio buttons when exactly one option of a group must be chosen, such as a billing plan, and give the group the same name.

Make them visible

WCAG 1.4.11 asks that the parts of a control people need to see have at least 3 to 1 contrast with their surroundings. Light grey switch tracks often fail this on a white page. A one- or two-pixel border, or a darker grey, fixes it. For switches, the difference between on and off should not rely on colour alone, because about one in twelve men has some colour vision deficiency. A tick and cross inside the track, or On and Off text next to it, gives a second clue.

Size matters too. The control itself can be small, around 20 to 24 pixels high, as long as the whole label is clickable and at least 44 pixels tall, so fingers have a comfortable target.

Building one with the generator

The CSS Toggle Switch and Checkbox Generator has a tab for each kind, and each keeps its own design.

  1. Pick the control and a style. Switch presets include iOS style, Material, square, with icons and large. Checkbox presets include rounded, square, circle and outline tick. Radio presets include dot, filled ring, square option and large.
  2. Set size and colours. The switch width follows from its height, and the tool shows the track, knob and travel in pixels. Choose on and off colours, knob or tick colour, border and radius.
  3. Set focus, motion and label side. A strong focus ring colour and a short animation are good defaults.
  4. Check visibility. The checks report the contrast of the off state against white, the knob or tick against the on colour and the focus ring. Try the preview on a dark page as well.
  5. Test with the keyboard. Tab to each control and press Space. The preview includes a disabled example and, for checkboxes, an indeterminate one.
  6. Copy the code. The HTML pattern is a label wrapping an input and a span with the text, each input with its own id.

Extras that come for free

The generated CSS includes a prefers-reduced-motion rule that turns off the sliding animation, and a forced-colors rule so the controls stay visible in Windows High Contrast mode, where custom colours are replaced by system colours. A :has() rule greys out the label of a disabled control in current browsers; older browsers skip it without harm.

Common mistakes to avoid

Hiding the input with display: none and styling a span instead removes it from the keyboard order and from screen readers. Using a click handler on a div means rebuilding focus, keyboard and form support by hand. Removing the focus outline without a replacement leaves keyboard users lost. And a switch that looks off when it is on, because the colours are reversed in a dark theme, confuses everyone. Styling the real input avoids all four.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages