Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From \$16.50/m

# How to Animate Festive SVG Icons With CSS

Difficulty:IntermediateLength:LongLanguages:

'Tis the season, so in this tutorial, I'll walk through creating some CSS animated, holiday-themed, SVG icons. There are some great icons on Iconmelon, a site which hosts many free vector icon sets for you to sink your teeth into. The icons I'm using come courtesy of designer Sam Jones. So grab yourself a cup of eggnog, pull your laptop up to the yule log, and let's gets started!

### SVG and the Web

If you're interested in using SVG on the web, icons are a great place to start. SVGs are flexible, resolution independent, and light weight, so icons naturally lend themselves to the vector format. Plus, just like HTML, SVGs can easily be styled with CSS, which includes CSS3 animation. Adding a dash of interactivity with animation to your icons can help create a delightful experience for your users and also add context about what an icon represents.

For an introduction on SVG and the web, take a look at SVG Files: From Illustrator to the Web.

Note: The following demos use cutting edge technologies that, as of the time of this writing, are not supported in some browsers such as Internet Explorer. If you're following along with the tutorial, it's best if you use Chrome or Safari. Mozilla support is perfectly possible with the appropriate property prefixes. You can certainly expect support for these technologies to improve in the future.

Additionally: In this article I've omitted some necessary browser prefixes from some CSS properties for concision and readability. Check out the prefixfree library by Lea Verou if you'd like to write prefix free CSS easily. You could also try creating your demos on Codepen, which can be easily configured to use prefixfree.

## Preparing SVG Code for Editing

One of the biggest knocks on SVG is that the code is difficult to deal with. The SVG code that is exported by Illustrator, my vector graphics editor of choice, is pretty unreadable at first glance. Inkscape actually does a better job of exporting SVG in this respect, but I've found that simplifying and formatting the code can go a long way towards making the code easier to read and work with.

Here's the SVG code for the first animation example I'll demonstrate, a blinking Christmas light.

This code was exported from my vector editing tool, Adobe Illustrator.  It's almost unreadable at first glance.  Here's that same markup simplified:

I've significantly simplified this xml by removing a lot of the extra markup that the program outputs by default. My base SVG element contains the following:

• A class: svg-light. I've used the svg- prefix to easily target elements inside of a specific SVG.
• The viewbox property.  The value of the viewbox property defines the aspect ratio of the document and is equivalent to the size of the artboard in Illustrator.
• The xmnls property.  This property defines the XML namespace of the SVG and helps some user agents understand the markup.

To the elements nested inside the SVG which define the shapes that make up the image, such as the pathscircles and rects, I've applied classes inline with their opening tag. All inline properties of the element, such as their coordinates, I've broken to new lines. In addition, I've indented all of inner elements under the SVG base tag.

All this work is for a reason. First of all, it makes the code much easier to read. Second of all, in my code editor of choice, Sublime Text 3, I can easily fold individual SVG elements or entire SVGs while the class names still help retain context about what those elements are.

## Christmas Lights

Alright, let's get into the holiday spirit and animate a blinking Christmas light! Here's what we'll be looking to achieve:

See the Pen Light by Noah Blon (@noahblon) on CodePen

What I want to do is animate the path element which I've given a class of bulb in the SVG.

With CSS, I'll give the bulb a fill color and define its animation properties.

The fill property allows us to set an SVG element's color. When possible, I like to use HSL (hue, saturation, lightness) to define color values because it is very intuitive to work with. Here, I've chosen a blue color (hue 204), and kept the lightness value low, 23%, meaning we'll have a dark blue color.

I've set the bulb to be animated by a keyframe animation called glow-blue. The duration of the animation is 1 second. The animation iterates infinitely, meaning it will run forever. The timing of the animation is eased at the beginning and end of the keyframes, creating a smoother looking transition at the start and end points of the animation. Finally, the animation is set to alternate, meaning it will go back and forth from the beginning to the end and back again.

Tip: You may know there is a shorthand syntax for CSS animation rules, but I generally prefer to split the rules out to make them easier to understand, modify, and share if chaining multiple animations.

Now, I'll define the glow-blue keyframe animation:

Here I've set the starting and and ending fill color of the element to which this animation is applied. The only value that is changing is the lightness of the color (the "l" bit in hsl), which means the light will animate from a darker to lighter version of the same hue. That's the intuitive syntax that HSL provides.

Because I've defined the animation to alternate infinitely, the light will alternate from dark to light and never stop. It will therefore appear to blink.

### Animating multiple lights

OK, now let's channel our inner Clark W. Griswold and animate a whole load of Christmas lights. This is what I'll create:

See the Pen 540257e4a8b727e435c8e4033602ebb0 by Noah Blon (@noahblon) on CodePen

I've created five differently colored lights. Each light is wrapped in a div with a width of 20%. These divs are then floated left so they appear inline with each other. As I haven't set the SVG to be a set height or width and retained the viewbox property, the SVG is fluid to the size of its parent without losing its aspect ratio or quality. Just one of the huge strengths of SVG.

For each SVG, I've extended the base svg-light class with a color suffix (for example svg-light--red). Here's an excerpt of the rules I've used to animate the many lights:

The basic animation properties are still applied to the base class of svg-light so that they can be shared between all of the lights. For each color variation, I've created a different keyframe animation such as glow-red or glow-blue and given them different animation delays and durations. In this way, the lights will blink with their appropriate color, and all not blink at the same time.

## Rudolph the Red-Nosed Reindeer

I'll use the same concepts I've described above to animate the most famous reindeer of all, Rudolph. Here's the end result:

See the Pen SVG Rudolph Icon Animated with CSS by Noah Blon (@noahblon) on CodePen

First, I'll animate his glowing red nose:

This looks pretty similar to our Christmas light. In the keyframe animation, I am animating to 50% and then to 100%. As the animation duration is six seconds, this means that in three seconds, the nose will become lighter red and remain that color for another three seconds to the end of the animation. Also, because I've set the animation to alternate, it will now run from the end to the beginning. Therefore the nose will stay full red for another three seconds before finally darkening in the last three seconds. Understanding the interplay between the keyframe percentages and the animation definitions, such as the duration, is key to creating effective CSS animations.

I've also applied a transition to the highlight on Rudy's nose:

Here I'm animating a different SVG property, the fill-opacity. This property takes a value between 0 and 1; 0 being fully transparent, 1 being fully opaque. Finally, I'll bring a bit more life to Rudy by making his eyes blink:

I've created an animation called blink. This animation runs for eight seconds and never stops. I'm not alternating the animation this time, instead the animation will run to the end and then restart from the beginning.

In the keyframes, the blinking animation is created by manipulating the scale of the eyes on the X and Y axes. For example, at the 1% mark, the eyes are scaled to 1.3 their size on the X axis and .1 times their normal size on the Y axis. In other words, they get a little bit wider and a lot shorter. One percent of the animation duration later, the scale of the eyes returns to normal. Therefore the scale change happens extremely quickly; 8 seconds / 100 = 8 hundredths of a second.

A key component of using transform to animate SVG elements is to set the elements transform-origin. In most cases, in order for a transform to be applied properly in an SVG, we must define the origin of the transform coordinate to be the center of the element. Unlike that of an HTML element, by default the transform origin of an SVG element is its top left corner, the (0,0) coordinate. By setting transform-origin to (50%, 50%), the origins for the X and Y axis of the transform will be at the center of the element, and transforms will then be applied correctly.

Note: Firefox has issues when it comes to transform origin. Unfortunately, setting the transform-origin to 50% actually resets the X/Y coordinates of the element. One work-around is to transform the element back to its original position.

## Ding Dong Merrily on High

For this demo, I'll animate a bell ringing like this:

See the Pen 63cdc3e8e785028deb35132d889b6090 by Noah Blon (@noahblon) on CodePen

Let's take a look at an excerpt of the SVG markup:

I've wrapped the bell outline and fills with a <g> (or group) tag. This tag is very useful for organizing elements, similar to how you might wrap several HTML elements with a <div>. Animations can be applied to a group. Since in this case, I want both the fills and outline of the bell to rotate, I can just rotate the group rather than each element individually.

This CSS should be familiar by now, but there are a couple of new things happening. First, in the keyframe animation, the bell is being transformed, but instead of transform scale I'm transforming the rotation value of the element. This animation makes the bell oscillate between 27 and -27 degrees.

The other new thing I've done is use a negative value for the animation-delay. A negative value causes the animation to start that amount of time into the animation. In this case, instead of the animation starting after a 1.5 second delay, the animation will actually start 1.5 seconds into the animation. By doing this, the bell will start at the neutral position, which it is at at 1.5 seconds into the animation, rather than at the rotated position, which it is at 0 seconds into the animation.

I've applied a similar animation to the group containing the bell's striker elements. This time, however, I'm translating, or moving, the elements along the X-axis rather than rotating, so the striker will oscillate to the left and right.

## Let It Snow! Let It Snow! Let It Snow!

For this animation, I'll animate snow falling inside a snow globe.

See the Pen 2addb5f98c757d61cec87bdcacb5870d by Noah Blon (@noahblon) on CodePen

I've created a bunch of circle elements to represent snow, and I've placed these elements around the perimeter of the snowglobe, wrapping them in a <g> tag.

The snow is not visible outside of the snowglobe because I've applied a clipping path to the group containing the snow. A clipping path is a shape that can be applied to SVG elements, masking others within it. In our globe example, I've created a circular clipping path which covers the interior of the globe.

The <clipPath> tag is our masking element, to effectuate it we must give it an ID.

I've wrapped the clipping path in a defs tag. SVG allows us to create elements which we can reuse later, such as clipping paths, filters, or shapes which we can stamp out. It is best practice to wrap definitions such as clipping paths in a defs tag so the SVG can be more easily read.

To apply the clipping path, we reference its url, in this case its ID preceded by a hash symbol, in the inline clipping path property of an SVG element:

The snow is now only visible inside of that circular clipping path.

The snow is animated with the same techniques I've described earlier; translated along the Y-axis and the fill-opacity approaching zero as the animation ends.

Yes, CSS3 selectors work on SVG elements! Here, I've given each snow element a different delay so they won't all fall at once.

## Season's Greetings

Finally, I'll animate some SVG-based text to achieve a result like this:

See the Pen Message by Noah Blon (@noahblon) on CodePen

SVG provides us with the ability to apply strokes to SVG elements with CSS. For my last animation, I'll show you how to progressively draw an element's stroke.

To achieve this animation, I've used the following stroke properties:

• stroke-width: the stroke's width. This is relative to the size of the SVG and therefore responsive.
• stroke: the stroke's color.
• stroke-dasharray: defines a dashed stroke. An array of alternating values define the length of the drawn parts of the dashed line and the empty space between dashes.
• stroke-dashoffset: defines where the stroke starts in relation to the length of the path.

Setting up the stroked text requires some work in a graphics editor. My workflow is as follows:

1. Create a text element.
2. Convert that text to vector paths.
3. Merge those paths into a compound path.

Here's my CSS to set up the animation:

I've defined the stroke-dasharray property to be the length of the total path (you can find this value in Illustrator under the Document Info Palette using the Object option). Then, I've given the stroke-dashoffset a value of the total length of the path, which pushes the entire stroke out of visibility. Then, by keyframe animating the stroke-dashoffset property, the stroke will progressively draw on screen.

I've also set the text to fade in after the line drawing has completed, using the fill-opacity property similar to what I did earlier with the snowglobe animation.

## Going Further with SVG and CSS

In this tutorial, I've demonstrated that with CSS and SVG, you can create some pretty effective animations. If you want to get stuck in with all of the code, as well as some additional animations, you can download the examples from Github or checkout my collection on Codepen.

If you want to get more advanced, you could explore controlling CSS animations with JavaScript or supplementing your CSS animations with more complex JavaScript animation, such as that supported by the amazing SVG JS library Snap SVG.

Thanks for following along with this tutorial, I can't wait to see the creations you come up with!