AI CSS Animations

SVG Line Draw Animation

A success check draws itself: first the circle, then the tick. A pathLength of 1 on each shape means a dash of 1 always fits, so the same keyframes draw any path whatever its length.

Also known as: svg line drawing animation css, stroke-dashoffset animation, animated checkmark css.

HTML

<div class="kfa-svg-line-draw"><svg class="kfa-svg-line-draw-icon" viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="9.5" pathLength="1" transform="rotate(-90 12 12)" /><path d="M7.5 12.4l3 3 6-6.4" pathLength="1" /></svg></div>

CSS

.kfa-svg-line-draw {
  --c: var(--kfa-color, #3b82f6);
  font-size: var(--kfa-size, 16px);
  display: grid;
  place-items: center;
  width: 5.5em;
  height: 5.5em;
  border-radius: 1em;
  background: var(--c);
  color: #fff;
  font-family: system-ui, sans-serif;
  font-weight: 700;
  box-shadow: 0 0.6em 1.4em -0.6em color-mix(in srgb, var(--c) 70%, transparent);
}
.kfa-svg-line-draw-icon {
  width: 3.4em;
  height: 3.4em;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.kfa-svg-line-draw-icon > * {
  stroke-dasharray: 1 2;
  stroke-dashoffset: 1;
  animation: kfa-svg-line-draw-stroke 0.7s cubic-bezier(0.65, 0, 0.35, 1) forwards;
}
/* the tick starts as the circle closes, and lands faster */
.kfa-svg-line-draw-icon > path {
  animation-duration: 0.4s;
  animation-delay: 0.55s;
  animation-timing-function: cubic-bezier(0.33, 1, 0.68, 1);
}
@keyframes kfa-svg-line-draw-stroke {
  to { stroke-dashoffset: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .kfa-svg-line-draw, .kfa-svg-line-draw *, .kfa-svg-line-draw::before, .kfa-svg-line-draw::after, .kfa-svg-line-draw *::before, .kfa-svg-line-draw *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

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