AI CSS Animations

Tick and Cross Checkbox

Off shows a red cross and on shows a tick, so both answers are explicit. Checking spins the cross out while the tick spins in with a spring, and the box swaps from a red tint to your accent.

Also known as: checkbox with x css, yes no checkbox, cross checkbox css.

HTML

<label class="chk-tick-and-cross-checkbox">
  <input type="checkbox">
  <span class="chk-tick-and-cross-checkbox-box">
    <svg class="chk-tick-and-cross-checkbox-cross" viewBox="0 0 24 24" aria-hidden="true"><path d="M7.5 7.5l9 9M16.5 7.5l-9 9"/></svg>
    <svg class="chk-tick-and-cross-checkbox-tick" viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>
  </span>
  Share usage data
</label>

CSS

.chk-tick-and-cross-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;
}
.chk-tick-and-cross-checkbox input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.chk-tick-and-cross-checkbox-box {
  --no: color-mix(in srgb, #ef4444 78%, currentColor);
  box-sizing: border-box;
  display: grid;
  place-items: center;
  flex: none;
  width: 1.375em;
  height: 1.375em;
  border: 0.125em solid color-mix(in srgb, var(--no) 55%, transparent);
  border-radius: 0.3em;
  background: color-mix(in srgb, var(--no) 10%, transparent);
  transition: background-color 0.25s ease, border-color 0.25s ease;
}
.chk-tick-and-cross-checkbox-box svg {
  display: block;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  grid-area: 1 / 1;
  width: 0.9em;
  height: 0.9em;
  stroke-width: 3.2;
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s ease;
}
.chk-tick-and-cross-checkbox-cross {
  stroke: var(--no);
}
.chk-tick-and-cross-checkbox-tick {
  stroke: #fff;
  opacity: 0;
  transform: rotate(-90deg) scale(0.3);
}
.chk-tick-and-cross-checkbox input:checked + .chk-tick-and-cross-checkbox-box {
  background: var(--c);
  border-color: var(--c);
}
.chk-tick-and-cross-checkbox input:checked + .chk-tick-and-cross-checkbox-box .chk-tick-and-cross-checkbox-cross {
  opacity: 0;
  transform: rotate(90deg) scale(0.3);
}
.chk-tick-and-cross-checkbox input:checked + .chk-tick-and-cross-checkbox-box .chk-tick-and-cross-checkbox-tick {
  opacity: 1;
  transform: none;
}
.chk-tick-and-cross-checkbox input:focus-visible + .chk-tick-and-cross-checkbox-box {
  outline: 0.125em solid var(--c);
  outline-offset: 0.18em;
}

@media (prefers-reduced-motion: reduce) {
  .chk-tick-and-cross-checkbox, .chk-tick-and-cross-checkbox *, .chk-tick-and-cross-checkbox::before, .chk-tick-and-cross-checkbox::after, .chk-tick-and-cross-checkbox *::before, .chk-tick-and-cross-checkbox *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

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