A wide pill knob that carries its own label: NO while it rests on the left, then it slides right and the word rolls up to YES. Good for consent and preference questions in forms.
Also known as: yes no switch css, toggle switch with text in knob, yes no toggle button.
<label class="tgl-yes-no">
<input type="checkbox" role="switch" aria-label="Send me the newsletter">
<span class="tgl-yes-no-track" aria-hidden="true">
<span class="tgl-yes-no-knob">
<span class="tgl-yes-no-words"><span>NO</span><span>YES</span></span>
</span>
</span>
</label>.tgl-yes-no {
--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-yes-no input {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
margin: 0;
opacity: 0;
cursor: pointer;
z-index: 1;
}
.tgl-yes-no-track {
position: relative;
display: block;
width: 5em;
height: 2.2em;
border-radius: 1.1em;
background: color-mix(in srgb, currentColor 13%, transparent);
transition: background-color 0.3s ease;
}
.tgl-yes-no-knob {
position: absolute;
top: 0.25em;
left: 0.25em;
width: 2.6em;
height: 1.7em;
border-radius: 0.85em;
overflow: hidden;
background: #fff;
box-shadow: 0 0.1em 0.3em rgb(0 0 0 / 0.22);
transition: transform 0.4s cubic-bezier(0.4, 1.3, 0.5, 1);
}
.tgl-yes-no-words {
display: grid;
grid-auto-rows: 1.7em;
transition: transform 0.4s cubic-bezier(0.4, 1.3, 0.5, 1) 0.05s;
}
.tgl-yes-no-words span {
display: grid;
place-items: center;
font-size: 0.68em;
letter-spacing: 0.08em;
color: #6b7280;
}
.tgl-yes-no-words span + span { color: var(--c); }
.tgl-yes-no input:checked + .tgl-yes-no-track { background: var(--c); }
.tgl-yes-no input:checked + .tgl-yes-no-track .tgl-yes-no-knob { transform: translateX(1.9em); }
.tgl-yes-no input:checked + .tgl-yes-no-track .tgl-yes-no-words { transform: translateY(-1.7em); }
.tgl-yes-no input:focus-visible + .tgl-yes-no-track {
outline: 2px solid var(--c);
outline-offset: 2px;
}
@media (prefers-reduced-motion: reduce) {
.tgl-yes-no, .tgl-yes-no *, .tgl-yes-no::before, .tgl-yes-no::after, .tgl-yes-no *::before, .tgl-yes-no *::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.