AI CSS Animations

Square Radio with Diagonal Fill

Square radios in an export menu. The accent wipes across the box corner to corner with an animated clip-path, then a small white square springs into the middle. Native inputs, no extra markup.

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

HTML

<div class="rad-square-radio-diagonal" role="radiogroup" aria-label="Export format">
  <label><input type="radio" name="rad-square-radio-diagonal" value="png" checked><span class="rad-square-radio-diagonal-name">PNG</span><span class="rad-square-radio-diagonal-meta">2.4 MB</span></label>
  <label><input type="radio" name="rad-square-radio-diagonal" value="svg"><span class="rad-square-radio-diagonal-name">SVG</span><span class="rad-square-radio-diagonal-meta">186 KB</span></label>
  <label><input type="radio" name="rad-square-radio-diagonal" value="pdf"><span class="rad-square-radio-diagonal-name">PDF</span><span class="rad-square-radio-diagonal-meta">1.1 MB</span></label>
</div>

CSS

.rad-square-radio-diagonal {
  --c: var(--rad-color, #3b82f6);
  font-size: var(--rad-size, 18px);
  display: grid;
  gap: 0.25em;
  width: 11em;
  font-family: system-ui, sans-serif;
  line-height: 1.2;
}
.rad-square-radio-diagonal label {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.7em;
  padding: 0.45em 0.55em;
  border-radius: 0.45em;
  cursor: pointer;
  transition: background-color 0.2s ease;
}
.rad-square-radio-diagonal label:hover {
  background: color-mix(in srgb, currentColor 5%, transparent);
}
.rad-square-radio-diagonal input {
  -webkit-appearance: none;
  appearance: none;
  display: grid;
  place-items: center;
  box-sizing: border-box;
  width: 1.2em;
  height: 1.2em;
  margin: 0;
  border: 0.11em solid color-mix(in srgb, currentColor 35%, transparent);
  border-radius: 0.2em;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
  transition: border-color 0.2s ease, transform 0.15s ease;
}
.rad-square-radio-diagonal input::before,
.rad-square-radio-diagonal input::after {
  content: "";
  grid-area: 1 / 1;
}
.rad-square-radio-diagonal input::before {
  width: 100%;
  height: 100%;
  background: var(--c);
  clip-path: polygon(0 0, 0 0, 0 0);
  transition: clip-path 0.22s cubic-bezier(0.65, 0, 0.35, 1);
}
.rad-square-radio-diagonal input::after {
  width: 0.38em;
  height: 0.38em;
  border-radius: 0.06em;
  background: #fff;
  transform: scale(0) rotate(45deg);
  transition: transform 0.2s ease;
}
.rad-square-radio-diagonal label:hover input:not(:checked) {
  border-color: color-mix(in srgb, var(--c) 60%, transparent);
}
.rad-square-radio-diagonal input:checked {
  border-color: var(--c);
}
.rad-square-radio-diagonal input:checked::before {
  clip-path: polygon(0 0, 200% 0, 0 200%);
  transition-duration: 0.35s;
}
.rad-square-radio-diagonal input:checked::after {
  transform: scale(1) rotate(0deg);
  transition: transform 0.4s cubic-bezier(0.34, 1.7, 0.64, 1) 0.2s;
}
.rad-square-radio-diagonal input:active {
  transform: scale(0.88);
}
.rad-square-radio-diagonal input:focus-visible {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}
.rad-square-radio-diagonal-name {
  font-size: 0.9em;
  font-weight: 600;
  letter-spacing: 0.02em;
}
.rad-square-radio-diagonal-meta {
  font: 0.75em ui-monospace, SFMono-Regular, Menlo, monospace;
  color: color-mix(in srgb, currentColor 50%, transparent);
  transition: color 0.2s ease;
}
.rad-square-radio-diagonal label:has(:checked) .rad-square-radio-diagonal-meta {
  color: var(--c);
}
@media (prefers-reduced-motion: reduce) {
  .rad-square-radio-diagonal *,
  .rad-square-radio-diagonal *::before,
  .rad-square-radio-diagonal *::after {
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

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