AI CSS Animations

Heart Like Checkbox

A heart that pops in filled when checked, with a shrinking ring and eight dots bursting outward, the like animation people know from social apps. It is a real checkbox, so it submits with a form.

Also known as: heart checkbox css, like button checkbox, favorite heart animation css.

HTML

<label class="chk-heart-like">
  <input type="checkbox">
  <span class="chk-heart-like-icon">
    <span class="chk-heart-like-ring"></span>
    <span class="chk-heart-like-dots"><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><i style="--i:5"></i><i style="--i:6"></i><i style="--i:7"></i></span>
    <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M12 20.3s-7.8-4.7-9.3-9.6C1.7 7.4 3.7 4.2 7 4.2c2.1 0 3.6 1.1 5 3 1.4-1.9 2.9-3 5-3 3.3 0 5.3 3.2 4.3 6.5-1.5 4.9-9.3 9.6-9.3 9.6z"/></svg>
  </span>
  <span class="chk-heart-like-text">Like</span>
</label>

CSS

.chk-heart-like {
  --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-heart-like input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 1;
}
.chk-heart-like-icon {
  position: relative;
  display: grid;
  place-items: center;
  width: 1.7em;
  height: 1.7em;
  border-radius: 50%;
}
.chk-heart-like-icon svg {
  position: relative;
  width: 1.4em;
  height: 1.4em;
  fill: transparent;
  stroke: color-mix(in srgb, currentColor 38%, transparent);
  stroke-width: 1.8;
  stroke-linejoin: round;
  transition: fill 0.2s ease, stroke 0.2s ease;
}
.chk-heart-like-ring {
  position: absolute;
  inset: 0;
  border: 0.85em solid var(--c);
  border-radius: 50%;
  opacity: 0;
  transform: scale(0);
}
.chk-heart-like-dots {
  position: absolute;
  inset: 0;
}
.chk-heart-like-dots i {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0.2em;
  height: 0.2em;
  margin: -0.1em;
  border-radius: 50%;
  background: var(--c);
  transform: rotate(calc(var(--i) * 45deg)) translateY(-0.6em) scale(0);
}
.chk-heart-like-dots i:nth-child(even) {
  background: color-mix(in srgb, var(--c) 55%, transparent);
}
.chk-heart-like-text {
  transition: color 0.2s ease;
}
.chk-heart-like:hover .chk-heart-like-icon svg {
  stroke: var(--c);
}
.chk-heart-like input:checked + .chk-heart-like-icon svg {
  fill: var(--c);
  stroke: var(--c);
  animation: chk-heart-like-beat 0.6s ease-out;
}
.chk-heart-like input:checked + .chk-heart-like-icon .chk-heart-like-ring {
  animation: chk-heart-like-ring 0.4s ease-out;
}
.chk-heart-like input:checked + .chk-heart-like-icon .chk-heart-like-dots i {
  animation: chk-heart-like-dot 0.65s cubic-bezier(0.2, 0.7, 0.3, 1) 0.12s both;
}
.chk-heart-like input:checked ~ .chk-heart-like-text {
  color: var(--c);
}
.chk-heart-like input:focus-visible + .chk-heart-like-icon {
  outline: 0.12em solid var(--c);
  outline-offset: 0.18em;
}
@keyframes chk-heart-like-beat {
  0%, 20% { transform: scale(0); }
  55% { transform: scale(1.25); }
  75% { transform: scale(0.92); }
  100% { transform: scale(1); }
}
@keyframes chk-heart-like-ring {
  0% { opacity: 1; transform: scale(0.2); border-width: 0.85em; }
  100% { opacity: 1; transform: scale(1.3); border-width: 0; }
}
@keyframes chk-heart-like-dot {
  0% { transform: rotate(calc(var(--i) * 45deg)) translateY(-0.5em) scale(0); }
  30% { transform: rotate(calc(var(--i) * 45deg)) translateY(-1em) scale(1.2); }
  100% { transform: rotate(calc(var(--i) * 45deg + 15deg)) translateY(-1.3em) scale(0); }
}

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