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.
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.
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 want | Use | JS added | Safari |
|---|---|---|---|
| A line that draws itself | stroke-dashoffset | 0 KB | Yes |
| A dot moving along a curve | <animateMotion> or offset-path | 0 KB | Yes, offset-path from 16 |
| Shape A turning into shape B | SMIL <animate> on d | 0 KB | Yes |
| Hover and click feedback | CSS transform | 0 KB | Yes |
| Springs and gestures in React | Motion | 33 to 48 KB | Yes |
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 start | Chromium | Firefox | WebKit |
|---|---|---|---|
pathLength=1, dash 1 | 209 | 0 | 0 |
pathLength=100, dash 100 | 209 | 0 | 0 |
Measured length, dash 92 | 0 | 0 | 0 |
pathLength=1, dash 1 2 | 0 | 0 | 0 |
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:
| Pack | Icons | Stroke icons | One shape, one subpath |
|---|---|---|---|
| Lucide | 2,100 | 2,085 | 109 (5%) |
| Tabler | 6,305 | 5,166 | 330 (6%) |
| Heroicons | 1,288 | 324 | 22 (7%) |
| Phosphor | 9,161 | 0 | 0 |
| Material Symbols | 16,475 | 0 | 0 |
| Remix Icon | 3,229 | 0 | 0 |
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 uses | CSS d, Chromium and Firefox | SMIL d, WebKit |
|---|---|---|
| Same commands | Morphs | Morphs |
Relative l for L | Morphs | Morphs |
H and V for L | Jumps to the end | Paints nothing |
C for L | Jumps to the end | Paints nothing |
| Fewer points | Jumps to the end | Morphs, 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:
| CSS | Corner moved to | Result |
|---|---|---|
| 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 file | Runs | Runs |
| SMIL in the file | Runs | Runs |
<script> in the file | Runs | Does not run |
| Page CSS, hover on parts | Works | Does 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:
| Import | Gzipped |
|---|---|
motion from motion/react | 48.3 KB |
LazyMotion with domMax and m | 48.6 KB |
LazyMotion with domAnimation and m | 33.1 KB |
animate from motion | 27.3 KB |
animate from motion/mini | 3.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:
| Method | Stops with prefers-reduced-motion? | What to do |
|---|---|---|
| CSS | Only if you write the media query | animation: none, and make the finished state the default |
| SMIL inline | No, it keeps moving | Call svg.pauseAnimations() from a script |
SMIL in <img> | No, it keeps moving | Use CSS animation in the file instead |
Motion MotionConfig | No, pathLength still animates | Use 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?
Why does my SVG path show a dot before it draws?
How do I animate an SVG path without measuring its length?
Is SMIL deprecated?
Do SVG animations work in an img tag?
Is Framer Motion the same as Motion?
Related posts