CSS Switch Generator

Make a toggle switch with HTML and CSS only, built on a real checkbox so it works with the keyboard and screen readers.

Processed on your device. Files are not uploaded

How to make a CSS toggle switch

  1. Set the size

    Choose the width of the switch.

  2. Set the colours

    Choose the off colour, the on colour and the knob.

  3. Check the preview

    It is shown switched on.

  4. Copy the code

    Copy the HTML and CSS blocks into your page.

Worked example

This is what CSS Switch Generator gives for its own sample values. Open the tool above and change anything to try your own.

Width (px)
56
Off colour
#cbd5e1
On colour
#0f766e
Knob colour
#ffffff

HTML

<label class="switch">
  <input type="checkbox" role="switch">
  <span class="track"></span>
</label>

CSS

.switch { position: relative; display: inline-block; width: 56px; height: 28px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.track {
  position: absolute; inset: 0;
  background: #cbd5e1;
  border-radius: 28px;
  transition: background 0.2s;
  pointer-events: none;
}
.track::before {
  content: "";
  position: absolute;
  top: 4px; left: 4px;
  width: 20px; height: 20px;
...

About CSS Switch Generator

Make an on and off toggle with HTML and CSS only. It is built on a real checkbox that is hidden but still focusable, so keyboard and screen reader users get the normal behaviour for free.

FAQs

How do I read the value in a form?

It is a normal checkbox, so add a name attribute and it submits like any other checkbox.

Why is the checkbox hidden with opacity?

Hiding it with display: none would remove it from the keyboard and from screen readers.

Can I change the animation?

Yes. Edit the transition lines in the CSS.

Related tools