CSS Icon Toggle Switch
A toggle switch whose knob shows a cross when off and a check mark when on, so state is not shown by color alone.
<label class="tgl-icon">
<input type="checkbox" role="switch">
<span class="tgl-icon-track" aria-hidden="true"></span>
Email alerts
</label>
.tgl-icon {
position: relative;
display: inline-flex;
align-items: center;
gap: 10px;
color: #500724;
font: 500 15px system-ui, sans-serif;
cursor: pointer;
}
.tgl-icon input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.tgl-icon-track {
position: relative;
width: 56px;
height: 32px;
border-radius: 999px;
background: #e4e4e7;
transition: background 0.3s ease;
}
.tgl-icon-track::before {
content: "✕";
position: absolute;
top: 4px;
left: 4px;
display: grid;
place-items: center;
width: 24px;
height: 24px;
border-radius: 50%;
background: #fff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
color: #71717a;
font-size: 12px;
font-weight: 800;
transition: transform 0.3s cubic-bezier(0.4, 1.4, 0.6, 1);
}
.tgl-icon input:checked + .tgl-icon-track {
background: #db2777;
}
.tgl-icon input:checked + .tgl-icon-track::before {
content: "✓";
color: #db2777;
transform: translateX(24px);
}
.tgl-icon input:focus-visible + .tgl-icon-track {
outline: 2px solid #db2777;
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-icon-track,
.tgl-icon-track::before {
transition: none;
}
}
About this CSS Icon Toggle Switch
This CSS Icon Toggle Switch is 64 lines of pure CSS (1.2 KB) with no JavaScript and no dependencies. It uses transitions, transforms, box shadows and CSS Grid. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.
CSS features used
- transitions
- transforms
- box shadows
- CSS Grid
- Flexbox
- the :checked state (no JavaScript)
- keyboard focus styles
- reduced-motion support
Customize it
The colors in this snippet. Search and replace them to match your brand:
#500724#e4e4e7#fff#71717a#db2777
How to use this icon toggle switch
- Copy the HTML and paste it where you want the element to appear.
- Copy the CSS into your stylesheet, or inside a
<style>tag. - Keep the
.tgl-iconclass name, or rename it in both places. Then tweak colors, sizes and timing to match your design. - Using React or Next.js? Change
classtoclassNamein the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).
Building with an AI tool?
“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.