AI CSS Animations

Custom Radio Button

The go-to custom radio: a native input with appearance: none, a soft ring and a dot that springs in with a little overshoot. One input and one ::before per option.

Also known as: custom radio button css, css radio button style, css radio group horizontal.

HTML

<div class="rad-custom-radio-button" role="radiogroup" aria-label="Contact method">
  <label><input type="radio" name="rad-custom-radio-button" value="email" checked>Email</label>
  <label><input type="radio" name="rad-custom-radio-button" value="phone">Phone</label>
  <label><input type="radio" name="rad-custom-radio-button" value="text">Text</label>
</div>

CSS

.rad-custom-radio-button {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.6em 1.4em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-custom-radio-button label {
  display: inline-flex;
  align-items: center;
  gap: 0.55em;
  cursor: pointer;
}
.rad-custom-radio-button input {
  -webkit-appearance: none;
  appearance: none;
  display: grid;
  place-content: center;
  width: 1.2em;
  height: 1.2em;
  margin: 0;
  border: 0.12em solid color-mix(in srgb, currentColor 35%, transparent);
  border-radius: 50%;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  transition: border-color 0.2s ease, transform 0.15s ease;
}
.rad-custom-radio-button input::before {
  content: "";
  width: 0.6em;
  height: 0.6em;
  border-radius: 50%;
  background: var(--c);
  transform: scale(0);
  transition: transform 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-custom-radio-button label:hover input:not(:checked) {
  border-color: color-mix(in srgb, var(--c) 60%, transparent);
}
.rad-custom-radio-button input:checked {
  border-color: var(--c);
}
.rad-custom-radio-button input:checked::before {
  transform: scale(1);
}
.rad-custom-radio-button input:active {
  transform: scale(0.88);
}
.rad-custom-radio-button input:focus-visible {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}

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

Tailwind CSS version

The same effect using Tailwind utility classes. No custom CSS or config needed.

<div role="radiogroup" aria-label="Contact method" class="inline-flex gap-6">
  <label class="inline-flex cursor-pointer items-center gap-2">
    <input type="radio" name="contact" value="email" checked class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded-full border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:scale-0 before:rounded-full before:bg-blue-500 before:transition-transform before:duration-300 before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Email
  </label>
  <label class="inline-flex cursor-pointer items-center gap-2">
    <input type="radio" name="contact" value="phone" class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded-full border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:scale-0 before:rounded-full before:bg-blue-500 before:transition-transform before:duration-300 before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Phone
  </label>
  <label class="inline-flex cursor-pointer items-center gap-2">
    <input type="radio" name="contact" value="text" class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded-full border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:scale-0 before:rounded-full before:bg-blue-500 before:transition-transform before:duration-300 before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Text
  </label>
</div>

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