SVG Path Animation with CSS and Motion: Draw, Move, Morph

Animate SVG paths with CSS stroke-dasharray, offset-path and SMIL, or with Motion in React. Tested in three browser engines, with real icon pack counts.

Amit Yadav
14 min read
On this page

To animate an SVG path with CSS, set pathLength="1" on the path, then animate stroke-dashoffset from 1 to 0 to make the line draw itself. Use offset-path to move something along a curve, SMIL <animate> to morph one shape into another, and Motion only when you are in React and need springs or gestures.

We ran each method in Chromium 141, Firefox 142 and WebKit 26.0 on 2026-11-05. WebKit here is the Playwright build, the engine behind Safari, not Safari on a device. Browser version numbers come from MDN's compatibility data.

Key takeaways

  • Draw: use pathLength=1 with a dash pattern of 1 2. The more common pattern of 1 leaves a dot in Chromium when the line has round caps.
  • Icons: only 5% of Lucide's stroke icons are one shape with one subpath. The rest need each part drawn separately. Filled packs have no stroke to draw.
  • Morph: both shapes need the same path commands. Only 1 of 8 common icon pairs matched, so animate the parts instead.
  • Safari: no CSS d property. SMIL animate on d works there if the commands match.
  • Reduced motion: MotionConfig does not stop a pathLength draw and SMIL ignores the setting. Handle both yourself.

Three looping SVG animations: a heart drawing itself, a dot moving along a curve, and a menu icon turning into a close icon

That demo is one SVG file in an <img> tag, with no JavaScript. It stops on the finished frame if your system asks for reduced motion.

Pick a method

You wantUseJS addedSafari
A line that draws itselfstroke-dashoffset0 KBYes
A dot moving along a curve<animateMotion> or offset-path0 KBYes, offset-path from 16
Shape A turning into shape BSMIL <animate> on d0 KBYes
Hover and click feedbackCSS transform0 KBYes
Springs and gestures in ReactMotion33 to 48 KBYes

Draw a line with stroke-dashoffset

The trick is one dash as long as the whole line, pushed off the line by its own length. Animate the push back to 0 and the line appears from its start.

Older tutorials measure the line with getTotalLength() and paste the number into the CSS. You can skip that: pathLength="1" tells the browser to treat the line as 1 unit long, whatever its real size. A half-way offset painted exactly half the line in all three engines, and it works on <circle> too.

<svg viewBox="0 0 24 24" width="48" height="48" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round">
  <path class="draw" pathLength="1" d="M20 6L9 17l-5-5" />
</svg>

<style>
  .draw {
    stroke-dasharray: 1 2;
    stroke-dashoffset: 1;
    animation: draw 0.6s ease-out forwards;
  }
  @keyframes draw {
    to { stroke-dashoffset: 0; }
  }
</style>

pathLength is an attribute, so it goes in the markup. CSS cannot set it.

Why 1 2 and not 1. Most examples write stroke-dasharray: 1, which means dash 1, gap 1. With round caps, Chromium paints a dot before the animation starts. A gap of 2 pushes the next dash past the end of the line, and the dot is gone. We counted the dark pixels on the undrawn line, which should be 0:

Pattern at the startChromiumFirefoxWebKit
pathLength=1, dash 120900
pathLength=100, dash 10020900
Measured length, dash 92000
pathLength=1, dash 1 2000

At the end of the animation, 1 2 paints the full line in all three.

Which icons can draw themselves

Line drawing needs a stroke, and most tutorials show one simple path. Real icons are not like that. We counted the published packs on 2026-11-05:

PackIconsStroke iconsOne shape, one subpath
Lucide2,1002,085109 (5%)
Tabler6,3055,166330 (6%)
Heroicons1,28832422 (7%)
Phosphor9,16100
Material Symbols16,47500
Remix Icon3,22900

Phosphor, Material Symbols and Remix draw their lines as filled outlines, so stroke-dashoffset has nothing to work on.

Several shapes. Give each shape its own pathLength and a delay, so they draw one after another:

.draw { animation-fill-mode: both; }
.draw:nth-child(2) { animation-delay: 0.3s; }

Several subpaths in one d. A d with more than one M is the trap. We drew M10 20 H90 M10 70 H50 to the half-way point: the long line reached 75% and the short line 100%. The dash restarts at each M, so the parts draw at the same time, and short ones finish first. To draw them in order, split the d into separate paths at each M. A relative m needs converting to absolute first.

Filled icons. Keep pathLength="1" on the path, trace the outline with a thin stroke, then fade the fill in:

.outline {
  fill: currentColor;
  fill-opacity: 0;
  stroke: currentColor;
  stroke-width: 0.5;
  stroke-dasharray: 1 2;
  stroke-dashoffset: 1;
  animation: draw 0.8s ease-out forwards, fill 0.3s 0.7s forwards;
}
@keyframes fill {
  to { fill-opacity: 1; }
}

Move something along a path

Both of these landed on the same point in all three engines, and offset-path uses the SVG's own units when the SVG is scaled.

.dot {
  offset-path: path("M2 18 Q12 -2 22 18");
  animation: along 2s linear infinite;
}
@keyframes along {
  to { offset-distance: 100%; }
}
<circle r="2">
  <animateMotion dur="2s" repeatCount="indefinite" path="M2 18 Q12 -2 22 18" />
</circle>

They differ in one way: offset-path turns the element to follow the curve by default, and <animateMotion> does not. A 10 by 2 bar stayed 10 by 2 under <animateMotion>, and tilted under offset-path. Use offset-rotate: 0deg or rotate="auto" to get the other behaviour.

Support: offset-distance needs Safari 16, Chrome 55 or Firefox 72. <animateMotion> has worked since Safari 6. MDN lists SMIL as widely available, so ignore older posts that call it deprecated.

Morph one shape into another

A browser morphs a path by moving each number in d to its partner in the other shape. So both shapes need the same commands in the same order. We animated a shape's width from 80 to 40 and read it half-way, where a working morph shows 60:

Second shape usesCSS d, Chromium and FirefoxSMIL d, WebKit
Same commandsMorphsMorphs
Relative l for LMorphsMorphs
H and V for LJumps to the endPaints nothing
C for LJumps to the endPaints nothing
Fewer pointsJumps to the endMorphs, do not rely on it

SMIL behaved the same as CSS d in Chromium and Firefox. CSS d does not work in Safari: the shape stays where it started. So SMIL is the zero-JS way that covers all three:

<path d="M4 4 L20 12 L20 12 L4 20 Z">
  <animate attributeName="d" begin="click" dur="0.3s" fill="freeze"
    to="M4 4 L20 4 L20 20 L4 20 Z" />
</path>

The first shape repeats a point so that the triangle and the square both have four. Watch your build step: optimizers such as our SVG optimizer can rewrite a straight L as H or V, which breaks the pair.

Icon pairs rarely match. We compared 8 pairs you would want to morph (menu to X, play to pause, plus to minus, sun to moon, chevron up to down, check to X, heart to star, eye to eye-off) in both Lucide and Tabler. Only chevron up to down matched, and a rotate does that job anyway. Menu in Lucide is three M h lines, and X is two diagonal lines.

What to do instead. Animate the parts. The menu icon in the demo above is three lines with transforms. Its middle line fades out, and that works in every browser:

.top, .mid, .bottom {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.2s, opacity 0.2s;
}
.open .top { transform: translateY(6px) rotate(45deg); }
.open .mid { opacity: 0; }
.open .bottom { transform: translateY(-6px) rotate(-45deg); }

For two unrelated shapes, Flubber adds the missing points for you. It adds 18.6 KB gzipped, and its last npm change was June 2022.

Hover, click and keyframes

Most SVG transforms go wrong because of the origin. We scaled a 20-unit square at (60, 60) to 2x inside a 100-unit viewBox, and read where its corner ended up:

CSSCorner moved toResult
Nothing(120, 120)Flies away from the top-left
transform-origin: center(70, 70)Scales around the SVG's center
Add transform-box: fill-box(50, 50)Scales around itself
.icon-btn svg {
  transform-box: fill-box;
  transform-origin: center;
  transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.icon-btn:hover svg { transform: scale(1.15); }
.icon-btn:active svg { transform: scale(0.9); }

.spinner { animation: spin 1s linear infinite; }
@keyframes spin {
  to { transform: rotate(360deg); }
}

Keep hover feedback between 100 and 300 ms. Animate transform and opacity where you can, because the browser can run those without repainting. Color works the same way as any other property: see how to change an SVG icon's color with CSS.

Inline SVG or an img tag

An SVG in <img> keeps its own animations, which makes it good for a self-contained demo or loader. The page cannot reach inside it.

Inline <svg><img src="x.svg">
CSS animation in the fileRunsRuns
SMIL in the fileRunsRuns
<script> in the fileRunsDoes not run
Page CSS, hover on partsWorksDoes not reach

To get an icon inline as a component, use the SVG to component tool or see how to use SVG icons in React.

Motion in React

Motion is the new name for Framer Motion. Install motion and import from motion/react. We tested 14.9.1 with React 19.3.1. It takes pathLength from 0 to 1 and writes the attributes for you:

import { motion } from 'motion/react';

export function Check() {
  return (
    <svg viewBox="0 0 24 24" width="48" height="48" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
      <motion.path
        d="M20 6L9 17l-5-5"
        initial={{ pathLength: 0, opacity: 0 }}
        animate={{ pathLength: 1, opacity: 1 }}
        transition={{ duration: 0.5, opacity: { duration: 0.01 } }}
      />
    </svg>
  );
}

Keep the opacity. At pathLength: 0, Motion writes a dash of 0 1, and with round caps that painted a dot in all three engines.

To draw several parts in turn, use variants with staggerChildren:

const list = { show: { transition: { staggerChildren: 0.2 } } };
const bar = {
  hidden: { pathLength: 0, opacity: 0 },
  show: { pathLength: 1, opacity: 1, transition: { duration: 0.3 } },
};

<motion.svg viewBox="0 0 24 24" initial="hidden" animate="show" variants={list}>
  <motion.path d="M4 6h16" variants={bar} />
  <motion.path d="M4 12h16" variants={bar} />
  <motion.path d="M4 18h16" variants={bar} />
</motion.svg>

Motion can animate d too, with one catch: it gives no warning when the commands differ. Going from L to H and V ran without an error in production and wrote wrong shapes such as M15.95 15.95 H84.05 V27.86 H72.14 Z. It writes the attribute from JavaScript, so a matching pair also morphs in WebKit.

What it costs. Minified and gzipped with esbuild, React not counted:

ImportGzipped
motion from motion/react48.3 KB
LazyMotion with domMax and m48.6 KB
LazyMotion with domAnimation and m33.1 KB
animate from motion27.3 KB
animate from motion/mini3.7 KB

LazyMotion with domAnimation drew pathLength fine in our test, so it is the easy saving. If one checkmark is all you animate, the CSS above costs nothing. GSAP and anime.js are other options that we did not test.

Respect reduced motion

Each method needs its own handling, and two of them surprised us:

MethodStops with prefers-reduced-motion?What to do
CSSOnly if you write the media queryanimation: none, and make the finished state the default
SMIL inlineNo, it keeps movingCall svg.pauseAnimations() from a script
SMIL in <img>No, it keeps movingUse CSS animation in the file instead
Motion MotionConfigNo, pathLength still animatesUse the hook below
@media (prefers-reduced-motion: reduce) {
  .draw { animation: none; stroke-dashoffset: 0; }
}

<MotionConfig reducedMotion="user"> only turns off transform and layout animations, so a line still draws. Skip the start state with the hook, which worked in Chromium and Firefox:

const reduce = useReducedMotion();

<motion.path initial={reduce ? false : { pathLength: 0, opacity: 0 }} animate={{ pathLength: 1, opacity: 1 }} />

The img result is from Chromium and Firefox with the real system setting. We could not set it for an img in WebKit. An animated icon also still needs a name for screen readers: see accessible SVG icons.

Draw on scroll

Tie the same keyframes to scroll position with one more property. No JavaScript is needed:

.draw {
  animation: draw linear both;
  animation-timeline: view();
  animation-range: entry 20% cover 60%;
}

It ran as expected in Chromium. MDN lists animation-timeline for Chrome 115, Firefox 159 and Safari 26. A browser without it shows the finished drawing, as Firefox 142 did in our test, so it fails safely.

Can you animate an SVG with only CSS?
Yes. Line drawing, moving along a path, hover effects, spins and fades all work with CSS and no JavaScript. Morphing is the exception: the CSS d property does not work in Safari, so use SMIL for it.
Why does my SVG path show a dot before it draws?
Round line caps paint even on a zero-length dash. In our test, Chromium did this with a dash pattern of 1 at pathLength 1. A pattern of 1 2 removes it in all three engines. In Motion, animate opacity from 0 as well.
How do I animate an SVG path without measuring its length?
Set pathLength to 1 on the path, then use a dash pattern of 1 2 and animate the dash offset from 1 to 0. The result matched the measured length in all three engines.
Is SMIL deprecated?
No. MDN lists animateMotion as widely available since January 2020, and it ran in all three engines in our test. It does ignore the reduced motion setting, so stop it yourself.
Do SVG animations work in an img tag?
CSS and SMIL animations inside the file run. Scripts inside the file do not, and CSS on your page cannot reach inside it.
Is Framer Motion the same as Motion?
Yes, Framer Motion was renamed. The motion package is the one to install, and React components import from motion/react. The framer-motion package is still published too.

Related posts

Share this post