Viewers: in countries Watching now:
This course presents a short series of CSS animation techniques, such as looping, playing, and pausing, and puts them together in a small project: an animated infographic. Author Val Head also addresses using CSS preprocessors, adding transitions, handling vendor prefixes, and understanding the best uses for CSS animations. Plus, discover how to measure the performance and current level of browser support for CSS animations and how we can expect the technology to change.
We've been creating and applying single animations to create our effects so far. But in practice, it's quite likely that you'll run into a project where you need multiple animations to create the movement you're after. Animating a character or demonstrating a software feature for instance, or even creating a longer playing effect will require more than one animation to complete it. We'll be animating a molecule graphic in isolation for this example, but this chaining technique can be used as part of any kind of linear animated content. We'll have our molecule rolling from the left and then scale up and back into its original position, and in the end it will look a little something like this.
Let's go to Coda and take a look at the HTML behind this example. Our HTML is pretty simple. We have a wrapper div and then one div with the class of mol for molecule, and that's the one we'll be using for our animation and the graphic. If we take a look at our starting styles in our CSS, they're also pretty simple. We have a background image and a width and height assigned to our mol class, or our molecule. And we also have some general positioning applied to it before we add our animation. I'm going to use transform to create the roll-in animation first.
I'll be affecting both the translation and the rotation of our molecule. So let's start writing our animation with our keyframes block as usual. I'm going to name this first animation roll-in, since we'll be making it roll in. For our first keyframe at 0%, we will set the translation off to the left so it starts off to the left, and we'll also set the rotation to 0 degrees. So we'll translate on the X axis -200 pixels so that will move our molecule off to the left for the start of our animation, and we'll add an additional transform of a rotation of 0 degrees.
To complete our roll-in animation, we'll add a 100% keyframe. We'll set the same transform properties to different values. So I'm just going to copy this line here, and we're going to set our translateX to 0 pixels, which will have our molecule and up with zero translation on the X axis, which means it will be in its normal position, and we're also going to add a rotation of 360 degrees. Technically, being rotated 360 degrees looks exactly the same as being rotated 0 degrees, but our animation will actually show all those rotation points between 0 and 360 as it plays.
Next, let's define the keyframes for our second animation, and we can do that right below the first in a similar fashion. I'm going to name this animation scale-up, and we'll complete that keyframe block. Our first keyframe will be at 0% of course, and I'm going to start with our scale set to 1, which is the same as 100%. Because for whatever reason, scale works on a scale of zero to 1 as opposed to percentages as you might expect. To give our molecule a bit of spring to it, I'll set the scale to 1.2 at the 25% keyframe.
So you can actually scale things above 100%. You just need to mark it as 1.2 or 3 or whatever amount of scale you want to add to it. Then I'll go back to a scale of 1 at the 60% keyframe, and I'll just copy and paste these to save myself a little typing. Finally, at the 100% keyframe, I'll assign the scale of 1.2 once again. This means our molecule will end up at the end of our animation slightly bigger than the original image. But that's okay, it's just a small scale up, so we shouldn't see any degradation in the image.
In our scale-up animation, I've chosen these percentages and scale values. More or less, it's just what looked best. This is the kind of thing where you might want to set up an animation and then adjust the numbers later to see what really looks best to you. So now with our keyframes all defined, let's assign our animations to our molecule, so we can see how things are shaping up. We'll go back up to our molecule class to assign the animation here. When we assign two animations to one element, we need to use commas to separate the values associated with each animation. So when we're defining the animation-name property, we're actually going to assign two animations.
In the first animation we want to see is the roll-in animation. That may have a comma and the name of the second animation we'd like to see. I know we've started using the animation shorthand in a few of the tutorials. But for this one, I'm going to keep them separate so we can really get a feel for how we're adding two animations. For all the remaining animation properties we'll define for this class, we'll use commas to separate the values for the two animations. So our animation duration would look like this, first, 1 second for our roll-in animation and then 0.75 seconds for our scale-up animation.
Keeping with that trend we'll add our animation-delay. In this case, we're going to have zero second as the animation-delay for our roll-in animation because we want that to play right away. But we don't want the scale-up animation to play until roll-in is done. So we'll set the delay on our scale-up animation to be the same amount of time that our roll-in animation takes. So add a delay of 1 second to our scale-up animation, which means it will wait for that 1 second, and during that 1 second our roll-in animation will take place.
It's a nice little trick for how we chain these animations together on one element. And then we'll add our animation iteration count. And for both of these, we only wanted to play once, because this is kind of a chained together entrance for a molecule. So we're just going to have 1 and 1. And when the values for two animations are the same, we can actually just list it once and then CSS will just continue to use for the next animation. But personally, I like to leave the two comma-separated values because it's a lot easier for me to follow later. But if you prefer just seeing one number there, you can go ahead and do it that way too.
And then as our very last property, we'll add our animation-fill-mode, and I'm going to set this to forward for both. By using forwards for both animations, this means the molecule will keep the 100% keyframe styles at the end of the roll-in animation to be ready for our scale-up. And then once our scale-up animation is done, our molecule will maintain the styles from the 100% keyframe of our scale up animation, and it will end up just how we wanted it. With all these animation properties set, let's save our file and preview what we have so far on our browser.
So if we refresh, we see our roll-in animation taking about one second and then our scale-up animation happening in about 0.75 seconds, and they're each happening one right after each other because of the delay we set up on our scale in animation. Everything is looking pretty good but there's one thing I wanted to add. We didn't define an animation timing function for either of these animations explicitly, which means they're both using the default of ease. But I think I'd like to change that to add just a little bit more style to this animation. So we're going to add one more property to our animation which should be our animation-timing-function.
So we'll add our animation-timing-function here, and for the roll-in, I want it to have a little bit of momentum to kind of start slow and end faster. So it has its momentum going into a scale-up. So I'm going to use ease-in here, which we know will start slow and then accelerate as it goes. And then since our scale-up animation has so many keyframes, the keyframes themselves are kind of indicating a bit of easing on its own. So I'm going to set the animation-timing- function for our scale-up animation to linear, and we'll let the keyframes take care of indicating that easing for us.
We'll go back to our browser and preview our animation again, and it kind of flowed just a bit better. So now that I'm happy with that, once again, our last but not least step is to add in our vendor prefixes so this animation will play in non-WebKit browsers. So it will just take a second to do that. So now with all our vendor prefixes in place, our file is a lot longer, but it's also a much more compatible with non-WebKit browsers for the time being. Now that you have a chained animation to start playing around with, why not go back and add some different easing or maybe change the duration or some other of the animation properties? You might be surprised how much a small change in properties can really change the look and feel of an animation, plus it's really fun to play with.
There are currently no FAQs about CSS: Animations.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.