AI CSS Animations

Size Picker Radio Buttons

Square size chips for a product page. The chosen size fills with the accent and gives a small pop, and a sold out size is a disabled radio with a diagonal strike through it.

Also known as: size selector css, clothing size radio buttons, s m l xl buttons css.

HTML

<div class="rad-size-picker" role="radiogroup" aria-label="Size">
  <label><input type="radio" name="rad-size-picker" value="xs"><span>XS</span></label>
  <label><input type="radio" name="rad-size-picker" value="s"><span>S</span></label>
  <label><input type="radio" name="rad-size-picker" value="m" checked><span>M</span></label>
  <label><input type="radio" name="rad-size-picker" value="l"><span>L</span></label>
  <label><input type="radio" name="rad-size-picker" value="xl"><span>XL</span></label>
  <label><input type="radio" name="rad-size-picker" value="xxl" disabled><span>XXL</span></label>
</div>

CSS

.rad-size-picker {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: inline-flex;
  gap: 0.45em;
  font-weight: 600;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-size-picker label {
  position: relative;
  cursor: pointer;
}
.rad-size-picker input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.rad-size-picker input + span {
  display: grid;
  place-items: center;
  box-sizing: border-box;
  min-width: 2.5em;
  height: 2.5em;
  padding: 0 0.55em;
  border: 1px solid color-mix(in srgb, currentColor 22%, transparent);
  border-radius: 0.55em;
  font-size: 0.9em;
  transition: background-color 0.25s ease, border-color 0.25s ease, color 0.25s ease, transform 0.15s ease;
}
.rad-size-picker label:hover input:enabled:not(:checked) + span {
  border-color: var(--c);
  color: var(--c);
}
.rad-size-picker label:active input:enabled + span {
  transform: scale(0.92);
}
.rad-size-picker input:checked + span {
  border-color: var(--c);
  background: var(--c);
  color: #fff;
  animation: rad-size-picker-pop 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-size-picker input:disabled {
  cursor: not-allowed;
}
.rad-size-picker input:disabled + span {
  opacity: 0.4;
  background: linear-gradient(to top right, transparent calc(50% - 1px), currentColor calc(50% - 1px), currentColor calc(50% + 1px), transparent calc(50% + 1px));
}
.rad-size-picker input:focus-visible + span {
  outline: 0.12em solid var(--c);
  outline-offset: 0.15em;
}
@keyframes rad-size-picker-pop {
  0% { transform: scale(0.88); }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

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