Learn CSS Animation Basics
To get started, let's take a look at the basic ingredients we need to create an animation in CSS. We'll create a simple animation of a unicycle rider riding across the screen to demonstrate the main components of a CSS animation. So we'll be creating an animation that will look a little like this. If we move over to Coda, we can take a look at the HTML behind this example. Coda is the text editor I'll be using for all the examples throughout this course. You can use any text editor you prefer, they will all do pretty much the much the same thing, but I like to use Coda, because it has a lot of code hinting and other tricks that help me get things done quickly.
So the HTML behind this example is pretty simple. We have a div with a class wrapper and then our unicycle rider image within that div. If we move over to look at our CSS file, we also have some basic styles in place. We have some positioning applied to our image to make sure it starts where we want it to, and a few margins and such to keep things away from the very edges of the page. Really not a whole lot yet. So let's get started to adding some animation. There are two main parts to any CSS animation. The first is defining the animation and the second is assigning it to the HTML element or elements that we'll be using in.
You can do this in any order, but I prefer to define the animation first and then assign it. To me that just makes more sense, but the order doesn't really matter. We'll start our CSS animation by defining its keyframes using the @-keyframes rule. Keyframes are essentially a list describing what should happen over the course of the animation. We'll define the values for the properties that we'll be animating at various points during the animation. Any property that we want to have changed over the course of one cycle of the animation needs to be listed in our keyframes. So let's take a look at how we write out these keyframes.
We'll go back to our CSS file, and we will start by writing our keyframes block. And our keyframes block works very similar to any other CSS rule or anything within the curly brackets is what's considered part of that block. To complete this @-keyframes block, we also need to give our animation a name. I'm going to call these animation rides, since we have a man riding in unicycle. That seems to make sense. There are few options available to us for how we can define each keyframe within our @-keyframe block. My personal favorite for animations that are going to move from one place to another, like the one we're creating here are the keyframes of from and to.
And these work just like they sound. You define where to animate from and where to animate to. It's really easy to remember and exactly what we want to do here. So for this animation, we'll be moving our unicycle rider from its current position so we'll start out with a translation of zero, which essentially means we are changing nothing and starting him exactly from where he is right now and then we will animate him to a position that is 400 pixels further to the right to get him across the screen. So now our to keyframe will translate our unicycle rider 400 pixels over to the right.
So by translating 400 pixels on the X axis, we'll be moving to the right by 400 pixels. This is essentially the same as if we had assigned a new left position of 420 pixels to our unicycle rider, because you'll see he started at the left position of 20, and then we're moving him 400 pixels to the right. But generally using translate to move things around, as opposed to animating the left and top properties is considered to get you better performance. A lot of community developers have detailed how transforms can take advantage of things like sub-pixel rendering and hardware acceleration where top and left cannot.
So it's a good idea to stick with translate to move things around. There's also little chance you would ever use translate to position an element while laying out a page. So using translate instead of top and left in our animations means there's less chance we'll accidentally override existing styles. So now that we have our animation defined, the next step is to assign this animation to one of our HTML elements. We're going to assign this animation to our image of our unicycle rider which we know has the class of cycle. So we'll go up here into this class and add a few more properties. The first additional property we're going to add is the animation name.
In the animation name we're going to assign it is ride, since that's where we just spent all the time making keyframes for. This rule tells our image which set of keyframes to take on. The second property we want to add is animation duration. Our keyframes have defined what will change over the course of our animation, but there's no indication of how long this will take until we define this animation duration property. I'm going to set it to 2 seconds to have our unicycle rider kind of moving at a-- I guess a relaxed unicycle pace. If we preview our files right now, you'll see we have some animation happening.
So let's save our CSS and go back over to our browser and see what's happening so far. So if we refresh our page, we can see our animations starts happening as soon as we refresh. Just like our keyframes are still there, our unicycle rider image moves across the screen to the right by 400 pixels, and it does it nice and smoothly. We've just set the minimum needed to create a CSS animation. However, there are two other properties I prefer to define explicitly for all my animations, because it makes my code easier to follow. So let's go back to our CSS in Coda and just add a couple more properties to our cycle class.
The first is the animation timing function. This property has a default value if you don't set one explicitly, but I prefer to set this property for all my animations because it has such an impact on the feel of the animation. We'll discuss this in more detail in later tutorials, but for now I'm going to set our unicycle rider's timing function to ease-in. This will have our unicycle rider starting at slow and then speed up as he gets closer to the end of his movement. That will make it look like he's just kind of maybe pedaling a little harder near the end or something like that.
The second property I like to always include is the animation iteration count. The animation iteration count determines how many times your animation will play. As we've just seen, if you don't define this the default is 1. And that's really how many times I would like this to play right now. So I'm going to set it to 1, even though that's also the default. But I like to type that explicitly so it's easier to look at my code and see how many times my animation is repeating. If I leave it out, I find it harder to remember. So now that we've added those few properties, let's save our CSS and go back and preview our animation one last time. So if we refresh the page, you'll see our unicycle rider starts out slow and then speeds up at the end, which is reflecting our ease-in animation timing function.
So now that we have the animation looking how we like it, let's go back to Coda and add one last thing. We're going to need to add in all our vendor prefixes so this animation will play in non-WebKit browsers too. Okay, so now that we have all our vendor prefixes in place, we'll be able to preview this animation in a non-WebKit browser that supports animation as well. Now we have the basics in place for our first CSS animation. Our unicycle rider moves across the screen smoothly and just how we expected. Next, we'll look at some additional properties that will let us fine tune this animation for even more control.
CSS: Flexbox First Look186,087 Views
CSS: Frameworks and Grids247,593 Views
CSS: Advanced Typographic Techniques83,198 Views
Design the Web: Graphics and CSS Pseudo-Elements53,248 Views
CSS: Visual Optimization132,763 Views
PHP with MySQL Essential Training4,259,922 Views
HTML Essential Training3,794,051 Views
Git Essential Training2,843,596 Views
CSS: Page Layouts1,735,726 Views