AI CSS Animations

Star Favorite Checkbox

An outlined star that spins in a fifth of a turn, fills with the accent and throws five sparks from between its points. Unchecking just drains the fill, so the fanfare only plays on a new favorite.

Also known as: star checkbox css, favorite star toggle, star bookmark checkbox.

HTML

<label class="chk-star-favorite">
  <input type="checkbox">
  <span class="chk-star-favorite-icon">
    <span class="chk-star-favorite-sparks"><i style="--i:0"></i><i style="--i:1"></i><i style="--i:2"></i><i style="--i:3"></i><i style="--i:4"></i></span>
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 3l2.7 5.6 6.1.9-4.4 4.3 1 6.1L12 17l-5.4 2.9 1-6.1-4.4-4.3 6.1-.9z"/></svg>
  </span>
  <span class="chk-star-favorite-text">Add to favorites</span>
</label>

CSS

.chk-star-favorite {
  --c: var(--chk-color, #3b82f6);
  font-size: var(--chk-size, 24px);
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.6em;
  line-height: 1.3;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.chk-star-favorite input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.chk-star-favorite-icon {
  position: relative;
  display: grid;
  place-items: center;
  width: 1.6em;
  height: 1.6em;
  border-radius: 0.4em;
}
.chk-star-favorite-icon svg {
  width: 1.45em;
  height: 1.45em;
  fill: transparent;
  stroke: color-mix(in srgb, currentColor 38%, transparent);
  stroke-width: 1.7;
  stroke-linejoin: round;
  transition: fill 0.25s ease, stroke 0.25s ease;
}
.chk-star-favorite-sparks {
  position: absolute;
  inset: 0;
}
.chk-star-favorite-sparks i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0.12em;
  height: 0.32em;
  margin: -0.16em -0.06em;
  border-radius: 1em;
  background: var(--c);
  transform: rotate(calc(var(--i) * 72deg + 36deg)) translateY(-0.7em) scaleY(0);
}
.chk-star-favorite-text {
  transition: color 0.25s ease;
}
.chk-star-favorite input:checked ~ .chk-star-favorite-text {
  color: var(--c);
}
.chk-star-favorite:hover .chk-star-favorite-icon svg {
  stroke: var(--c);
}
.chk-star-favorite input:checked + .chk-star-favorite-icon svg {
  fill: var(--c);
  stroke: var(--c);
  animation: chk-star-favorite-spin 0.6s cubic-bezier(0.34, 1.4, 0.64, 1);
}
.chk-star-favorite input:checked + .chk-star-favorite-icon .chk-star-favorite-sparks i {
  animation: chk-star-favorite-spark 0.5s ease-out 0.3s both;
}
.chk-star-favorite input:focus-visible + .chk-star-favorite-icon {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}
@keyframes chk-star-favorite-spin {
  0% { transform: rotate(-72deg) scale(0.4); }
  100% { transform: rotate(0deg) scale(1); }
}
@keyframes chk-star-favorite-spark {
  0% { transform: rotate(calc(var(--i) * 72deg + 36deg)) translateY(-0.6em) scaleY(0); }
  40% { transform: rotate(calc(var(--i) * 72deg + 36deg)) translateY(-0.95em) scaleY(1); }
  100% { transform: rotate(calc(var(--i) * 72deg + 36deg)) translateY(-1.25em) scaleY(0); }
}

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