Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Animating smooth color transitions

From: CSS: Transitions and Transforms

Video: Animating smooth color transitions

Really smooth gradual animations on the web are such an anomaly, you may do a double take when you encounter one on a site. However, I think they can be really quite compelling, like the slowly changing header on this page. In this movie I'm going to show you how to put the @keyframe statement and animation properties we discussed in the second movie of this chapter to work and create this precise effect. If you're unfamiliar with the @keyframe statements and animation properties, please review that movie before continuing. All right! I have the main.css file from Chapter 3/ 03_03/_css folder of the exercise files open in my code editor, and I'm ready to start coding my @keyframe statements.

Animating smooth color transitions

Really smooth gradual animations on the web are such an anomaly, you may do a double take when you encounter one on a site. However, I think they can be really quite compelling, like the slowly changing header on this page. In this movie I'm going to show you how to put the @keyframe statement and animation properties we discussed in the second movie of this chapter to work and create this precise effect. If you're unfamiliar with the @keyframe statements and animation properties, please review that movie before continuing. All right! I have the main.css file from Chapter 3/ 03_03/_css folder of the exercise files open in my code editor, and I'm ready to start coding my @keyframe statements.

The first of which, we'll set up a range of color values. So I'm going to scroll all the way down to the bottom of the page just to have it placed correctly, create a little space here, and let's start out with our vendor-specific versions first, and the syntax here for an @keyframes statement that uses a vendor-specific prefix is to enter @ and then the vendor prefix, which in the first case is -webkit-, the name of the property keyframes, and then as you may remember, we need to enter a unique name for this animation.

I'm going to call this spotlightBG, and we'll start putting in our rule. Now I want to slowly rotate between five different background colors. So I'm going to enter in five different declarations, one for each of the background colors at a different point on the timeline. So let's start off right at the beginning at 0%, and we'll make this background color white. Now the other values I'm going to enter are all light pastels that I picked from actually working with Adobe Kuler, which you can find at kuler.adobe.com, a great resource.

Enter a new a line, and let's put in my 25%, and we'll start this one by triggering this background color change at 25% of the duration, and we will enter in our next color value, and you can see how this is going to go, so I'll copy the 25% line and paste it in again and change this value from 25% to 50%--so the halfway mark--and then change the color value, something slightly different, again, starting off with ff and then d4da.

Let's paste that in one more time, this time we'll move to 75%, and again, change the background color, this one is 99d2e4. One last time for pasting it in. Let's change the percent to 100% and the background color to d8cab4. Okay, so that is the keyframes statement for just webkit, now let's do the same thing for our three other browsers. I'll copy that entire rule and then paste it once, change the -webkit prefix to the Mozilla prefix, -moz, paste it again, change the prefix to the one for Opera, and finally paste it one last time and get rid of the prefix altogether.

Now let's set up the CSS rule that targets the div containing the Student Spotlight title and the artist's name. That's a div with the class of student, and we'll establish the original background color. This is the value that's actually going to be changing, and we want it to be the same as the color we start off with, which is white. Next, we want to enter in our animation rules. Again, we'll start with the -webkit vendor prefix, add the property name animation: and the first thing on our line of values that we want to enter in is the name of the animation, which is spotlightBG, followed by the duration.

If I were doing this for real, I would probably set the value really high like 40 or 50 seconds; however, for demonstration purposes, let's set it at 10 seconds. Now I want this to continuously repeat, so I'll enter in the keyword infinite, and I want it to bounce back and forth, so when it reaches 100%, it will then go to 75%, 50%, 25% and back to 0. So I enter in another keyword, alternate. Okay, so let's copy that line and paste it three times and then enter in our other vendor prefixes, -moz and -o and remove the vendor prefix for the final entry.

All right let's save the page, and let's head back to the browser. This is the final version. Now let's go to the version we've been working on, which is found in Chapter 3/03_03 and labeled spotlight.htm. Now you'll notice that at this point the background is not changing, but let me reload the page, and we start to see some change. Okay, it's not bad, but there are definitely a few moments where the contrast between the artist name and the background gets perilously close to one another.

So why don't we create another animation for the student-named text color to run parallel with our background animation and see if we can keep the content always readable. So back into the code editor. At the bottom we'll create a new rule, again, this is going to be an @keyframes setup, so let's start with @-webkit-keyframes and give it a unique name. In this case we'll just call it studentName, and here I think we only need three values, a beginning, middle, and end.

So we'll start with 0%, open our curly brace, put in color, and we'll start with a gray value, which is the original color used, which is d6d6d6; and then for the 50% mark, we'll change the color to a slightly darker gray, and at 100% mark let's make the color half of that, 333. Again, we'll need to copy this and do our vendor prefix versions, so let's paste it once, change -webkit to -moz, paste again, change -webkit to -o for Opera, and one final time and then remove the vendor prefix totally. Good! Now we're ready to build out the CSS rule for the paragraph tag within the student div.

So we enter our selector div.student space p for paragraph, start our curly braces, and we'll start it off with the same color that we began our animation, and now let's add in the animation itself. The name before our animation comes next, the duration, how we want it to repeat, which is infinitely, and the direction, which will be alternate. All right! Let's do that copy thing, and that vendor prefix replace thing. Let's save the page and take another look in the browser.

One more reload, and you can see the student name gradually changing, as well as the background changing. I think that's much better, and you could certainly tweak the values more to match your own designer's eye, but this is working for me. We've really just scratched the surface of the potential of CSS3's animation possibilities, but hopefully this colorful example will give you the tools and inspiration to start exploring.

Show transcript

This video is part of

Image for CSS: Transitions and Transforms
CSS: Transitions and Transforms

26 video lessons · 14123 viewers

Joseph Lowery
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.

Join now "Already a member? Log in

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: Transitions and Transforms.

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


OK
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?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

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:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.