AI CSS Animations

Tailwind CSS Checkbox

The Tailwind pattern: an appearance-none input as the box and an SVG sibling that rises and fades in with peer-checked. The CSS tab is the same checkbox written out, a hairline border and all.

Also known as: tailwind checkbox, tailwind peer-checked checkbox, tailwind custom checkbox.

HTML

<label class="chk-tailwind-checkbox">
  <span class="chk-tailwind-checkbox-box">
    <input type="checkbox">
    <svg viewBox="0 0 20 20" aria-hidden="true"><path d="M5 10.5l3.2 3.2L15 6.5"/></svg>
  </span>
  Email me receipts
</label>

CSS

.chk-tailwind-checkbox {
  --c: var(--chk-color, #3b82f6);
  font-size: var(--chk-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.6em;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  line-height: 1.3;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  gap: 0.625em;}
.chk-tailwind-checkbox-box {
  display: grid;
  place-items: center;
  flex: none;
}
.chk-tailwind-checkbox input {
  appearance: none;
  -webkit-appearance: none;
  box-sizing: border-box;
  grid-area: 1 / 1;
  width: 1.25em;
  height: 1.25em;
  margin: 0;
  font: inherit;
  background: transparent;
  border: 1px solid color-mix(in srgb, currentColor 35%, transparent);
  border-radius: 0.25em;
  box-shadow: 0 1px 2px color-mix(in srgb, currentColor 10%, transparent);
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}
.chk-tailwind-checkbox:hover input {
  border-color: color-mix(in srgb, currentColor 60%, transparent);
}
.chk-tailwind-checkbox input:checked {
  background: var(--c);
  border-color: var(--c);
}
.chk-tailwind-checkbox:hover input:checked {
  background: color-mix(in srgb, var(--c) 85%, #000);
}
.chk-tailwind-checkbox svg {
  display: block;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  grid-area: 1 / 1;
  width: 0.875em;
  height: 0.875em;
  stroke: #fff;
  stroke-width: 2.4;
  pointer-events: none;
  opacity: 0;
  transform: translateY(0.125em) scale(0.75);
  transition: opacity 0.2s ease-out, transform 0.2s ease-out;
}
.chk-tailwind-checkbox input:checked + svg {
  opacity: 1;
  transform: none;
}
.chk-tailwind-checkbox input:focus-visible {
  outline: 0.125em solid var(--c);
  outline-offset: 0.18em;
}

@media (prefers-reduced-motion: reduce) {
  .chk-tailwind-checkbox, .chk-tailwind-checkbox *, .chk-tailwind-checkbox::before, .chk-tailwind-checkbox::after, .chk-tailwind-checkbox *::before, .chk-tailwind-checkbox *::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.

<label class="group inline-flex cursor-pointer items-center gap-2.5">
  <span class="relative grid place-items-center">
    <input type="checkbox" class="peer h-5 w-5 cursor-pointer appearance-none rounded border border-gray-300 shadow-sm transition-colors duration-200 checked:border-blue-600 checked:bg-blue-600 group-hover:border-gray-400 checked:group-hover:bg-blue-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600">
    <svg viewBox="0 0 20 20" aria-hidden="true" class="pointer-events-none absolute h-3.5 w-3.5 translate-y-0.5 scale-75 fill-none stroke-white stroke-[2.4] opacity-0 transition duration-200 ease-out [stroke-linecap:round] [stroke-linejoin:round] peer-checked:translate-y-0 peer-checked:scale-100 peer-checked:opacity-100"><path d="M5 10.5l3.2 3.2L15 6.5"/></svg>
  </span>
  Email me receipts
</label>

Customizing this snippet

  • Color: set --chk-color on the element or a parent. Currently previewing #3b82f6.
  • Size: set --chk-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 24px.
  • 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 checkboxes