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

Adding the header animation


From:

CSS: Animations

with Val Head

Video: Adding the header animation

For our first animation of this project, we'll animate the coffee cup in our header. This animation is a lot like the ones we've done previously in the course, so it's a good one to start with. Let's take a look at our final version first to get a feel for what we're creating. I'll just refresh the page and then we'll see after a short delay, our coffee cup slides into the middle of our frame. It holds there for a moment and then slides back out in the other direction. So that's the animation we'll be creating in this tutorial. If we go to Coda and just look at our HTML briefly, we'll see that these are the two images we'll be dealing with for this animation.
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

Adding the header animation

For our first animation of this project, we'll animate the coffee cup in our header. This animation is a lot like the ones we've done previously in the course, so it's a good one to start with. Let's take a look at our final version first to get a feel for what we're creating. I'll just refresh the page and then we'll see after a short delay, our coffee cup slides into the middle of our frame. It holds there for a moment and then slides back out in the other direction. So that's the animation we'll be creating in this tutorial. If we go to Coda and just look at our HTML briefly, we'll see that these are the two images we'll be dealing with for this animation.

And that's our coffee cup image and the aperture image that's overlaying our coffee cup. In my initial CSS positioning, I placed the coffee cup below the aperture image, so that way it's showing up behind it. The reason you can see it when it's in the middle is our aperture image is actually transparent in the middle. So that's the kind of trick to this animation. The two images layered overtop and the top one is transparent in the middle. So let's go to our styles.css, and we'll head all the way to the bottom of the file and start our animation section. The first thing we're going to start with as usual to create our keyframe is our @keyframes blog.

I'm going to call this animation cup-in because it's the coffee cup moving in. That seems like it will work. Now we want our coffee cup to slide in somewhat fast and then stay in the middle and just kind of hold there for a little bit before it makes its exit. If you've ever worked with animation software like After Effects or similar, you might have called that a hold keyframe or a hold. There aren't really hold keyframes in CSS, but I'll show you how we can get the same idea. So let's start with the 0% keyframe, and we'll have a transform with a translate of 0, so that will start our coffee cup off exactly where it is right now.

At about 5%, because we want this animation at least the entrance to happen pretty quickly, let's add another transform and translate our coffee cup in to about 103 pixels. Now I've come up with these numbers just by adjusting things and playing it around it first, so that's why I'm guessing numbers like 103. I've actually checked this out before. Now we want our coffee cup to kind of hold in that middle at that 103 pixels position. And in order to do that, what we'll actually do is create another keyframe that's exactly the same as this 5% keyframe.

So I'm just going to copy this and paste it below and then set this to 80%. This might look a little bit strange to have a 0%, 5%, and 80% keyframe, where 5% and 80% are exactly the same. But essentially we're creating an animation with zero change, so our coffee cup will stay static in one position between 5% and 80%. That's a handy little trick. At the 90% keyframe, we want our coffee cup to leave the frame off to the right. So we're going to transform it again just a little further out.

We're going to translate it here to 240 pixels. That happens to be so far off to the right that our coffee cup image will be covered up by the opaque portion of our aperture image. Then one last keyframe we'll be adding is our 100% keyframe, and we're going to repeat this whole pattern again, because what we want is for our coffee cup to go over to the right and then stay there just for a little bit out of sight before it comes back in again. So we will once again do a repetitive keyframe and make our 100% keyframe the same position as our 90% to create a second sort of hold at the end of our animation.

Now the nice thing is with all these repetitive keyframes is we can actually make our keyframe notation a lot more compact by doing a couple of things. Since both our 5% and 80% keyframes are the same, we can actually notate this by a comma separating them. So adding 5%, 80% with one declaration means we can eliminate this 80% line here and then we can do the same for the 90% and 100%. We can add a comma, 100%, and then remove our 100% keyframe line. Now we've just done this animation with even fewer keyframes, and we don't had to repeat that over and over again.

And one other thing we can remove if we'd like to is when we have a 0% keyframe with a translation of nothing. That's essentially using the same styles that are already on our coffee cup, and if we delete this line, CSS will automatically take the existing styles in our coffee cup and make that into a 0% keyframe, even though we haven't explicitly stated that. So that's a nice little shortcut as well. With our keyframes in place, let's go and assign this animation to our coffee cup. I should probably fix that so we have the proper syntax. The semicolon should be outside of the brackets of my translate, because the semicolons are kind of like the end of the sentence when you've created your property, not in the middle of your sentence.

Our coffee cup is in our header and a div with a classic opening, and it has the classic cup. So we're just going to scroll up a little bit 'til we get there, and here we are. Opening cup is exactly the image we want to deal with. So we're going to add our animation properties here, and of course we're going to assign it, the cup-in animation that we just created. I'm going to have this animation take 10 seconds, because we want it to be kind of slow, it's going to repeat a lot. I'll set my animation- timing-function to ease-in. I'm going to have this repeat infinitely, and I'm also going to add a delay of 5 seconds, and we'll have our animation-fill-mode set to forwards.

So with that all in place, we can save our file and take a look at what we've created. We'll refresh the page. And then after a short, approximately 5-second delay, our coffee cup will slide in, it will hold there for a little bit, that's between our 5% and 80% keyframe, and then it will slide back out once it hits 80%, and there we go, and it will hold off to the right for just a short while and then come back and repeat. Now if you notice, our 5-second delay only occurred the first time the animation played. After that, the delay we're seeing is really just that fake delay we've created between our 90% and 100% keyframe.

So when infinitely repeating animations, the delay only takes place the first time. Next, we'll move on to animating the data section of our infographic.

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.