AI CSS Animations

Yes No Radio Buttons

A yes or no answer as a split pill. The chosen half widens with a springy flex-grow, fills with its own color, green-blue for yes or red for no, and its icon pops.

Also known as: yes no toggle css, yes no buttons css, binary choice radio buttons.

Would you recommend us?

HTML

<div class="rad-yes-no-radio" role="radiogroup" aria-label="Would you recommend us?">
  <p class="rad-yes-no-radio-q">Would you recommend us?</p>
  <div class="rad-yes-no-radio-pill">
    <label class="rad-yes-no-radio-yes"><input type="radio" name="rad-yes-no-radio" value="yes"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>Yes</label>
    <label class="rad-yes-no-radio-no"><input type="radio" name="rad-yes-no-radio" value="no"><svg viewBox="0 0 24 24" aria-hidden="true"><path d="M6.5 6.5l11 11M17.5 6.5l-11 11"/></svg>No</label>
  </div>
</div>

CSS

.rad-yes-no-radio {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: grid;
  justify-items: center;
  gap: 0.65em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-yes-no-radio-q {
  margin: 0;
  font-weight: 600;
}
.rad-yes-no-radio-pill {
  display: flex;
  gap: 0.3em;
  box-sizing: border-box;
  width: 13em;
  padding: 0.3em;
  border-radius: 99em;
  background: color-mix(in srgb, currentColor 8%, transparent);
}
.rad-yes-no-radio label {
  position: relative;
  display: flex;
  flex: 1 1 0;
  align-items: center;
  justify-content: center;
  gap: 0.4em;
  padding: 0.6em 0;
  border-radius: 99em;
  font-weight: 700;
  color: color-mix(in srgb, currentColor 60%, transparent);
  cursor: pointer;
  transition: flex-grow 0.5s cubic-bezier(0.34, 1.3, 0.64, 1), background-color 0.3s ease, color 0.3s ease, box-shadow 0.3s ease;
}
.rad-yes-no-radio .rad-yes-no-radio-yes {
  --tone: var(--c);
}
.rad-yes-no-radio .rad-yes-no-radio-no {
  --tone: #e11d48;
}
.rad-yes-no-radio label:hover {
  color: var(--tone);
}
.rad-yes-no-radio label:has(:checked) {
  flex-grow: 2.2;
  background: var(--tone);
  color: #fff;
  box-shadow: 0 0.3em 0.8em -0.35em var(--tone);
}
.rad-yes-no-radio label:has(:focus-visible) {
  outline: 0.12em solid var(--tone);
  outline-offset: 0.2em;
}
.rad-yes-no-radio input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-yes-no-radio svg {
  width: 1em;
  height: 1em;
  fill: none;
  stroke: currentColor;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.6;
  transform: scale(0.8);
  transition: transform 0.45s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s ease;
}
.rad-yes-no-radio input:checked + svg {
  opacity: 1;
  transform: scale(1.15);
}

@media (prefers-reduced-motion: reduce) {
  .rad-yes-no-radio, .rad-yes-no-radio *, .rad-yes-no-radio::before, .rad-yes-no-radio::after, .rad-yes-no-radio *::before, .rad-yes-no-radio *::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