CSS toggle switch generator

This CSS toggle switch generator creates an accessible checkbox-based on/off control, using :checked, transform and transition to slide the knob, with a live preview and copy-ready HTML and CSS.

A CSS toggle switch is a checkbox styled as a sliding on/off control: set the size and colours below and copy accessible HTML and CSS that works without JavaScript.

#cbd5e1
#2563eb
#ffffff

Click the switch or press Space after Tab to focus it.

HTML

<label class="switch">
  <input type="checkbox" role="switch">
  <span class="slider" aria-hidden="true"></span>
  <span class="switch-label">Notifications</span>
</label>

CSS

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font: inherit;
}

.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.switch .slider {
  position: relative;
  display: inline-block;
  flex: none;
  width: 52px;
  height: 30px;
  background: #cbd5e1;
  border-radius: 15px;
  transition: background-color 200ms ease;
}

.switch .slider::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  background: #ffffff;
  border-radius: 50%;
  transition: transform 200ms ease;
}

.switch input:checked + .slider {
  background: #2563eb;
}

.switch input:checked + .slider::before {
  transform: translateX(22px);
}

.switch input:focus-visible + .slider {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  .switch .slider,
  .switch .slider::before {
    transition: none;
  }
}

How the checkbox switch works

The markup is a label wrapping a checkbox, a track span and a text label. The checkbox itself is visually hidden but still focusable, so the browser keeps its native click and keyboard behaviour. The track is the .slider span, and the knob is its ::before pseudo-element. When the checkbox is checked, input:checked + .slider changes the track colour and moves the knob with translateX(). Because the state lives in the checkbox, the switch submits with a form and needs no script.

The knob travel is the track width minus the track height, which keeps the knob inside the track at any size. If you are styling the button that sits next to a switch, the CSS button generator uses the same live-preview and copy workflow. To tune the sliding motion itself, open the cubic-bezier generator and paste the easing into the transition.

Making the switch accessible

  • Add role="switch" to the checkbox so screen readers announce an on/off switch rather than a checkbox.
  • Keep the input focusable and show input:focus-visible + .slider with a 2px outline and 2px offset.
  • Give every switch a visible text label, not just a colour change, so the meaning does not rely on colour alone.
  • Wrap the transition in @media (prefers-reduced-motion: reduce) and set it to none for visitors who ask for less motion.

Frequently asked questions

How do I make a CSS toggle switch accessible?

Use a real checkbox input with role="switch", keep it focusable instead of hiding it with display:none, and give it a visible text label. Add a focus-visible outline on the track so keyboard users can see where focus is, and turn off the sliding transition inside a prefers-reduced-motion media query.

Does a CSS toggle switch need JavaScript?

No. A checkbox plus a label and the :checked pseudo-class gives you a working on/off switch with no JavaScript at all. The browser handles the click, the keyboard Space key and the form value, so the same markup works in a plain HTML file and inside a framework.

Why is my toggle switch not keyboard accessible?

The usual cause is hiding the checkbox with display:none, which removes it from the tab order. Use a visually hidden style instead: position it absolutely, clip it to one pixel and keep it in the document. Then style the sibling track with input:checked + .slider and add input:focus-visible + .slider for the outline.

More CSS generators

Color tools

Image tools

Image & SVG tools

Typography & units

More free tools

All free tools