What is the CSS Toggle Switch Generator?
Build custom CSS toggle switches with smooth animations. Accessible markup included with every generated style.
Everything runs locally in your browser. Adjust the controls above, preview instantly, and copy clean, production-ready CSS — no account, no upload.
Using the CSS Toggle Switch Generator
Everything lives in the panel at the top of this page. There is nothing to install — it all runs right here in your browser.
Set toggle dimensions
Adjust the track width and height. The thumb diameter is automatically calculated at 75% of the track height with equal margins.
Choose colours
Set the off-state and on-state track colours. The thumb colour defaults to white.
Copy the HTML and CSS
The generator outputs both the CSS and the minimal HTML markup. Use them together for a working accessible toggle.
What the generated code looks like
Clean, production-ready CSS — no vendor bloat, no unnecessary declarations.
.toggle {
position: relative;
display: inline-block;
width: 48px;
height: 28px;
}
.toggle input {
opacity: 0;
width: 0;
height: 0;
}
.toggle-slider {
position: absolute;
inset: 0;
background: #ccc;
border-radius: 28px;
cursor: pointer;
transition: background 0.2s;
}
.toggle-slider::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
left: 4px;
top: 4px;
background: #fff;
border-radius: 50%;
transition: transform 0.2s;
}
input:checked + .toggle-slider {
background: #0071e3;
}
input:checked + .toggle-slider::before {
transform: translateX(20px);
}The actual input is hidden with opacity: 0 (not display: none) so it remains focusable and keyboard-operable. Screen readers read the input label, not the visual slider.
Common mistakes & how to fix them
A few habits trip people up. Here is what to watch for — and the exact fix.
Hiding input with display:none breaks accessibility
display: none removes the element from the accessibility tree. Screen readers cannot read a toggle with a hidden input.
Fix: Use opacity: 0; width: 0; height: 0 to visually hide the input while keeping it focusable.
Missing label element
A toggle without an associated label has no accessible name.
Fix: Wrap the input and slider in a <label> element, or use aria-label on the input.
Pro tips for better results
Add :focus-visible to the slider for keyboard users
When the hidden input is focused via keyboard, add a visible outline to .toggle-slider using the :has selector: .toggle:has(input:focus-visible) .toggle-slider { outline: 2px solid #0071e3; }
Frequently asked questions
From the blog
Read more →Modern CSS techniques every developer should know
A practical tour of the CSS features that replaced yesterday’s hacks.
From design to code without the friction
How visual generators speed up the build without sacrificing clean output.
Writing CSS that scales with your project
Tokens, naming, and structure that keep large stylesheets maintainable.