# Bringing SVGs to Life With CSS, SMIL, and JavaScript by [kirupa](https://www.kirupa.com/me/index.htm) | filed under [Web Animation](https://www.kirupa.com/html5/learn_animation.htm) | 2 December 2026 **TL;DR:** To animate an SVG, first check where it lives. Our page's CSS and JavaScript can animate an inline svg element, while an SVG loaded with img only runs the CSS and the animation tags (called SMIL) inside its own file. CSS handles colors, spins, and line drawing, SMIL moves shapes along paths, and JavaScript handles motion driven by data. - An SVG loaded with img or as a CSS background is sealed off from our page, so only the CSS keyframes and SMIL written inside the file can animate it. - SVG shapes rotate around the SVG's 0, 0 point by default, so we add transform-box: fill-box and transform-origin: center to spin a shape in place. - A line draws itself when we set pathLength="1" and stroke-dasharray: 1 on the path and animate stroke-dashoffset from 1 to 0. - Safari ignores the CSS d property, so a path morph that works in every browser uses SMIL's animate element or JavaScript. An SVG is a picture made out of instructions: draw a circle here, a curvy line there, and fill this shape with orange. Because those shapes are real elements instead of a grid of pixels, we can change their colors, positions, and even their outlines over time. That is how logos spin, checkmarks draw themselves, and little dots race along a map. The tricky part is that there are three ways to animate an SVG, and which ones work depends on how our SVG got onto the page. We are going to walk through all three, fix the rotation gotcha that bites just about everyone, and build the famous line-drawing effect along the way. Onwards! ## Three Little SVG Animations Before we look at any code, here is what we will build. Each panel below animates an SVG a different way: Interactive example: [Three SVG animations side by side](https://www.kirupa.com/animations/examples/svg_animation_lab.htm) **Try it:** 1. Uncheck **Use fill-box** and watch the sun 2. Press **Replay** to draw the squiggle again 3. Press **Pause** under the red dot The sun spins and glows with plain CSS. The squiggle draws itself with a trick on its outline (aka its **stroke**), and the red dot follows its track using tags inside the SVG. Unchecking **Use fill-box** sends our sun swinging around the corner, which is a really common bug. ## Where Does Our SVG Live? An SVG that we paste right into our HTML as an `` element (aka an **inline SVG**) can be styled and scripted by our page, just like a `div`. An SVG that we load as a file, with `` or as a CSS `background-image`, is treated like a photo. Our page's CSS and JavaScript can't reach the shapes inside it. Here is the same sun loaded both ways: [![On the left, our sun is an inline svg element inside our page, and arrows show page CSS and page JS both reaching it, each with a green check. On the right, the sun is loaded with img src="sun.svg". Arrows from page CSS and page JS stop at red X marks before the image's thick border, and inside the image are two tags: @keyframes and SMIL.](https://www.kirupa.com/animations/images/svga_reach.png)](https://www.kirupa.com/animations/images/svga_reach.png?v=2a2145519c1f) On the left, our page's CSS and JavaScript can change the sun's circle and rays. On the right, our `sun.svg` file is sealed off. Only animations written inside the file run, as CSS `@keyframes` in a `