The knob is a coin with OFF on one face and ON on the other: it rolls across the track and flips over in 3D on the way, landing with a small wobble. Uses preserve-3d and backface-visibility.
Also known as: 3d flip toggle css, coin flip switch, flip card toggle switch.
<label class="tgl-flip">
<input type="checkbox" role="switch" aria-label="Public profile">
<span class="tgl-flip-track" aria-hidden="true">
<span class="tgl-flip-coin">
<span class="tgl-flip-off">OFF</span>
<span class="tgl-flip-on">ON</span>
</span>
</span>
</label>.tgl-flip {
--c: var(--tgl-color, #3b82f6);
font-size: var(--tgl-size, 24px);
position: relative;
display: inline-block;
vertical-align: middle;
cursor: pointer;
-webkit-tap-highlight-color: transparent;
font-family: system-ui, sans-serif;
font-weight: 800;
}
.tgl-flip input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-flip-track {
position: relative;
display: block;
width: 4.6em;
height: 2.4em;
border-radius: 1.2em;
background: color-mix(in srgb, currentColor 13%, transparent);
perspective: 10em;
transition: background-color 0.35s ease;
}
.tgl-flip-coin {
position: absolute;
top: 0.2em;
left: 0.2em;
width: 2em;
height: 2em;
transform-style: preserve-3d;
transition: transform 0.6s cubic-bezier(0.45, 1.25, 0.5, 1);
}
.tgl-flip-off,
.tgl-flip-on {
position: absolute;
inset: 0;
display: grid;
place-items: center;
border-radius: 50%;
background: #fff;
box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.2);
font-size: 0.56em;
letter-spacing: 0.04em;
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
}
.tgl-flip-off { color: #6b7280; }
.tgl-flip-on {
color: var(--c);
transform: rotateY(180deg);
}
.tgl-flip input:checked + .tgl-flip-track { background: var(--c); }
.tgl-flip input:checked + .tgl-flip-track .tgl-flip-coin { transform: translateX(2.2em) rotateY(180deg); }
.tgl-flip input:focus-visible + .tgl-flip-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-flip, .tgl-flip *, .tgl-flip::before, .tgl-flip::after, .tgl-flip *::before, .tgl-flip *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}--tgl-color on the element or a parent.
Currently previewing #3b82f6.--tgl-size. Every other dimension is in
em, so the whole thing scales together. Currently previewing 24px.transition or animation
shorthand. Staggered delays are relative, so they keep their rhythm.