AI CSS Animations

Poll Radio Buttons with Results

A one question poll. Before voting the answers are plain rows; picking one reveals the results as bars that grow in a stagger, with the percentages sliding in after them.

Also known as: css poll, voting radio buttons css, poll with progress bars css.

Tabs or spaces?

HTML

<div class="rad-poll-results" role="radiogroup" aria-label="Tabs or spaces?">
  <p class="rad-poll-results-q">Tabs or spaces?</p>
  <label style="--p: 0.58; --i: 0"><input type="radio" name="rad-poll-results" value="tabs"><span class="rad-poll-results-bar" aria-hidden="true"></span><span class="rad-poll-results-text">Tabs</span><span class="rad-poll-results-pct">58%</span></label>
  <label style="--p: 0.34; --i: 1"><input type="radio" name="rad-poll-results" value="spaces"><span class="rad-poll-results-bar" aria-hidden="true"></span><span class="rad-poll-results-text">Spaces</span><span class="rad-poll-results-pct">34%</span></label>
  <label style="--p: 0.08; --i: 2"><input type="radio" name="rad-poll-results" value="linter"><span class="rad-poll-results-bar" aria-hidden="true"></span><span class="rad-poll-results-text">Whatever the linter says</span><span class="rad-poll-results-pct">8%</span></label>
</div>

CSS

.rad-poll-results {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: grid;
  gap: 0.4em;
  width: 16em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-poll-results-q {
  margin: 0 0 0.15em;
  font-weight: 700;
}
.rad-poll-results label {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.45em 0.75em;
  overflow: hidden;
  border: 1px solid color-mix(in srgb, currentColor 16%, transparent);
  border-radius: 0.6em;
  cursor: pointer;
  transition: border-color 0.3s ease, background-color 0.3s ease;
}
.rad-poll-results:has(:checked) label {
  border-color: transparent;
  background: color-mix(in srgb, currentColor 4%, transparent);
}
.rad-poll-results label:hover {
  border-color: color-mix(in srgb, var(--c) 60%, transparent);
}
.rad-poll-results label:has(:focus-visible) {
  outline: 0.12em solid var(--c);
  outline-offset: 0.15em;
}
.rad-poll-results input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-poll-results-bar {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, currentColor 10%, transparent);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.8s cubic-bezier(0.22, 1, 0.36, 1) calc(var(--i) * 90ms), background-color 0.3s ease;
}
.rad-poll-results:has(:checked) .rad-poll-results-bar {
  transform: scaleX(var(--p));
}
.rad-poll-results label:has(:checked) .rad-poll-results-bar {
  background: color-mix(in srgb, var(--c) 30%, transparent);
}
.rad-poll-results-text {
  position: relative;
  flex: 1;
  font-size: 0.9em;
}
.rad-poll-results input:checked ~ .rad-poll-results-text {
  font-weight: 700;
}
.rad-poll-results input:checked ~ .rad-poll-results-text::after {
  content: " \2713";
  color: var(--c);
}
.rad-poll-results-pct {
  position: relative;
  font-size: 0.8em;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  opacity: 0;
  transform: translateX(0.6em);
  transition: opacity 0.3s ease, transform 0.4s ease;
  transition-delay: calc(var(--i) * 90ms + 0.25s);
}
.rad-poll-results:has(:checked) .rad-poll-results-pct {
  opacity: 1;
  transform: none;
}

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