SVG line drawing animation with CSS: make any icon draw itself
· 8 min read
To make an SVG icon draw itself, give each <path> the attribute pathLength="1", set stroke-dasharray so a single dash covers the whole path, and animate stroke-dashoffset from 1 to 0. The dash slides into place along the path, so the line looks as if it's being drawn, with no JavaScript and no animation library.
This tutorial builds that effect step by step on a real icon, then covers what the quick demos skip: icons made of several strokes, the stray dot that round line caps leave behind, replaying the animation, and people who turn motion off. The examples use Iconkeep icons, which are outline icons on a 48px grid with stroke="currentColor", but the technique works on any stroked SVG.
How does the line drawing trick work?
A stroke in SVG can be dashed. stroke-dasharray sets the length of the dashes and the gaps between them, and stroke-dashoffset moves where that pattern starts along the path. MDN's page on >stroke-dashoffset describes it as an offset for the starting point of the dash array: positive values shift the pattern backwards.
So if a dash is exactly as long as the path, and the gap after it is at least as long, the offset decides how much of the line you see:
stroke-dashoffset |
What you see |
|---|---|
| Equal to the path length | Nothing: the gap covers the whole path |
| Half the path length | The first half of the line |
| 0 | The whole line |
| Minus the path length | Nothing again: the line erases from its start |
Animate the offset from the first row to the third and the line draws itself. Jake Archibald described this technique in his 2013 article >Animated line drawing in SVG, measuring each path with JavaScript. Today you can usually skip the measuring.
Step by step: make an icon draw itself
We'll use the rocket icon, which is made of seven separate paths: the body, the window, two fins, the center line, the flame and a crossbar.
Step 1: inline the SVG
CSS on your page only reaches shapes inside an inline <svg>. Copy the SVG from the icon page and paste it into your HTML (see how to use SVG icons in HTML for the other methods and their limits). Keep fill="none": the effect moves the stroke, not the fill.
Step 2: add pathLength="1" to every path
The >pathLength attribute tells the browser to treat the path as if it had that total length; MDN notes that stroke-dasharray then assumes the path runs from 0 to the value you set. With pathLength="1", a dash of 1 covers any path exactly, whatever its real length. It works on path, circle, ellipse, line, polygon, polyline and rect.
<svg class="draw" width="96" height="96" viewBox="0 0 48 48" fill="none"
stroke="currentColor" stroke-width="1.5" stroke-linecap="round"
stroke-linejoin="round" aria-hidden="true">
<path pathLength="1" d="M24 2C12.74 10.69 21.73 29.76 21.73 29.76L26.34 29.82C26.34 29.82 35.26 10.69 24 2"/>
<path pathLength="1" d="M22.61 10C21.71 10.9 21.71 12.36 22.61 13.26C23.51 14.16 24.97 14.16 25.87 13.26C26.77 12.36 26.77 10.9 25.87 10C24.97 9.1 23.51 9.1 22.61 10"/>
<!-- …the other five paths, each with pathLength="1" -->
</svg>
Step 3: hide the strokes with a dash and a long gap
One dash of 1, then a gap of 2, pushed back by just over 1:
.draw path {
stroke-dasharray: 1 2;
stroke-dashoffset: 1.01;
}
Why 1 2 instead of the 1 used in many demos? A single value repeats, so 1 means dash 1, gap 1, and the next dash starts right where the path ends. With round line caps, in our tests that zero-length dash still painted a small dot at the end of closed shapes such as the heart. A longer gap keeps the next dash away from the path. The extra 0.01 on the offset is a small safety margin at the start.
Step 4: animate the offset to 0
.draw path {
stroke-dasharray: 1 2;
stroke-dashoffset: 1.01;
animation: draw 1.2s ease-out forwards;
}
@keyframes draw {
to { stroke-dashoffset: 0; }
}
forwards keeps the finished drawing on screen when the animation ends. Because the hidden state is the element's own style and not only the first keyframe, the icon also stays hidden until the animation starts, which matters once you add delays.
Step 5: stagger the strokes
Drawn all at once, seven paths start together and the icon appears everywhere at once. A short delay per path makes it read as one hand drawing the icon:
.draw path:nth-child(2) { animation-delay: 0.15s; }
.draw path:nth-child(3) { animation-delay: 0.3s; }
.draw path:nth-child(4) { animation-delay: 0.45s; }
/* …or set style="--i: 3" on each path and use: */
.draw path { animation-delay: calc(var(--i, 0) * 0.15s); }
Paths draw in source order, so the order of the <path> elements decides which line comes first.
That's the whole effect. The same CSS works on any outline icon; only the number of delays changes.
When should you measure the path instead?
pathLength="1" is the easiest route, but you may want real lengths, for example if you're animating a path you don't control the markup of, or you want every line to draw at the same speed in pixels rather than in the same time. >getTotalLength() returns a path's length in user units (the units of the viewBox):
document.querySelectorAll('.draw path').forEach(path => {
path.style.setProperty('--len', path.getTotalLength());
});
.draw path {
stroke-dasharray: var(--len) calc(var(--len) * 2);
stroke-dashoffset: calc(var(--len) * 1.01);
animation: draw 1.2s ease-out forwards;
}
The heart icon, for example, measures about 125 units on its 48-unit grid. Measuring costs a script and a layout read, so use it when equal speed matters; otherwise pathLength is simpler.
Replay the animation on hover or when it scrolls into view
A drawing that runs once on page load is often over before anyone looks. Two common triggers:
On hover. Start from the hidden state only while the parent is hovered or focused:
.card .draw path { stroke-dasharray: 1 2; stroke-dashoffset: 0; }
.card:is(:hover, :focus-within) .draw path {
animation: draw-in 0.9s ease-out both;
}
@keyframes draw-in {
from { stroke-dashoffset: 1.01; }
to { stroke-dashoffset: 0; }
}
When it scrolls into view. Add a class with an IntersectionObserver and keep the animation in CSS:
const io = new IntersectionObserver(entries => {
for (const e of entries) if (e.isIntersecting) {
e.target.classList.add('is-drawn');
io.unobserve(e.target);
}
}, { threshold: 0.6 });
document.querySelectorAll('svg.draw').forEach(svg => io.observe(svg));
Then put the animation declaration under .draw.is-drawn path.
Respect reduced motion
Some people get dizzy or distracted by movement on screen and turn on their system's reduce-motion setting. The >prefers-reduced-motion media query reports that setting to CSS, so show the finished icon without the animation:
@media (prefers-reduced-motion: reduce) {
.draw path { animation: none; stroke-dashoffset: 0; }
}
WCAG's >Animation from Interactions criterion (Level AAA) asks that motion triggered by interaction can be disabled, and lists this media query among its sufficient techniques. If your drawing loops, also give people a way to stop it, as the >Pause, Stop, Hide criterion asks.
Common problems and fixes
| Problem | Cause | Fix |
|---|---|---|
| Nothing animates | The SVG is loaded with <img> or as a CSS background, so page CSS can't reach it |
Inline the SVG, or put the CSS inside the SVG file (next section) |
| A dot shows before or after the drawing | Round caps paint a zero-length dash at the path's end | Use stroke-dasharray: 1 2 and an offset just over 1 |
| Lines flash fully visible, then vanish and draw | The hidden state is only in the first keyframe, and the path waits for its delay | Set the offset on the element itself, or use animation-fill-mode: both |
| Filled parts appear at once | Fills don't have a dash pattern | Fade fills in with opacity, or use outline-only icons |
| Several lines draw at the same time | One <path> contains several strokes (several M commands); in Chrome each part started its own dash in our test |
Split them into separate <path> elements and stagger them |
| The icon looks too thin when small | Stroke width scales with the icon | See icon size guidelines for stroke widths at each size |
Put the animation inside the SVG file
To use a drawing animation as a file (in an <img>, a CMS or an email builder that strips page CSS), move the CSS into a <style> element inside the SVG:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" fill="none"
stroke="#111" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
<style>
path { stroke-dasharray: 1 2; stroke-dashoffset: 1.01;
animation: d 1.2s ease-out forwards; }
path:nth-child(3) { animation-delay: .15s; }
@keyframes d { to { stroke-dashoffset: 0; } }
@media (prefers-reduced-motion: reduce) {
path { animation: none; stroke-dashoffset: 0; }
}
</style>
<path pathLength="1" d="…"/>
<path pathLength="1" d="…"/>
</svg>
Scripts don't run in an SVG loaded as an image, but this is plain CSS. In our test in Chrome, a file built like this played in an <img> tag. One catch: currentColor inside an image can't pick up your page's text color, so write the color into the file.
This is how the line-draw versions in Iconkeep Animated are built: each stroke has pathLength="1", the strokes start one after another, the drawing holds and then clears before it loops, small solid details fade in, and everything stays still for people who turn on reduced motion. Every one of the 16,050 icons comes in that version, and 600 icons also have their own crafted animation.
Which icons draw well?
Icons made of a few long, continuous strokes look best: the line has room to travel, and the shape is recognizable halfway through. Busy icons with many short strokes tend to look like flicker. These draw cleanly:
The heart and star are single paths, good for a first test. The route map and pen drawing line suit the effect by subject: something is being traced. For interface feedback, such as a confirmed form or a finished upload, a checklist or trophy drawing in once is enough; don't loop it.
Ui & Interface icons · 485 free icons, SVG and PNGIf the stroke should follow your text color on the page, keep stroke="currentColor" as explained in how to change the color of an SVG icon with CSS. The icons are free for personal and commercial use, and attribution isn't required.
Frequently asked questions
How do you make an SVG path draw itself with CSS?
Give the path pathLength="1", set stroke-dasharray so one dash covers the whole path and a gap hides it, start stroke-dashoffset at 1 and animate it to 0. The dash slides into place and the line appears to draw from its start to its end.
Do I need JavaScript for an SVG line drawing animation?
No. With the pathLength attribute you don't have to measure the path, so the whole effect is CSS. JavaScript is only needed if you want to measure paths with getTotalLength() or start the animation when the icon scrolls into view.
Why does my line drawing animation show a dot before it starts?
With round line caps, a dash of zero length can still paint a round dot. Make the gap longer than the dash, for example stroke-dasharray 1 2 with pathLength="1", so no second dash touches the end of the path.
Does the line drawing animation work with filled icons?
Only on strokes. The technique moves the dash pattern of the outline, so filled shapes don't draw. Fade filled details in instead, or use outline icons, which are made of strokes.
Does the animation work when the SVG is loaded with an img tag?
Page CSS can't reach the inside of an SVG loaded with img, so put the style element and keyframes inside the SVG file itself. In our test in Chrome, the animation then played in an img tag.