CSS Toggle Switch Generator

Generate an accessible CSS toggle switch. Set the track size, knob gap, on and off colors, knob color and animation speed, try it in the preview with the mouse or keyboard, and copy HTML and CSS built on a real checkbox — so it works with screen readers, forms and right-to-left layouts. It's free, runs in your browser, and nothing leaves your device.

Common switch sizes

StyleTrackKnob gap
iOS-like51 × 312px
Material-like52 × 324px
Compact36 × 202px

When to use it

  1. Add on/off settings to a preferences page.
  2. Make a dark-mode toggle.
  3. Replace a div-based switch that screen readers can't operate.
  4. Match iOS- or Android-style switches to your brand colors.

How to Use

  1. Set the width, height and knob gap.
  2. Pick on, off and knob colors and the animation speed.
  3. Edit the label and class name.
  4. Test it with Tab and Space, then copy the HTML and CSS.

Features

  1. Native checkbox with role="switch"
  2. Keyboard and screen-reader support
  3. Focus ring and disabled state
  4. Right-to-left support
  5. Reduced-motion support
  6. 100% client-side

FAQs

Why build a switch on a checkbox?

A real input gives you keyboard support, form submission and screen-reader announcements for free. A div switch needs JavaScript and ARIA to match.

What does role="switch" do?

It tells assistive technology to announce the control as a switch that's on or off, rather than a checkbox that's checked.

How do I read its value?

Like any checkbox: input.checked in JavaScript, or the field is included in form data when on.

Does it work right-to-left?

Yes. The knob slides in the opposite direction on pages with dir="rtl".

Is anything uploaded?

No. The code is generated in your browser.

Technical Details

The switch is a native checkbox with role="switch", visually hidden rather than removed, followed by a styled span. Because the input is still in the document, it gets keyboard focus, toggles with Space, submits with a form and is announced by screen readers as a switch with its on or off state — none of which a div-based switch provides without extra scripting. The knob is a ::after pseudo-element on the track; :checked moves it with transform: translateX by the track width minus the knob and padding, which the generator calculates from the sizes chosen, and changes the track color. A [dir="rtl"] rule moves the knob the other way on right-to-left pages. :focus-visible draws a ring around the track for keyboard users, :disabled dims it, and a reduced-motion block removes the slide animation. The visible label sits inside the same label element, so clicking the text toggles the switch too.

Web & CSS Tools

Web & CSS Tools

  • CSS Checkbox Generator
  • CSS Background Pattern Generator
  • CSS Scrollbar Generator
  • CSS Triangle Generator
  • CSS Text Glitch Generator
  • React Native Shadow Generator
  • CSS Specificity Calculator
  • CSS Color & Property Extractor