Skip to content
Back to all articles
Developer5 min readSeptember 30, 2026

How to Animate SVG With CSS: A Practical Guide With Examples

Line drawing, spinning loaders, hover effects and staggered reveals: how CSS SVG animation works, the gotchas that trip everyone up, and how to keep it accessible.

How to Animate SVG With CSS: A Practical Guide With Examples article
In this guide
  1. Why animate SVG with CSS?
  2. The basic pattern
  3. Gotcha 1: shapes rotate around the wrong point
  4. Gotcha 2: the line-drawing effect
  5. Gotcha 3: an SVG in an img tag cannot be styled from your page
  6. Five effects worth knowing
  7. Staggered reveals
  8. Triggers: auto, hover, click and load
  9. Respect reduced motion
  10. Building animations visually
  11. Keep it fast and purposeful

There is a small moment of delight when a checkmark draws itself after you submit a form, or a logo traces its outline as a page loads. It feels polished and alive, and it usually costs just a few lines of CSS. Yet the first time many people try it, the result is baffling: the shape spins around a point in the corner, the "draw" effect does nothing, or the animation looks fine on one icon and completely wrong on the next.

This guide explains how CSS animation of SVG really works, with small examples you can copy, the gotchas that trip almost everyone up, and how to keep motion accessible for people who find it uncomfortable.

Why animate SVG with CSS?

SVG graphics are made of shapes described in code, which makes them ideal for animation:

  • They stay sharp at every size and on every screen.
  • They are tiny compared with GIFs or videos.
  • Every part can move independently, because each path or circle is its own element.
  • CSS animation is widely supported and efficient, especially for transforms and opacity.

The same keyframes and animation properties you use for HTML work on SVG, with a couple of important differences covered below.

The basic pattern

An animation has two parts: keyframes describing the change, and an animation rule applying them to an element.

css
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.12); }
}

.icon-heart {
  transform-box: fill-box;
  transform-origin: center;
  animation: pulse 1.6s ease-in-out infinite;
}

Apply the class to an element inside your inline SVG, and it gently beats. Notice the transform-box line; it is the key to the most common SVG animation problem.

Gotcha 1: shapes rotate around the wrong point

In HTML, transform-origin: center means the centre of the element. Inside an SVG, by default it refers to the SVG's own coordinate system, often its top-left corner. So a spinning gear swings around the corner of the drawing instead of turning in place.

The fix is transform-box: fill-box, which makes the origin relative to the element's own bounding box. Add it to any element you rotate or scale, and transform-origin: center behaves the way you expect.

Gotcha 2: the line-drawing effect

The "self-drawing" effect uses two stroke properties:

  • stroke-dasharray turns the outline into dashes of a given length.
  • stroke-dashoffset shifts where the dash pattern starts.

If one dash is as long as the whole path, and you offset it by the same length, the line is completely hidden. Animating the offset to zero draws it in. The traditional problem is knowing how long the path is. A tidy trick avoids measuring entirely: set pathLength="1" on the path, and the browser treats its length as 1.

html
<path d="M40 62 L54 76 L82 44" pathLength="1" class="check" />
css
.check {
  fill: none;
  stroke: #178a66;
  stroke-width: 8;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: draw 0.8s ease-out forwards;
}

@keyframes draw {
  to { stroke-dashoffset: 0; }
}

Two things to remember: the effect animates the stroke, so shapes that only have a fill will not appear to draw; and forwards keeps the finished line visible after the animation ends.

Gotcha 3: an SVG in an img tag cannot be styled from your page

CSS from your stylesheet can only reach an SVG that is inline, written directly in the HTML. If the SVG is loaded with an img tag or as a CSS background, your page's styles cannot touch its insides. In that case, put the animation styles inside the SVG file itself, in a style element. That is exactly what a self-contained animated SVG export does.

Five effects worth knowing

EffectProperty animatedGood for
Drawstroke-dashoffsetLogos, checkmarks, signatures, diagrams
Spintransform: rotate()Loaders, refresh icons, gears
Pulsetransform: scale()Hearts, notifications, calls to action
Floattransform: translateY()Illustrations, hero graphics
FadeopacityReveals, subtle attention

Transforms and opacity are the smoothest to animate, because browsers can handle them very efficiently. Animating things like blur filters on large graphics is more demanding, so use those sparingly.

Staggered reveals

Some of the nicest effects come from animating parts one after another: three dots pulsing in turn for a loader, or illustration pieces appearing in sequence. Give each element the same animation with a growing delay:

css
.dot { animation: pulse 1.2s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
.dot:nth-child(2) { animation-delay: 0.15s; }
.dot:nth-child(3) { animation-delay: 0.30s; }

A stagger of 0.1 to 0.2 seconds usually feels natural. Much longer and it starts to feel slow.

Triggers: auto, hover, click and load

  • Automatic and infinite suits loaders and ambient motion, but use it sparingly on content pages.
  • On hover works well for icons and buttons: wrap the animation rule in a :hover selector.
  • On click needs a tiny bit of JavaScript to add a class that starts the animation.
  • Once on load is ideal for logo reveals and success states: one iteration with forwards.

Respect reduced motion

Some people experience dizziness, nausea or distraction from animation, and they can ask their operating system to reduce motion. Honour that preference:

css
@media (prefers-reduced-motion: reduce) {
  .animated-svg,
  .animated-svg * {
    animation: none !important;
    stroke-dashoffset: 0 !important;
  }
}

Setting the dash offset to zero means line drawings still appear, fully drawn, instead of staying invisible.

Building animations visually

Writing keyframes by hand is a great way to learn, but tweaking timing by editing numbers and refreshing gets slow. The SVG Animation Generator lets you load an SVG, pick from 15 effects, animate the whole graphic or each element with a stagger, and adjust duration, easing, direction, origin and trigger with a live preview. It exports plain CSS, a self-contained animated SVG, or a React component, adds the reduced-motion rule automatically, and cleans scripts out of uploaded SVGs before previewing them.

If you need to adjust the shapes themselves first, the SVG Path Editor helps with path data, and for animating HTML elements rather than SVG, the CSS Animation Generator covers the same ideas.

Keep it fast and purposeful

  • Animate with a reason: feedback, guidance or a moment of delight, not constant motion.
  • Keep durations short for interface feedback, roughly 200 to 600 milliseconds.
  • Prefer transforms and opacity for smoothness.
  • Optimise the SVG before animating; smaller files with fewer elements are easier to animate and faster to load.

Animation also affects how a page feels during loading. If your hero graphic is large, our guide to Core Web Vitals explains how to keep it from slowing down the main content, and if you are adding motion to a design mockup, the placeholder images guide covers keeping layouts stable while assets are still in progress.

Start small: take one icon on your site, make it draw itself once on load, and add the reduced-motion rule. It is a two-minute change, and it is the kind of detail visitors notice without quite knowing why.

Was this article helpful?

Found this guide useful?

Share it with someone who is struggling with the same problem.

Free tool · No sign-up

SVG Animation Generator

Animate any SVG with CSS: choose from 15 effects such as Draw, Spin and Pulse, animate the whole graphic or each element, then export CSS, a self-contained animated SVG or a React component.

Open tool

Written by

Hemant Adhikari HemantInsights

Senior Software Engineer · Developer of Toolyconvert

I’m a senior software engineer from Dehradun, India, building websites since 2014. I designed and developed Toolyconvert, from the Next.js front end and headless WordPress back end to the tools themselves, and I review every guide before it goes live.