SVG animation: SMIL, CSS and the Web Animations API

Compare SMIL, CSS and JavaScript animation for SVG, with working examples for draw-on strokes and motion paths, plus how to honor prefers-reduced-motion.

SVG can be animated three ways: with SMIL elements written inside the SVG, with CSS animations and transitions, and with JavaScript through the Web Animations API. They overlap, but each has strengths. This guide shows all three and ends with the part that matters for real users: respecting reduced-motion settings.

Choosing an approach

Approach Lives in Good for Watch out for
SMIL (<animate>, <animateTransform>, <animateMotion>, <set>) The SVG markup Self-contained animated files, motion along a path, click-triggered changes with no script CSS rules such as animation: none do not switch it off; less familiar tooling
CSS animations and transitions A stylesheet or <style> Hover and focus effects, looping decoration, anything you already style with CSS Transform origins behave differently on SVG shapes
Web Animations API JavaScript Sequencing, pausing, reversing, reacting to user input Needs script, so it will not run when the SVG is loaded as an image

The last point comes from MDN’s page on SVG as an image: JavaScript is disabled when an SVG is used through <img> or CSS backgrounds. See SVG with JavaScript for the contexts where scripts do run.

SMIL: animation inside the file

SMIL stands for Synchronized Multimedia Integration Language. In SVG you use a small set of elements, placed as children of the shape they animate.

  • <animate> changes one attribute over time. It takes attributeName, from, to or values, dur and repeatCount.
  • <animateTransform> animates the transform attribute, which is not a single number.
  • <animateMotion> moves an element along a path written with the same syntax as the d attribute.
  • <set> changes a value at a point in time.

MDN marks <animate> and <animateMotion> as widely available across browsers, with support dating from January 2020. The Web Animations specification says it aims to let SVG stop depending on SMIL, so SMIL is not where new animation features are headed. It remains a practical choice for self-contained graphics.

The <mpath> child reuses an existing path instead of repeating its data. For motion, rotate="auto" turns the element to follow the direction of travel.

Triggering SMIL without script

The begin attribute accepts offsets, the end of another animation (first.end), and events on an element (startButton.click). The value indefinite waits until script calls beginElement(). This is enough for small interactions with no JavaScript at all.

fill="freeze" keeps the end value after the animation finishes. A pointer-only trigger like this is fine as a demo. For production, give the control a keyboard path as described in interactive infographics.

CSS animation on SVG

Most presentation attributes, including fill, stroke, opacity and transform, can be animated with CSS. Two SVG-specific details cause most of the trouble.

Transform origin. By default, transform-box is view-box, so percentage and keyword origins resolve against the whole SVG viewport, not against the shape. A shape you rotate swings around the canvas. Setting transform-box: fill-box makes the origin relative to the shape’s own bounding box. MDN’s example uses exactly this pairing: transform-origin: 50% 50% with transform-box: fill-box.

Draw-on strokes. stroke-dasharray sets a repeating dash and gap pattern, and stroke-dashoffset shifts where the pattern starts. If the dash is as long as the path and the offset is also that long, the stroke is invisible. Animating the offset to 0 reveals the line. You need the path length. For a straight line of 160 units it is simply 160. For curves, measure it in script with getTotalLength(), covered in SVG with JavaScript.

Remove transform-box:fill-box from the editor and the square orbits a point far from itself. If your browser or operating system is set to reduce motion, this example shows the finished line and a still square.

JavaScript: the Web Animations API

element.animate(keyframes, options) runs an animation and returns an Animation object. Unlike CSS, you can pause it, reverse it, change its speed and wait on a promise.

const path = document.querySelector("#route");
const length = path.getTotalLength();

path.style.strokeDasharray = length;
const anim = path.animate(
  [{ strokeDashoffset: length }, { strokeDashoffset: 0 }],
  { duration: 1200, easing: "ease-out" }
);

anim.finished.then(() => anim.commitStyles());

Durations are in milliseconds, and iterations: Infinity repeats forever. To keep the end state, MDN recommends commitStyles() over fill: "forwards", because it writes the final values into the element’s style so it can be restyled normally. anim.pause(), anim.play(), anim.reverse() and anim.updatePlaybackRate(0.5) give you playback control that CSS alone cannot.

Respecting prefers-reduced-motion

Some people get dizzy or ill from motion on screen. Operating systems let them ask for less of it, and browsers expose that choice as the prefers-reduced-motion media feature. MDN notes that scaling and panning large objects are common triggers, and that replacing motion with a subtler effect, such as a fade, is better than removing feedback altogether.

For CSS animations, add a media query that turns them off or swaps them, as the example above does.

For the Web Animations API, test the preference before you start:

const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");
if (!reduce.matches) {
  path.animate(frames, { duration: 1200 });
}

SMIL timing is not a CSS animation, so an animation: none rule inside a media query has no effect on it. Pause the animation clock from script instead. pauseAnimations() suspends all running animations in that SVG, and the same media query tells you when to call it.

const svg = document.querySelector("svg");
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)");
if (reduce.matches) svg.pauseAnimations();

Check what the frozen first frame looks like. If it is a half-drawn state, design the starting values so the static image still makes sense.

MDN’s accessibility guidance on <animate> adds two more rules: avoid blinking and flashing, and give users a way to pause or disable animation. A visible pause button on any decorative loop that runs longer than a few seconds is a reasonable default. See SVG accessibility for naming and describing animated graphics.

Practical advice

  • Keep looping decoration subtle and finite where possible.
  • If a graphic must work as an <img>, prefer SMIL or CSS inside the file and skip script. Test the context you ship in, because browser compatibility differs between embedding methods.
  • Animated filters are the most expensive effect on this page. See SVG filters before animating baseFrequency or blur radius.

Sources

Checked against these references on 2026-11-21.