Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this tutorial we'll be creating an infinite looping animation. CSS is great for this type of animation because it makes it possible to set up an infinite loop with less code than most other methods. We'll be creating a couple of animated clouds that drift across the sky, just like the ones you're looking at here. We'll also look at the animation shorthand syntax and the idea of sharing one animation across multiple elements. So first, let's go into Coda and take a look at the HTML behind this example. You can see our setup is pretty simple. We have a div with the class of sky, and within that we have two divs with the class of cloud.
Note that each cloud div has two classes, one that is shared between the two of them, and one that's unique to each. We'll use that unique class to create slight variations on the animation for each cloud and the shared class for all the common styles between the two. If we go into our CSS and take a look at our initial styles, you can see we have the shared styles for the clouds within a cloud class and then below that we have two classes called fcloud01 and fcloud02 which have the individual properties for each cloud. The main things that are different between the two clouds is a slightly staggered top position, one is at 100, one is at 240 pixels and a slightly staggered zed or z-index position, depending on which way you like to say that letter.
And basically we're just staggering them one overlapping the other as well. So now that we have our initial styles all set, let's get to adding some animation. First, we'll go to the bottom of our style sheet and add our @keyframes block. I'm going to name this animation drift, since that's pretty much how clouds work. And now we'll get to writing our keyframes. Since in this animation we're basically going to be moving these clouds from somewhere far off to the left to somewhere far off to the right so they disappear on both ends, I'm going to use the keywords of from and to, to define my keyframes because that's exactly what were doing.
So we're going to start out from a position far off to the left, and for that I'm going to use a translation of -250 pixels which will have us far off to the left where we can't see our clouds. So I've set our translation along the X axis to -255 pixels and where I got that number is from knowing that our clouds are 250 pixels wide, so -255 pixels will definitely be so far off to the left we won't be able to see our clouds. Then we'll be animating to a place that's far off to the right.
So I'm just going to copy and paste this since we'll use translate for our to keyframe as well, and this time I'm going to translate to a very high number that I know is all the way off to the right, and that happens to be 1350 pixels, so we're covering a lot of ground here. Now that we have our keyframes set, we can assign this animation to our clouds. We're going to use the same animation for both but adjust the properties in each individual class to make them behave a little differently. So let's start with the fcloud01, and we'll add some animation properties here. First, we're going to assign it the animation of drift since that's the one we just defined.
We'll add an animation duration of 25 seconds because clouds tend not to move very fast, and then we will add an animation timing function of linear, and I'm picking linear here because I want my clouds to move at a constant pace. That may not be realistic of actual clouds, but I prefer the look of it in this case so I'm sticking with it. And we'll add one more bit to our property, which is the iteration-count, and we're going to set this one to infinite, and that's where we're going to get our endlessly looping behavior.
And this animation will repeat over and over infinitely, basically forever. Now that we're starting to get the hang of CSS animation syntax, I think we're ready to start working with the shorthand. We can actually summarize the last four lines we just wrote into one line using the animation syntax shorthand. And I'll show you how. The animation syntax shorthand works like this, we start listing out the name of our animation, the duration we'd like it to take, the timing function that it should use, any delay that might apply, an iteration-count, any direction, and then any fill-mode that may be applied to our animation as well.
If you have trouble remembering the order for this shorthand you can add a comment to your CSS to remind you just like this one. If the shorthand doesn't actually help you work faster, it's really not doing you much of any good. So whatever you have to do to remember the order and what goes in there, definitely do it. So, let's rewrite those last four lines into one line. We'll start with the animation property, and then we will add our animation name which is drift, then our duration of 25 seconds, then our animation timing function of linear, we don't have a delay so we can skip it, and this going to be smart enough to know what we're doing, and then we'll add our animation iteration count of infinite.
And with that, we can delete these top four lines since that last one is doing all the same work. You can see this will really come in handy when you get to adding your vendor prefixes because that's three less lines we need to prefix, and that's a lot of copying and pasting. So now let's go on to our second cloud. We'll copy the animation properties from our first one since we'll just be making a few adjustments and paste it into fcloud02. We'll change a couple of things here for our second cloud. The first thing that we're going to do is increase the duration for our second cloud. So we'll change that to 35 seconds, and this means that our second cloud will be using the same keyframes as the first but will take 10 seconds longer to get through them, so it'll be moving at a slower pace.
In addition to that, I'm going to add a delay to this animation, and according to our shorthand syntax I'm going to add that right here between linear and infinite, and I'm going to add a 10-second delay to this animation. So this way, not only will it be taking longer to do the same keyframes as the first cloud, it will start a little bit later. And last but not least, I'm going to add a fill-mode to the second cloud of backwards and the reason I'm doing that is so during our 10-second delay our second cloud will maintain the position from our first from keyframe, and that way it'll stay off to the left for that 10-second delay before it starts moving.
And one last thing before we preview our animation, let's add all our vendor prefixes so we can look at this in any browser, not just one with WebKit. Now with all our vendor prefixes in place, we can save our file and go back to our browser to preview what we've done. So if we refresh our page, our first cloud starts coming across from the left, going at a very nice slow cloud-like pace, and approximately 10 seconds later our second cloud will show up, going a little bit slower but using the same keyframes as the first. Being able to reuse animations on multiple elements like this is a nice feature of CSS animations.
You could have multiple elements animating with the same keyframes, but differing the properties just a little bit can make things a whole lot more interesting. So it's a good trick to add to your CSS animations.
Get unlimited access to all courses for just $25/month.Become a member
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.
Your file was successfully uploaded.