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 →CSS Box Shadow: A Complete Guide with Examples (2026)
Master CSS box-shadow: every value explained, layering multiple shadows for realistic depth, inset shadows, the spread trick, and copy-paste examples for cards, buttons, and more.
CSS Animations: A Practical Guide to Keyframes and Transitions (2026)
Learn the difference between transitions and keyframe animations, how to write @keyframes, the properties worth animating for 60fps, and easing that actually looks good.
CSS clamp() and Fluid Typography: Stop Writing Font-Size Media Queries (2026)
Learn how CSS clamp() creates type that scales smoothly between screen sizes with a single line, the formula behind it, and how to build a fully fluid type scale without media queries.