Guide · Vector graphics
How to Create an SVG Animation for the Web
An SVG animation is motion created by changing an attribute, a style or an element inside a vector graphic over time, so the artwork stays sharp at every size. You can build one with CSS keyframes, with SMIL elements inside the file, or with JavaScript through the DOM and the Web Animations API. The hard part is not the syntax. It is choosing what should move, how the file will be embedded, and how the animation behaves for people who prefer less motion. This guide covers that path from planning to browser testing.
Key takeaways
- Animate one property on one named group at a time, because a clear target is easier to time, test and debug.
- Choose CSS for simple loops and hover states, SMIL for a self-contained file, and JavaScript when the motion must respond to input.
- The embed method decides what is possible: page CSS and scripts cannot reach inside an SVG loaded through an img tag.
- Keep the viewBox stable and animate inner groups so the drawing scales without recalculating the canvas.
- Honor prefers-reduced-motion and offer a pause control before publishing any autoplaying motion.
- Test the page, not the file, in every browser and embed method you support.
Which parts of an SVG can you animate?
An SVG is a tree of elements and attributes, and an animation changes one of those values over time. MDN describes the animate element as a way to animate an attribute of an element over time.
The parts that reward animation are usually groups, not individual points. A group can carry a transform, an opacity or a color, and every shape inside it follows. That keeps the animation data small and the timing readable.
- Transform on a group: rotate a gear or slide a panel without redrawing its shapes.
- Opacity: fade a layer in, out or between states.
- Fill and stroke: shift a color or highlight an outline.
- Stroke dash pattern: animate stroke-dasharray and stroke-dashoffset to draw a line.
- Path data: morph one outline into another, which is powerful but uneven across browsers.
- Motion along a path: send an icon along a route with animateMotion or an equivalent technique.
- Text: move or fade a whole block, but avoid animating font layout.
Leave the viewBox and canvas size alone. Animating the coordinate frame changes every shape at once and makes the result hard to reason about. If the drawing needs to move, put the motion on an inner group.
How should you plan an SVG animation before you build it?
Start with the job the motion has to do. A loading indicator, a sequence diagram, a hover response and a brand flourish need different timing and technology. Write the job in one sentence, then decide whether the animation plays once, loops or waits for an interaction.
Decide how the file will be embedded before you choose a technique. Inline SVG is part of the page, an object embed is a separate document, and an img is treated as an image. MDN notes that scripts never run in an image, and SVGator notes that img interactivity is disallowed in most browsers.
- Write one sentence on what the motion explains.
- Choose one-shot, loop or interaction, and note the trigger.
- Sketch three to five key moments with rough timings.
- Name the groups and IDs you will animate.
- Decide between inline, object and img.
- Set a size budget with the rest of the page.
- Design the still frame first, as your fallback.
- Plan the reduced-motion version now.
If you cannot explain the job in one sentence, a static SVG is the better answer. Motion without a job usually distracts from the content and adds weight to the page.
How do CSS, SMIL and JavaScript compare?
The W3C SVG 2 animation chapter lists five routes: SVG animation elements, CSS animations, CSS transitions, scripting through the SVG DOM, and the Web Animations API. It also states that SVG does not mandate support for any of these methods. The method you choose is a delivery decision, not a guarantee that every renderer behaves the same way.
In practice, the five routes collapse into three choices.
- CSS: keyframes and transitions for opacity, transform, fill and stroke on simple loops and hover states. It is the lightest option for simple motion.
- SMIL: animation elements inside the file, for a self-contained loop that must work without a script.
- JavaScript: the SVG DOM and Web Animations API, for input, sequencing, path work and play or pause control.
The lines are not absolute. A page can use a CSS idle loop and switch to JavaScript for an interactive sequence. Avoid letting two systems fight over the same property, because the result depends on order and is hard to debug.
Pick one owner for each animated value.
When should you use CSS animation?
CSS is the right default for simple, repeatable motion: a fade, a slow rotation, a small loading state. MDN explains that a CSS animation has an animation declaration and a set of keyframes, with 0 percent and 100 percent marking the start and end. SVG has its own CSS properties, including fill, stroke and stroke-width.
Keep the animated properties cheap. MDN recommends transform-based properties over properties that affect the box model, and notes that the browser can optimize a CSS animation, including skipping frames and reducing work in background tabs. Rotate or scale a group rather than recalculating a complex path on every frame.
Where you put the CSS matters. A style element inside the SVG travels with the file, so it works when the SVG is used as an image. Page CSS only reaches an inline SVG.
SVGator says a CSS export works with inline, object and img embedding, which makes it the most portable option.
CSS becomes awkward when you need a timeline, a scroll trigger or a dependent sequence. SVGator also reports that Safari is the least reliable browser for advanced CSS SVG work, especially filters, path motion and path morphing.
When should you use SMIL?
SMIL is an XML-based language for interactive multimedia, and SVG uses its animation elements. The common ones are animate for one attribute, animateTransform for translation, scaling, rotation and skew, and animateMotion for movement along a path. They live inside the file, so the motion travels with the artwork.
The timing model is compact. MDN lists attributeName, from, to, values, dur, begin, repeatCount, fill, keyTimes and calcMode. A repeatCount of indefinite loops forever, begin can be a time offset or an event, and fill decides whether the final value stays or reverts. animateTransform is separate because a transform is not a single number.
SMIL fits a self-contained loop: a spinner, a pulsing status dot, a diagram that draws itself. It needs no script and no build step, and the animation is visible in the markup. It is a poor fit for complex sequencing, scroll effects and anything that reacts to application state.
Verify support rather than assuming it. The SVG 2 specification does not require support for any animation method. Test your browsers and embed methods, and keep a static frame as a fallback.
When should you use JavaScript?
JavaScript is the right choice when motion must respond to something: a click, a scroll position, a form state or a sequence that depends on what happened before. The Web Animations API gives you element.animate, which MDN describes as a shortcut that creates an Animation and plays it. It returns the object, so you can pause, reverse or inspect it.
SVG fits this model because its elements are part of the DOM. MDN notes that the same DOM APIs used with HTML work on SVG, but elements created from script must use the SVG namespace through createElementNS. Scripts never run when the SVG is loaded as an image, so interactive motion must be inline or in an object embed.
The cost is weight and complexity. SVGator says JavaScript animations usually increase file size because the animation data and the code that runs it are both embedded. They also need error handling, cleanup and a plan for when the script fails.
Use JavaScript when the interaction justifies that cost.
How do you build a simple SVG animation step by step?
Build a small animation in layers, with a test after each step. The sequence below works for a CSS loop, a SMIL loop or a JavaScript interaction, because the order is about decisions rather than syntax.
- Start from a clean SVG with a viewBox and a few named groups.
- Save a static copy before you add motion.
- Pick one group and one property, such as rotation or opacity.
- Add the smallest animation that shows the effect.
- Set the timing and trigger, then watch the first and last frame.
- Add a reduced-motion rule and a pause control.
- Optimize the file, embed it for real and test the page.
A loading icon can be a group that rotates at a constant speed with a CSS keyframe. A line that draws itself can use stroke-dasharray and stroke-dashoffset, with the dash pattern set to the path length. A five-step diagram with next and back buttons needs JavaScript.
Keep the first version plain. Add easing and polish only after the timing works with the real content around it.
Which tools export animated SVG or Lottie?
You can hand-code a small animation. A timeline editor earns its place when the motion has many layers or a designer needs to iterate without editing markup. Check export formats first, because the format decides how the file is embedded.
SVGator exports animated SVG using CSS or JavaScript, Lottie as JSON or optimized JSON, GIF and animated WebP and APNG, several video formats, and React Native and Flutter packages. Its guidance is to use CSS export for simple motion and JavaScript export for interactivity, with object or inline embedding for the JavaScript version.
Lottie is a JSON format that describes an animation's elements and motion. LottieFiles notes that the JSON format has no built-in compression, while dotLottie is a compressed ZIP container that can package animations, assets, themes and state machines, and needs a dotLottie player. SVGator says Lottie handles complex motion-design work well but needs a runtime library.
Rive describes itself as an engine for interactive 2D and 3D content, with state machines that respond to input, screen size and scrolling, and open-source runtimes without royalties. Our SVG animation tools, motion graphics software and 2D animation software categories list more options.
How do you keep an animated SVG small and fast?
Animation adds data, so remove what the file does not need first. Delete hidden layers, unused defs, editor metadata and embedded raster images before you animate anything. Reduce coordinate precision and reuse symbols instead of duplicating shapes.
The how to edit SVG files guide covers cleanup in more detail.
Then choose the lightest technique that does the job. SVGator reports that CSS-only animations keep files smaller because there is no JavaScript runtime, while JavaScript animations usually increase size. Fewer keyframes and group-level transforms cost less than animating many points or a complex path.
For Lottie, the format matters. Lottie JSON has no built-in compression, while dotLottie is a compressed ZIP that can be smaller and can bundle assets and themes. Choose dotLottie when your player supports it, and JSON when you need the widest compatibility.
Measure the file your server sends, not the project file.
- Remove hidden layers, unused defs and metadata.
- Avoid embedded raster images inside an animated SVG.
- Animate group transforms instead of many path points.
- Use CSS for simple loops and reserve JavaScript for interaction.
- Prefer dotLottie when the player supports it.
If the file is still heavy, remove a layer of motion rather than hiding the cost behind a loading screen.
How do you make an SVG animation accessible?
Start with motion preference. MDN describes prefers-reduced-motion as a media feature that detects a device setting to minimize non-essential motion, with reduce meaning it is enabled. It notes that animation can trigger discomfort for people with vestibular motion disorders, especially scaling or panning large objects.
A reduced version should replace that movement with a fade or a static state.
Give people a way to stop autoplaying motion. MDN's animate reference warns that blinking or flashing can affect some users and recommends a mechanism for pausing or disabling animation, plus respect for prefers-reduced-motion. A visible pause button covers cases the media query does not.
Name the graphic. The SVG title element provides an accessible short description and should be the first child of its parent, and MDN recommends aria-labelledby when visible text already describes the element. Do not rely on motion alone to communicate a state change.
Pair it with text, color or shape so the meaning survives without animation.
Test with reduced motion enabled, with a keyboard and with a screen reader. Check that nothing flashes rapidly, that the page does not jump on start, and that the still frame makes sense.
How do you test an SVG animation in browsers?
Test the page, not the SVG file. An animation that works when you open the file can fail when it is embedded, because the embed method changes what is allowed. MDN states that scripts never run when SVG is used as an image.
SVGator recommends object embedding for interactive SVG and notes that img interactivity is disallowed in most browsers.
Test every browser and embed method you support, on desktop and on a real phone. SVGator calls Safari the least reliable browser for CSS SVG animation, with problems around filters, path motion and morphing of the path data attribute. It reports that CSS morphing of that attribute works in Chrome, Firefox, Edge and Opera but not Safari.
Run the same checks every time.
- Test each embed method you ship: inline, object and img.
- Check Safari for CSS filters, path motion and path morphing.
- Turn on reduced motion and confirm the fallback.
- Disable JavaScript and check the CSS or SMIL version.
- Watch the first frame, the loop seam and the final frame.
- Test at 390 pixels wide and at a zoomed text size.
If a feature fails, simplify before adding a workaround. A shorter animation with fewer moving parts usually survives more browsers. For format decisions, read SVG vs EPS vs AI vs PDF and vector vs raster images.
Frequently asked questions
Can I create an SVG animation without JavaScript?
Yes. CSS keyframes and SMIL animation elements both run without a script. CSS is the lighter option for simple loops and hover states, while SMIL keeps the animation inside the SVG file and needs no stylesheet.
Both are worth testing in the browsers you support, because the SVG specification does not require support for any particular animation method.
Does an animated SVG work inside an img tag?
It depends on the technique. CSS placed inside the SVG file can run in an image, and SVGator says a CSS export works with inline, object and img embedding. JavaScript does not run when SVG is loaded as an image, and SVGator notes that img interactivity is disallowed in most browsers.
For interactive motion, use an object embed or inline SVG.
What is the difference between animated SVG and Lottie?
An animated SVG is a vector document with CSS, SMIL or JavaScript motion inside it. Lottie is a JSON format that describes an animation's elements and motion and needs a player runtime. Lottie suits complex motion-design work, while SVG is simpler to embed and inspect.
SVGator notes that CSS-animated SVG can be indexed by search engines, while Lottie animations are not.
Which browsers support SVG animation?
Current desktop and mobile browsers render CSS and JavaScript SVG animation, but support for specific features is uneven. SVGator reports that Safari is the least reliable for advanced CSS work such as filters, path motion and path morphing. Test the exact effect in Chrome, Firefox, Safari and Edge, and keep a static frame as a fallback.
How do I make an SVG animation accessible?
Respect prefers-reduced-motion, provide a pause control for autoplaying motion, and avoid rapid flashing. Give the SVG an accessible name with a title element, and do not use motion as the only way to communicate a change. Add text or a shape change as well, then test with reduced motion, a keyboard and a screen reader.
How do I reduce the size of an animated SVG?
Remove hidden layers, unused defs, metadata and embedded raster images before adding motion. Animate group transforms rather than many individual points, cut keyframes that do not change the result, and use CSS for simple loops. If you export Lottie, compare JSON with the compressed dotLottie container and measure the file the server sends.
Should I use an animation tool or write the code myself?
Write the code for a single icon or a simple loop, because the result is small and easy to inspect. Use a timeline tool when the motion has many layers, when a designer needs to iterate visually, or when the same animation must also become video or a Lottie file. Check the export formats first, since they decide how the file is embedded.
Sources and further reading
- SVG animation with SMIL, MDN Web Docs
- animate element, MDN Web Docs
- animateTransform element, MDN Web Docs
- Using CSS animations, MDN Web Docs
- prefers-reduced-motion, MDN Web Docs
- Scripting SVG with JavaScript, MDN Web Docs
- SVG 2 animation chapter, W3C
- JavaScript animations vs CSS animations, SVGator
- Browser support for animated SVG, SVGator
- Adding animated SVG to your website, SVGator
- Lottie JSON and dotLottie, LottieFiles
- SVG and CSS, MDN Web Docs
- Element: animate() method, MDN Web Docs
- title element, MDN Web Docs
- Export and file formats, SVGator
- Lottie vs SVG, SVGator
- Getting started with Rive, Rive