Animating SVG: SMIL versus CSS, and the costs nobody mentions
SVG can be animated in three ways: SMIL elements such as <animate> and <animateTransform>, CSS animations and transitions on SVG properties, and JavaScript that changes attributes or the DOM. Each has a different support story, a different cost, and a different relationship with accessibility settings.
SMIL is part of the SVG specification and runs without script. It can animate almost any attribute, including path data, which CSS cannot do reliably. It also has no media-query awareness: a <animate> element does not know that the user asked for reduced motion. Chrome and Firefox support it; support has been deprecated and restored more than once, which is a reason to avoid depending on it for essential meaning.
| Technique | Animates | Reduced motion | Notes |
|---|---|---|---|
| SMIL animate | Almost any attribute, including d | Manual | No script needed, patchy future |
| CSS transition | CSS properties only | Media query | Best for hover and state changes |
| CSS @keyframes | CSS properties only | Media query | Good for loaders and attention cues |
| Web Animations API | CSS properties and transforms | Check in script | Fine control, needs script |
| JavaScript attribute loop | Anything | Check in script | Most expensive, easiest to get wrong |
What to animate for an icon
For icons, animate transform and opacity first. They can be composited on the GPU and do not force a layout. Animating stroke-dashoffset is the standard way to draw a line on, and it is cheap because it only changes paint. Animating geometry such as d, cx or width forces the renderer to rebuild the shape on every frame and is visible on low-end phones.
Keep the animation short. A spinner that runs forever is a battery cost and a distraction; a state change that takes 150 to 250 ms reads as responsive. If the animation is decorative, it should stop when the tab is hidden, which CSS does automatically for compositor-driven animations and JavaScript often forgets.
Reduced motion is not optional
WCAG 2.3.3 asks that motion triggered by interaction can be disabled unless it is essential. The practical implementation is a media query that removes or shortens the animation, plus a static state that still communicates the result. For a loading spinner, the static state can be a partially drawn arc with a text label. For a success check, the static state is the check itself.
Test with the operating system setting on. A media query written in CSS may be overridden by a more specific rule, and an inline style attribute beats both. Put the reduced-motion rule last and keep animations out of inline styles.
An animated SVG is still an image. If the animation conveys state, the state needs a text or ARIA equivalent; a spinning icon that means loading must also be announced or labelled as such.
Measure the cost before optimising it. A single small spinner on a page rarely matters; a dozen animated icons in a list can keep the compositor busy on every scroll. The fix is usually to animate only what is visible, not to rewrite the animation.
Sources
Questions
Can I animate an SVG with CSS?
Yes, for properties that CSS can animate: transform, opacity, stroke-dashoffset, fill and stroke colour, and the CSS custom properties a drawing uses. SVG geometry attributes such as cx or d are not CSS properties in every browser, so path morphing still needs SMIL or JavaScript.
Does prefers-reduced-motion work with SMIL?
Not automatically. CSS animation respects the media query when you write one, but SMIL does not read it. For SMIL you need JavaScript to pause the timeline, or you duplicate the static state and swap it. For accessibility, CSS animation is easier to make respectful.
Why did my animated SVG grow so much?
Animation adds elements and attributes to every animated node, and SMIL often requires duplicate keyframe values. A 1 KB icon with three animated attributes can double in size. Optimisers cannot remove animation data without breaking it, so the saving must come from the rest of the file.
Paste an animated SVG into the optimizer; the markup passes are safe, but check the preview before shipping.
Open the optimizerWritten against the W3C SVG specifications, MDN and the React and WCAG documentation; every sample in the examples can be pasted into the optimizer linked above.
Back to the guides