How to Animate SVG: Complete Beginner's Guide
Learn how to animate SVGs from scratch. Learn SMIL, CSS, and JavaScript animation techniques with step-by-step tutorials and real code examples.
Vector Wizard Team
May 10, 2026 · 8 min read
How to Animate SVG: Complete Beginner's Guide
SVG animation turns static vector graphics into dynamic visual elements. Whether you are building a landing page, designing an interactive dashboard, or creating a loading spinner, animated SVGs offer flexibility, tiny file sizes, and crisp rendering at any resolution.
In this guide, you will learn every major approach to SVG animation, complete with working code examples you can copy and modify. By the end, you will know exactly which technique to use for any project and how to generate animations in minutes using AI.
What you will learn
- The three main ways to animate SVG: SMIL, CSS, and JavaScript
- When to choose each technique based on your use case
- How to write your first animated SVG from scratch
- Common mistakes beginners make and how to avoid them
- How to speed up your workflow with AI-powered tools
What is SVG animation?
Scalable Vector Graphics (SVG) is an XML-based format for describing two-dimensional graphics. Unlike raster images (PNG, JPEG), SVGs are made of mathematical paths, which means they scale infinitely without losing quality. Animation adds the dimension of time, allowing these paths to move, morph, fade, and transform.
SVG animations are resolution-independent, meaning they look sharp on a 320px mobile screen and a 4K desktop monitor without requiring multiple image files.
The most common use cases for SVG animation include:
- Logo animations — Bring brand marks to life with subtle motion
- Icon animations — Add hover effects and state transitions to UI elements
- Loading spinners — Create lightweight, customizable loading indicators
- Hero sections — Animate illustrations on landing pages
- Data visualizations — Make charts and graphs dynamic
- Micro-interactions — Delight users with button animations, toggles, and form feedback
The three approaches to SVG animation
There are three primary ways to animate SVGs, each with its own strengths, browser support, and ideal use cases:
1. SMIL (Synchronized Multimedia Integration Language)
SMIL is SVG's native animation syntax. You write animations directly inside the SVG markup using elements like <animate>, <animateTransform>, and <animateMotion>.
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="20" fill="#9333ea">
<animate
attributeName="r"
values="20;30;20"
dur="2s"
repeatCount="indefinite"
/>
</circle>
</svg>
Best for: Simple, declarative animations that loop forever or run on a timeline.
Pros:
- Native to SVG, no external dependencies
- Declarative and easy to read
- Supports path morphing and motion along paths
Cons:
- Deprecated in Chrome for CSS animations (but still works for attribute animation)
- Cannot respond to user interaction easily
- Limited control compared to JavaScript
2. CSS Animation
CSS animations apply to SVG elements just like HTML elements. You use @keyframes and properties like transform, opacity, and stroke-dashoffset.
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.2); }
}
.animated-circle {
animation: pulse 2s ease-in-out infinite;
transform-origin: center;
}
<svg viewBox="0 0 100 100">
<circle class="animated-circle" cx="50" cy="50" r="20" fill="#9333ea"/>
</svg>
Best for: UI animations, hover effects, loading spinners, and anything triggered by CSS states.
Pros:
- Excellent browser support
- Hardware-accelerated (smooth 60fps)
- Easy to trigger with classes and pseudo-selectors
- Familiar syntax for web developers
Cons:
- Cannot animate path data (morphing shapes)
- Limited to CSS-transformable properties
- Complex sequencing is difficult
3. JavaScript Animation
JavaScript gives you complete control. You can use the Web Animations API, GSAP, or requestAnimationFrame to manipulate SVG attributes directly.
const circle = document.querySelector('circle');
circle.animate([
{ r: 20 },
{ r: 30 },
{ r: 20 }
], {
duration: 2000,
iterations: Infinity
});
Best for: Complex sequences, interactive animations, games, and data visualizations.
Pros:
- Full programmatic control
- Can animate any SVG attribute
- Perfect for interaction and state management
- Libraries like GSAP provide powerful timelines
Cons:
- Requires JavaScript knowledge
- More verbose for simple animations
- Potential performance overhead if not optimized
Choosing the Right Technique
| Use Case | Recommended Approach | Why | |---|---|---| | Simple looping animation | SMIL | Declarative, no JS needed | | Hover effects, toggles | CSS | Hardware accelerated, easy triggers | | Complex sequences | JavaScript / GSAP | Timeline control, callbacks | | Path morphing | SMIL or JavaScript | CSS cannot morph paths | | Interactive dashboards | JavaScript | Event-driven, dynamic data | | Loading spinners | CSS | Lightweight, GPU accelerated | | Logo reveals | CSS or JavaScript | Precise timing control |
For most web projects, start with CSS. If you need path morphing or complex sequencing, upgrade to JavaScript with GSAP. Reserve SMIL for simple, self-contained SVGs that do not need interaction.
Step-by-Step: Your First Animated SVG
Let us build a simple loading spinner using CSS animation. This is the most common beginner project and teaches core concepts you will use everywhere.
Step 1: Create the SVG Structure
<svg width="64" height="64" viewBox="0 0 64 64" xmlns="http://www.w3.org/2000/svg">
<circle
cx="32"
cy="32"
r="28"
fill="none"
stroke="#9333ea"
stroke-width="4"
stroke-linecap="round"
stroke-dasharray="120"
stroke-dashoffset="120"
class="spinner"
/>
</svg>
Step 2: Add the CSS Animation
@keyframes spin {
0% {
transform: rotate(0deg);
stroke-dashoffset: 120;
}
50% {
stroke-dashoffset: 30;
}
100% {
transform: rotate(360deg);
stroke-dashoffset: 120;
}
}
.spinner {
animation: spin 1.5s linear infinite;
transform-origin: center;
}
Step 3: Embed in Your Page
Place the SVG inline in your HTML and include the CSS. The animation starts automatically.
Always use transform-origin: center when rotating SVG elements. Without it, the element will rotate around the SVG origin (0,0), not its own center.
Common beginner mistakes
1. Forgetting transform-origin
SVG elements default to transform-origin: 0 0, unlike HTML elements which default to center. Always set transform-origin: center or use explicit coordinates.
2. Using px Units in viewBox
The viewBox attribute defines a coordinate system, not pixel dimensions. Use unitless numbers: viewBox="0 0 100 100", not viewBox="0 0 100px 100px".
3. Animating with setInterval
Never use setInterval for animation. It runs independently of the browser's render loop and causes jank. Always use requestAnimationFrame, CSS animations, or the Web Animations API.
4. Overlooking Accessibility
Animated content can cause issues for users with vestibular disorders. Respect prefers-reduced-motion:
@media (prefers-reduced-motion: reduce) {
.spinner {
animation: none;
}
}
5. Inline SVG vs. External File
CSS animations only work on inline SVGs or SVGs embedded via <object> or <iframe>. If you use an <img> tag, the SVG is treated as a static image and animations will not run.
Speed up your workflow with AI
Writing animation code by hand is educational, but for production work, you need speed. AI-powered tools can generate animated SVGs from simple text descriptions.
For example, instead of writing keyframes for a logo reveal, you can describe what you want:
"A logo where the letters draw themselves in sequence, then the icon pulses once."
The AI generates the SVG markup with SMIL or CSS animations, ready to export and embed. You can then tweak the timing, colors, and easing in a visual editor.
When using AI for SVG animation, always review the generated code. Check that animations respect prefers-reduced-motion, use efficient properties (transform, opacity), and do not contain unnecessary elements.
Export formats explained
When you create an animated SVG, you typically have three export options:
SMIL (.svg) — The pure SVG format with embedded animation tags. Works everywhere SVG is supported. Best for simple, self-contained animations.
CSS (.svg + .css) — SVG markup with class names that reference an external or inline stylesheet. Best for web projects where you already have a CSS pipeline.
JavaScript (.js or .html) — SVG manipulated via JavaScript. Best for React, Vue, or Angular projects where you want component-level control.
Next steps
Now that you understand the fundamentals, put them into practice:
- Animate a logo — Start with a simple fade-in or draw-on effect
- Build a loading spinner — Experiment with stroke-dasharray animations
- Add hover effects to icons — Use CSS transitions for subtle feedback
- Try an AI generator — Describe an animation and see what code it produces
For more advanced techniques, check out our guide on CSS vs JavaScript vs SMIL for SVG Animation or explore 10 SVG Animation Examples with Code.
Ready to create your first animated SVG? Try Vector Wizard's SVG Animation Generator and turn your ideas into motion in seconds.