How to animate SVGs for total beginners!

The Meet-Cute

SVGs and I met on a lovely afternoon in Toronto, Ontario at the gorgeous new Learning Labs. That's where hackerYou, the school that is teaching me how to code and changing my life, headquarters is.

In exchange for everything my teachers at hackerYou are giving me, if you're reading this and happen to know nothing about SVGs, I'm going to give you the gift of blowing your mind!

I discovered what I'm about to share with you during the first week of my coding bootcamp. That's just how quickly you learn with @WesBos and @brnnbrn at hackerYou.

What are SVGs?

Scalable Vector Graphics, as SVGs are known to be called once in a while, are basically images that are scalable as large as you want without losing any resolution. You could have a screen as big as Canada and still have a perfect looking SVG image on it. This is despite the fact that they're quite small in file-size. Now keep in mind that when I say "image", I'm referring to simpler graphics like logos and cartoons, rather than photographs.

I'm not the greatest illustrator in the world so I like to support The Noun Project and get a lot of my cool-looking SVG files from them instead of subjecting the world to my drawing skills.

Pretty amazing, right? Well, it gets better!

How do you use your SVGs?

The Good Ol' Way

SVGs are sort of like any other image file that you work with in html. You can simply insert them into your code, like you would any other image.

<img src="images/superawesomelogo.svg" alt="make me accessible and describe me please">

The Ever Better Way

SVGs can also be used in code format. Yes, they have code in there somewhere. As in < svg >< /svg >.

Try it for yourself:

Take a moment right now, go to the The Noun Project, download any icon you like and go to the folder you downloaded. In there you'll most likely see three documents:

  1. Your attributions txt. file.
  2. Your .png icon file.
  3. Your .svg file

I'm using this one:
book icon

You can use the png and svg files in the way I showed above. But what happens if you open your svg file in your text editor of choice? I use Sublime Text so I dropped it in there and this is what I got from my cute little svg phone.

<svg class="telephone" viewBox="-100 -97.711 300 297.711">
<path fill="none" d="M133.51,181.456l8.038-6.023c2.611-1.946,4.074-4.58,4.15-7.396c0.07-2.809-1.257-5.574-3.756-7.779
<g id="Your_Icon">
<circle fill="#FF3D7F" cx="45.699" cy="90.648" r="43.877"/>
<circle fill="#FF3D7F" cx="-14.674" cy="92.587" r="12.399"/>
<circle fill="#FF3D7F" cx="-7.877" cy="64.789" r="12.399"/>
<circle fill="#FF3D7F" cx="11.434" cy="42.016" r="12.399"/>
<circle fill="#FF3D7F" cx="39.602" cy="31.225" r="12.399"/>
<circle fill="#FF3D7F" cx="68.308" cy="34.314" r="12.399"/>
<circle fill="#FF3D7F" cx="91.532" cy="51.776" r="12.399"/>
<circle fill="#FF3D7F" cx="-6.191" cy="122.154" r="12.402"/>
<circle fill="#FF3D7F" cx="16.128" cy="143.32" r="12.402"/>
<circle fill="#FF3D7F" cx="45.781" cy="150.571" r="12.402"/>
<path fill="#FF3D7F" d="M87.395,145.027c-1.628,6.653-8.333,10.73-14.993,9.105c-6.65-1.632-10.726-8.336-9.098-14.99
    <path fill="#FF3D7F" d="M104.535,1.289c0,5.633-4.609,10.241-10.241,10.241H84.053c-5.633,0-10.241-4.609-10.241-10.241v-30.724
    <path fill="#FF3D7F" d="M19.191,1.289c0,5.633-4.609,10.241-10.241,10.241H-1.292c-5.633,0-10.241-4.609-10.241-10.241v-30.724
<path fill="#FF3D7F" d="M183.053-73.814C172.811-90.883,63.57-97.711,46.501-97.711s-126.31,6.828-136.552,23.897
    <path fill="#FF3D7F" d="M-14.947,194.168c0,2.816-3.072,5.121-6.828,5.121h-27.31c-3.755,0-6.828-2.304-6.828-5.121
    <path fill="#FF3D7F" d="M148.915,194.168c0,2.816-3.072,5.121-6.828,5.121h-27.31c-3.755,0-6.828-2.304-6.828-5.121
<path fill="#FF3D7F" d="M143.094,11.493c-3.718-6.524-7.227-12.556-14.662-13.618c-23.897-3.414-46.086-5.121-81.931-5.121
    c7.456-0.929,15.963-12.935,16.062-20.445C169.397,96.875,162.57,45.668,143.094,11.493z M112.472,130.344l-18.178-12.986

Oh, hello there! You're still here. After all that? I'd say you and SVGs have a long and healthy relationship ahead of you.

But seriously, thanks for scrolling down to the end of that overwhelming, seemingly nonsensical ream of code. That, my friend, is just one little phone. And why would I replace my clean one line img tag with that?

Let me explain what it all means

In the above example we can easily make sense of everything you see:

  • The < svg > tag simply demarks the start and end of your svg.
  • The Viewbox attribute simply demarks the dimensions of the area in which your icon exists. Sort of like your "artboard" in illustrator.
  • The < path > tag My favourite! This little tag hugs a giant ball of numbers that seems incomprehensible but actually, they're just computer lingo for four simple things that come after the attribute "d=":
    • Moveto Denoted by "m" and some coordinates, this tells the computer where to start your drawing.
    • Arcto Denoted by "a", this tells the computer to draw an arc.
    • Curveto Denoted by "c", this tells the computer to draw a curve determined by something called the Bezier Curve. Don't ask me more about that.
    • Lineto Denoted by "l" is the easiest one - just a straight line on a path. That wasn't so bad, eh? Paths are fun!
  • The < circle > tag This is no more than a circle. A perfect circle.
  • The < g > tag Enables you to group different elements in your svg. So for instance, in the example above where I have all those little circles on the dialing wheel of the phone - I could use a group tag to group them together.

This is just a very short list of all the elements of an svg you can use. There are a lot more! See here. (I know, amazing.)

Styling your SVG

What the heck do you do with these elements?

The short answer is: EVERYTHING!. You can apply styles to each of the components of an SVG file in your CSS. Simple as that! In fact, you've probably already noticed that the svg code I pasted above had some inline styling. You can get rid of it or simply override it in your CSS. But how do you even know which path is what? I had no idea when I first saw svg code. I don't read path!

Follow these two simple steps and you'll be laughing:

  1. Inspect your SVG in Chrome or your broswer of choice Open your SVG file in the browser and use your developer tools to inspect the svg and its various components. Then, carefully apply classes to these parts, back in the text editor. For example, I have decided that I want to make the receiver of my phone blue instead of red. I look in my inspector to see what the code is for the receiver, and voila! I got it:

    <path class="receiver" fill="#FF3D7F" d="M183.053-73.814C172.811-90.883,63.57-97.711,46.501-97.711s-126.31,6.828-136.552,23.897
  2. Select your class and style your heart away! I added the class of "receiver" to my receiver path above in my svg element. Now I can go into my CSS file, select the receiver and add styles to it. You should also delete that ugly inline style that gives it a fill of #FF3D7F.

    .receiver { fill: darkorchid }

Tada! That's that. Now you have a "darkorchid" coloured phone receiver.

If you want to group a bunch of elements and colour them at the same time, follow the same procedure. Just 2 steps!

And let me blow your mind even more: here's a handy list of ALL the attributes you can apply to your SVGs!

Extra Tip: Change the width of your whole svg element to a percentage so that you make your icon responsive! (meaning it can adjust itself to any browser size)

How to animate your svg?

Just as easily as you styled it. Apply a class to any element you want to animate, and go wild!

I wanted to animate the receiver to look like it was ringing. So that was easy. I defined my animation keyframes and then applied the class of my animation name to my receiver element.

See it all in action!!!

Take a look at my Code Pen! You'll be able to see it all in action and you can even play around with the code yourself to practice all the steps and tips I shared.

Additional Resources

There is so much wonderful information out there on SVGs. I relied mainly on two articles that I found the most useful.
My thanks to them:

Chris Coyier in Using SVG


Noah Blon in How To Animate My SVGs

Once you've become the SVG animation whiz I know you'll be, please share your Code Pens with me!

Stay tuned for my next post about animating with HSL!

Have fun with SVGs!