The iPhone settings switch: a pill track that fills with your accent and a white knob that glides across. Hold it down and the knob stretches toward the far side, just like iOS.
Also known as: ios toggle button css, iphone toggle switch css, apple settings switch.
<label class="tgl-ios"> <input type="checkbox" role="switch" aria-label="Wi-Fi" checked> <span class="tgl-ios-track" aria-hidden="true"></span> </label>
.tgl-ios {
--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;
}
.tgl-ios input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-ios-track {
position: relative;
display: block;
width: 3.2em;
height: 1.95em;
border-radius: 1em;
background: color-mix(in srgb, currentColor 13%, transparent);
transition: background-color 0.25s ease;
}
.tgl-ios-track::before {
content: "";
position: absolute;
top: 0.125em;
left: 0.125em;
width: 1.7em;
height: 1.7em;
border-radius: 0.85em;
background: #fff;
box-shadow: 0 0.15em 0.4em rgb(0 0 0 / 0.18), 0 0.05em 0.08em rgb(0 0 0 / 0.1);
transition: transform 0.3s cubic-bezier(0.3, 0.7, 0.3, 1), width 0.3s cubic-bezier(0.3, 0.7, 0.3, 1);
}
.tgl-ios input:checked + .tgl-ios-track { background: var(--c); }
.tgl-ios input:checked + .tgl-ios-track::before { transform: translateX(1.25em); }
/* pressed: the knob stretches, anchored on the side it rests on */
.tgl-ios:active .tgl-ios-track::before { width: 2.1em; }
.tgl-ios:active input:checked + .tgl-ios-track::before { transform: translateX(0.85em); }
.tgl-ios input:focus-visible + .tgl-ios-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-ios, .tgl-ios *, .tgl-ios::before, .tgl-ios::after, .tgl-ios *::before, .tgl-ios *::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.