47 CSS Toggles & Switches42 / 47
Glassmorphism
A frosted-glass pill with a backdrop-blurred thumb. The colored backdrop tints the glass; the thumb floats above it. Modern dark-mode aesthetic.
Published Updated
The code
<label class="tgl-42">
<input class="tgl-42__input" type="checkbox" checked>
<span class="tgl-42__track" aria-hidden="true">
<span class="tgl-42__thumb"></span>
</span>
<span class="tgl-42__label">Focus mode</span>
</label><label class="tgl-42">
<input class="tgl-42__input" type="checkbox" checked>
<span class="tgl-42__track" aria-hidden="true">
<span class="tgl-42__thumb"></span>
</span>
<span class="tgl-42__label">Focus mode</span>
</label>.tgl-42 {
font-family: "Inter", "Segoe UI", system-ui, sans-serif;
font-size: 14px;
color: #f0eeff;
min-height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24px;
padding: 48px 20px;
background: #14141e;
box-sizing: border-box;
}
.tgl-42__input {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
.tgl-42__track {
position: relative;
width: 56px;
height: 30px;
background: rgba(255,255,255,0.08);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.18);
border-radius: 999px;
transition: background 0.3s ease, border-color 0.3s ease;
box-sizing: border-box;
}
.tgl-42__thumb {
position: absolute;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
border-radius: 50%;
background: rgba(255,255,255,0.85);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
box-shadow: inset 0 1px 0 rgba(255,255,255,0.6), 0 2px 6px rgba(0,0,0,0.18);
transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease;
}
.tgl-42__input:checked ~ .tgl-42__track {
background: rgba(124,108,255,0.32);
border-color: rgba(124,108,255,0.55);
}
.tgl-42__input:checked ~ .tgl-42__track .tgl-42__thumb {
/* Inner width: 56 - 2 = 54. Thumb 24. Max left to stay inside: 54-24-2 = 28. */
left: 28px;
background: rgba(255,255,255,0.95);
}
.tgl-42__input:focus-visible ~ .tgl-42__track {
outline: 2px solid rgba(255,255,255,0.7);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-42__track,
.tgl-42__thumb {
transition: none;
}
}.tgl-42 {
font-family: "Inter", "Segoe UI", system-ui, sans-serif;
font-size: 14px;
color: #f0eeff;
min-height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24px;
padding: 48px 20px;
background: #14141e;
box-sizing: border-box;
}
.tgl-42__input {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
.tgl-42__track {
position: relative;
width: 56px;
height: 30px;
background: rgba(255,255,255,0.08);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.18);
border-radius: 999px;
transition: background 0.3s ease, border-color 0.3s ease;
box-sizing: border-box;
}
.tgl-42__thumb {
position: absolute;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
border-radius: 50%;
background: rgba(255,255,255,0.85);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
box-shadow: inset 0 1px 0 rgba(255,255,255,0.6), 0 2px 6px rgba(0,0,0,0.18);
transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease;
}
.tgl-42__input:checked ~ .tgl-42__track {
background: rgba(124,108,255,0.32);
border-color: rgba(124,108,255,0.55);
}
.tgl-42__input:checked ~ .tgl-42__track .tgl-42__thumb {
/* Inner width: 56 - 2 = 54. Thumb 24. Max left to stay inside: 54-24-2 = 28. */
left: 28px;
background: rgba(255,255,255,0.95);
}
.tgl-42__input:focus-visible ~ .tgl-42__track {
outline: 2px solid rgba(255,255,255,0.7);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-42__track,
.tgl-42__thumb {
transition: none;
}
}/* No JavaScript — a blurred 8%-white glass track over a gradient backdrop tints violet on :checked while a denser glass puck slides across. */
/* No JavaScript — a blurred 8%-white glass track over a gradient backdrop tints violet on :checked while a denser glass puck slides across. */Here's a working CSS Toggles & Switche from CodeFronts. Use it as-is or adapt to your framework. All classes are scoped under a unique prefix so the code won't collide with your existing styles. MIT licensed.
Demo: Glassmorphism
Source: https://codefronts.com/components/css-toggles-switches/glassmorphism/
A frosted-glass pill with a backdrop-blurred thumb. The colored backdrop tints the glass; the thumb floats above it. Modern dark-mode aesthetic.
## HTML
```html
<label class="tgl-42">
<input class="tgl-42__input" type="checkbox" checked>
<span class="tgl-42__track" aria-hidden="true">
<span class="tgl-42__thumb"></span>
</span>
<span class="tgl-42__label">Focus mode</span>
</label>
```
## CSS
```css
.tgl-42 {
font-family: "Inter", "Segoe UI", system-ui, sans-serif;
font-size: 14px;
color: #f0eeff;
min-height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24px;
padding: 48px 20px;
background: #14141e;
box-sizing: border-box;
}
.tgl-42__input {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
.tgl-42__track {
position: relative;
width: 56px;
height: 30px;
background: rgba(255,255,255,0.08);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.18);
border-radius: 999px;
transition: background 0.3s ease, border-color 0.3s ease;
box-sizing: border-box;
}
.tgl-42__thumb {
position: absolute;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
border-radius: 50%;
background: rgba(255,255,255,0.85);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
box-shadow: inset 0 1px 0 rgba(255,255,255,0.6), 0 2px 6px rgba(0,0,0,0.18);
transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease;
}
.tgl-42__input:checked ~ .tgl-42__track {
background: rgba(124,108,255,0.32);
border-color: rgba(124,108,255,0.55);
}
.tgl-42__input:checked ~ .tgl-42__track .tgl-42__thumb {
/* Inner width: 56 - 2 = 54. Thumb 24. Max left to stay inside: 54-24-2 = 28. */
left: 28px;
background: rgba(255,255,255,0.95);
}
.tgl-42__input:focus-visible ~ .tgl-42__track {
outline: 2px solid rgba(255,255,255,0.7);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-42__track,
.tgl-42__thumb {
transition: none;
}
}
```
## JavaScript
```js
/* No JavaScript — a blurred 8%-white glass track over a gradient backdrop tints violet on :checked while a denser glass puck slides across. */
```Here's a working CSS Toggles & Switche from CodeFronts. Use it as-is or adapt to your framework. All classes are scoped under a unique prefix so the code won't collide with your existing styles. MIT licensed.
Demo: Glassmorphism
Source: https://codefronts.com/components/css-toggles-switches/glassmorphism/
A frosted-glass pill with a backdrop-blurred thumb. The colored backdrop tints the glass; the thumb floats above it. Modern dark-mode aesthetic.
## HTML
```html
<label class="tgl-42">
<input class="tgl-42__input" type="checkbox" checked>
<span class="tgl-42__track" aria-hidden="true">
<span class="tgl-42__thumb"></span>
</span>
<span class="tgl-42__label">Focus mode</span>
</label>
```
## CSS
```css
.tgl-42 {
font-family: "Inter", "Segoe UI", system-ui, sans-serif;
font-size: 14px;
color: #f0eeff;
min-height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 24px;
padding: 48px 20px;
background: #14141e;
box-sizing: border-box;
}
.tgl-42__input {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0,0,0,0);
white-space: nowrap;
border: 0;
}
.tgl-42__track {
position: relative;
width: 56px;
height: 30px;
background: rgba(255,255,255,0.08);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.18);
border-radius: 999px;
transition: background 0.3s ease, border-color 0.3s ease;
box-sizing: border-box;
}
.tgl-42__thumb {
position: absolute;
top: 2px;
left: 2px;
width: 24px;
height: 24px;
border-radius: 50%;
background: rgba(255,255,255,0.85);
backdrop-filter: blur(6px);
-webkit-backdrop-filter: blur(6px);
box-shadow: inset 0 1px 0 rgba(255,255,255,0.6), 0 2px 6px rgba(0,0,0,0.18);
transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s ease;
}
.tgl-42__input:checked ~ .tgl-42__track {
background: rgba(124,108,255,0.32);
border-color: rgba(124,108,255,0.55);
}
.tgl-42__input:checked ~ .tgl-42__track .tgl-42__thumb {
/* Inner width: 56 - 2 = 54. Thumb 24. Max left to stay inside: 54-24-2 = 28. */
left: 28px;
background: rgba(255,255,255,0.95);
}
.tgl-42__input:focus-visible ~ .tgl-42__track {
outline: 2px solid rgba(255,255,255,0.7);
outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-42__track,
.tgl-42__thumb {
transition: none;
}
}
```
## JavaScript
```js
/* No JavaScript — a blurred 8%-white glass track over a gradient backdrop tints violet on :checked while a denser glass puck slides across. */
```How this works
The label itself carries a soft two-tone gradient backdrop — the crucial supporting layer, because backdrop-filter needs something non-flat behind it to refract. The 56×30 track is 8%-white glass with a 10px blur and hairline light border; checking it tints the pane toward violet at 32% alpha, keeping the material read.
The 24px thumb is denser glass (85% white, 6px blur) with an inset top highlight so it reads as a puck resting ON the pane. Slide math is annotated in-source: 54px inner − 24px thumb − 2px inset = left:28px.
Make it yours
- Recolor the backdrop gradient — the glass adapts automatically.
- Frost harder with blur(16px) + higher track alpha for a milkier pane.
- Add an @supports fallback with a solid track for old WebViews (see demo 21).
- Tint the checked state amber/green for semantic states.
Gotchas — read before shipping
- Never put a solid background on the track's parent chain between it and the gradient — it blanks the refraction.
- Keep checked-state tints ≤ ~35% alpha or the glass collapses into paint.
- Translucency erodes contrast: the focus ring here is 70% white minimum — don't soften it further.
Browser support
| Chrome | Safari | Firefox | Edge |
|---|---|---|---|
| 76+ | 9+ (-webkit-) | 103+ | 76+ |
backdrop-filter with -webkit- prefix; degrades to translucent flat fills where unsupported.