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.
How to make a CSS toggle switch
Set the size
Choose the width of the switch.
Set the colours
Choose the off colour, the on colour and the knob.
Check the preview
It is shown switched on.
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.