AI CSS Animations

Square Radio Button

Radios drawn as rounded squares in a two by two grid. The inner square spins in from a diamond with a springy overshoot. Still real radios, so arrow keys move the choice.

Also known as: css radio button square, square radio input css, boxed radio buttons.

HTML

<div class="rad-square-radio" role="radiogroup" aria-label="Time of day">
  <label><input type="radio" name="rad-square-radio" value="morning" checked>Morning</label>
  <label><input type="radio" name="rad-square-radio" value="noon">Noon</label>
  <label><input type="radio" name="rad-square-radio" value="evening">Evening</label>
  <label><input type="radio" name="rad-square-radio" value="night">Night</label>
</div>

CSS

.rad-square-radio {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: grid;
  grid-template-columns: repeat(2, auto);
  gap: 0.75em 1.8em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-square-radio label {
  display: flex;
  align-items: center;
  gap: 0.55em;
  cursor: pointer;
}
.rad-square-radio 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: 0.28em;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  transition: border-color 0.2s ease, transform 0.15s ease;
}
.rad-square-radio input::before {
  content: "";
  width: 0.58em;
  height: 0.58em;
  border-radius: 0.12em;
  background: var(--c);
  transform: scale(0) rotate(-135deg);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.rad-square-radio label:hover input:not(:checked) {
  border-color: color-mix(in srgb, var(--c) 60%, transparent);
}
.rad-square-radio input:checked {
  border-color: var(--c);
}
.rad-square-radio input:checked::before {
  transform: scale(1) rotate(0deg);
}
.rad-square-radio input:active {
  transform: scale(0.88);
}
.rad-square-radio input:focus-visible {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}

@media (prefers-reduced-motion: reduce) {
  .rad-square-radio, .rad-square-radio *, .rad-square-radio::before, .rad-square-radio::after, .rad-square-radio *::before, .rad-square-radio *::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="Time of day" class="grid grid-cols-2 gap-x-7 gap-y-3">
  <label class="flex cursor-pointer items-center gap-2">
    <input type="radio" name="time" value="morning" checked class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:-rotate-[135deg] before:scale-0 before:rounded-sm before:bg-blue-500 before:transition-transform before:duration-[400ms] before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:rotate-0 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Morning
  </label>
  <label class="flex cursor-pointer items-center gap-2">
    <input type="radio" name="time" value="noon" class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:-rotate-[135deg] before:scale-0 before:rounded-sm before:bg-blue-500 before:transition-transform before:duration-[400ms] before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:rotate-0 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Noon
  </label>
  <label class="flex cursor-pointer items-center gap-2">
    <input type="radio" name="time" value="evening" class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:-rotate-[135deg] before:scale-0 before:rounded-sm before:bg-blue-500 before:transition-transform before:duration-[400ms] before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:rotate-0 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Evening
  </label>
  <label class="flex cursor-pointer items-center gap-2">
    <input type="radio" name="time" value="night" class="grid h-5 w-5 cursor-pointer appearance-none place-content-center rounded border-2 border-gray-400 transition before:h-2.5 before:w-2.5 before:-rotate-[135deg] before:scale-0 before:rounded-sm before:bg-blue-500 before:transition-transform before:duration-[400ms] before:ease-[cubic-bezier(.34,1.56,.64,1)] before:content-[''] checked:border-blue-500 checked:before:rotate-0 checked:before:scale-100 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-500 active:scale-90">
    Night
  </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