Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now
Watching:

Animation basics


From:

CSS: Animations

with Val Head

Video: 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.
Expand all | Collapse all
  1. 4m 47s
    1. Welcome
      41s
    2. What you should know before watching this course
      58s
    3. Vendor prefixes and browser support
      2m 11s
    4. Using the exercise files
      57s
  2. 28m 16s
    1. Animation basics
      6m 32s
    2. Exploring animation delay and the fill-mode property
      6m 6s
    3. Working with animation direction
      4m 49s
    4. Understanding easing
      8m 47s
    5. Challenge: Adding keyframes to our animation
      49s
    6. Solution: Adding keyframes to our animation
      1m 13s
  3. 47m 37s
    1. Creating a seamlessly looping animation
      6m 27s
    2. Animating an element into place
      5m 43s
    3. Playing and pausing an animation on hover
      6m 20s
    4. Animating 3D transforms
      8m 6s
    5. Preparing a sprite image for animation
      4m 55s
    6. Animating the sprite image with steps
      5m 34s
    7. Chaining multiple animations on one element
      8m 2s
    8. Challenge: Adding a third animation to the chain
      46s
    9. Solution: Adding a third animation to the chain
      1m 44s
  4. 19m 54s
    1. A preview of what we'll be creating
      1m 10s
    2. Setting up the HTML structure
      1m 48s
    3. Adding the header animation
      5m 57s
    4. Animating the data
      6m 8s
    5. Adding interactivity with JavaScript
      4m 51s
  5. 12m 44s
    1. Why and when to use CSS animations
      4m 50s
    2. Organizing animation code for semantics and fallbacks
      4m 51s
    3. Crafting effective animations
      3m 3s
  6. 3m 0s
    1. Helpful online tools for creating and building animations
      3m 0s
  7. 34s
    1. Next steps
      34s

please wait ...
Watch the Online Video Course CSS: Animations
1h 56m Intermediate Feb 14, 2013

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.

Topics include:
  • Understanding delay and fill-mode
  • Dictating an animation's direction
  • Using easing
  • Adding keyframes
  • Looping an animation
  • Chaining multiple animations on one element
  • Setting up the HTML structure
  • Adding interactivity with JavaScript
Subject:
Web
Software:
CSS
Author:
Val Head

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.

There are currently no FAQs about CSS: Animations.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

* Estimated file size

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS: Animations.

Return to your organization's learning portal to continue training, or close this page.


OK

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.