Animating smooth color transitions
Video: Animating smooth color transitionsReally 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.
- Next steps
Viewers: in countries Watching now:
With robust and ever-increasing browser support, it is now possible to take advantage of expressive CSS3 capabilities across modern browsers. In this course, Joseph Lowery explores the possibilities of the new coding options, which animate well over 50 different properties automatically or interactively, and how they open the door to enhanced user experiences. This course covers the range from simple to complex transitions, including 2D and 3D transforms, and illustrates how transitions are expedited in various web authoring tools, as well as Dreamweaver. The course also contains a start-to-finish interactive slideshow project that allows you to practice and see the transitions and transforms immediately in action.
- Understanding transition basics
- Working with 2D and 3D transforms
- Animating color changes
- Fading objects
- Changing the size of page elements and fonts
- Transitioning multiple properties at the same time
- Understanding keyframes and animations
- Working in the z-index
- Crafting transitions in Dreamweaver
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.
There are currently no FAQs about CSS: Transitions and Transforms.