AI CSS Animations

Yes No Radio Switch

Two radios share one track. Unanswered, a small gray thumb waits in the middle; picking No or Yes springs it to that side, where it fills with its color and draws a check or cross.

Also known as: yes no radio button, radio button yes or no, yes no switch three state, unanswered yes no toggle.

HTML

<div class="rad-yes-no-switch" role="radiogroup" aria-label="Require two factor sign in?">
  <span class="rad-yes-no-switch-q" aria-hidden="true">Require two factor sign in?</span>
  <div class="rad-yes-no-switch-row">
    <span class="rad-yes-no-switch-track" aria-hidden="true">
      <span class="rad-yes-no-switch-thumb">
        <svg class="rad-yes-no-switch-tick" viewBox="0 0 24 24"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>
        <svg class="rad-yes-no-switch-cross" viewBox="0 0 24 24"><path d="M6.5 6.5l11 11M17.5 6.5l-11 11"/></svg>
      </span>
    </span>
    <label class="rad-yes-no-switch-no"><input type="radio" name="rad-yes-no-switch" value="no"><span>No</span></label>
    <label class="rad-yes-no-switch-yes"><input type="radio" name="rad-yes-no-switch" value="yes"><span>Yes</span></label>
  </div>
</div>

CSS

.rad-yes-no-switch {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: inline-grid;
  justify-items: center;
  gap: 0.75em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-yes-no-switch-q {
  font-weight: 600;
}
.rad-yes-no-switch-row {
  position: relative;
  display: grid;
  grid-template-columns: 2.2em 2.5em 2.5em 2.2em;
  align-items: center;
}
.rad-yes-no-switch-track {
  grid-column: 2 / 4;
  grid-row: 1;
  position: relative;
  height: 2.1em;
  border-radius: 99em;
  background: color-mix(in srgb, currentColor 8%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, currentColor 8%, transparent);
  transition: background-color 0.35s ease;
}
.rad-yes-no-switch-thumb {
  position: absolute;
  top: 0.25em;
  left: calc(50% - 0.8em);
  display: grid;
  place-items: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 50%;
  background: color-mix(in srgb, currentColor 30%, transparent);
  transform: scale(0.5);
  transition: transform 0.55s cubic-bezier(0.34, 1.45, 0.5, 1), background-color 0.3s ease, box-shadow 0.3s ease;
}
.rad-yes-no-switch-thumb svg {
  grid-area: 1 / 1;
  width: 0.95em;
  height: 0.95em;
  fill: none;
  stroke: #fff;
  stroke-width: 3.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0;
  transform: scale(0.3) rotate(-45deg);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
}
.rad-yes-no-switch label {
  position: relative;
  z-index: 1;
  grid-row: 1;
  display: flex;
  align-items: center;
  height: 2.1em;
  font-weight: 700;
  color: color-mix(in srgb, currentColor 45%, transparent);
  cursor: pointer;
  transition: color 0.25s ease;
}
.rad-yes-no-switch-no {
  --tone: #f43f5e;
  grid-column: 1 / 3;
  justify-content: flex-start;
}
.rad-yes-no-switch-yes {
  --tone: var(--c);
  grid-column: 3 / 5;
  justify-content: flex-end;
}
.rad-yes-no-switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-yes-no-switch label:hover,
.rad-yes-no-switch label:has(:checked) {
  color: var(--tone);
}
/* unanswered: the thumb leans toward the side you hover */
.rad-yes-no-switch-row:not(:has(:checked)):has(.rad-yes-no-switch-yes:hover) .rad-yes-no-switch-thumb {
  transform: translateX(0.4em) scale(0.62);
}
.rad-yes-no-switch-row:not(:has(:checked)):has(.rad-yes-no-switch-no:hover) .rad-yes-no-switch-thumb {
  transform: translateX(-0.4em) scale(0.62);
}
.rad-yes-no-switch-row:has(.rad-yes-no-switch-yes :checked) .rad-yes-no-switch-track {
  background: color-mix(in srgb, var(--c) 16%, transparent);
}
.rad-yes-no-switch-row:has(.rad-yes-no-switch-no :checked) .rad-yes-no-switch-track {
  background: color-mix(in srgb, #f43f5e 14%, transparent);
}
.rad-yes-no-switch-row:has(.rad-yes-no-switch-yes :checked) .rad-yes-no-switch-thumb {
  background: var(--c);
  box-shadow: 0 0.25em 0.6em -0.2em var(--c);
  transform: translateX(1.4em);
}
.rad-yes-no-switch-row:has(.rad-yes-no-switch-no :checked) .rad-yes-no-switch-thumb {
  background: #f43f5e;
  box-shadow: 0 0.25em 0.6em -0.2em #f43f5e;
  transform: translateX(-1.4em);
}
.rad-yes-no-switch-row:has(.rad-yes-no-switch-yes :checked) .rad-yes-no-switch-tick,
.rad-yes-no-switch-row:has(.rad-yes-no-switch-no :checked) .rad-yes-no-switch-cross {
  opacity: 1;
  transform: none;
  transition-delay: 0.12s;
}
.rad-yes-no-switch-row:has(:focus-visible) .rad-yes-no-switch-track {
  outline: 0.12em solid var(--c);
  outline-offset: 0.2em;
}
@media (prefers-reduced-motion: reduce) {
  .rad-yes-no-switch *,
  .rad-yes-no-switch *::before,
  .rad-yes-no-switch *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  .rad-yes-no-switch, .rad-yes-no-switch *, .rad-yes-no-switch::before, .rad-yes-no-switch::after, .rad-yes-no-switch *::before, .rad-yes-no-switch *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --rad-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --rad-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 18px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar radio buttons