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

Animating color changes

From: CSS: Transitions and Transforms

Video: Animating color changes

Adding a transition to a color change is very straightforward to implement and imparts a sense of dynamism to the page. Let me show you two examples. I have opened the request.htm file. Now if hover over the links in the right sidebar, you'll see a very--some might say too subtle effect, where a background diagonal pattern appears. Let's bump that up a little. First I'm going to need to find the controlling CSS rule. So I'll enable my Chrome Developer Tools by going to View > Developer > Developer Tools.

Animating color changes

Adding a transition to a color change is very straightforward to implement and imparts a sense of dynamism to the page. Let me show you two examples. I have opened the request.htm file. Now if hover over the links in the right sidebar, you'll see a very--some might say too subtle effect, where a background diagonal pattern appears. Let's bump that up a little. First I'm going to need to find the controlling CSS rule. So I'll enable my Chrome Developer Tools by going to View > Developer > Developer Tools.

You can also do this in Firefox if you have Firebug installed, and I'm going to choose my Element Selector, which is a magnifying glass, and then click on one of the Academic Links, so I'll go to it. Over on the right-hand side under the Matched CSS Rules section, I can see it identifies the main.css file, line 304 as where the CSS rule resides that controls this code. So let me switch over to my code editor. Throughout the course I'll be using a free version of Aptana Studio, but you can use whatever code editor you like.

In my code editor, I've got main.css open from the Chapter 2/02_01/_css folder, which is found in the exercise files. So let's go to line 300. I'll choose Navigate > Go to Line, enter 300, and that takes us right to the CSS rule that governs section.info list item anchor, and this is where I want to put my initial transition code. I'll be using the vendor-specific prefix for WebKit, the browser engine used in Safari and Chrome, as well as the iOS in Android devices.

So I'll place my cursor right after background color, press Return, enter on the PC, and start to enter in my code. It's -webkit-, that's the vendor prefix, and then the property, transition: Now I'm going to use the shorthand version. Previously in Chapter 1, I showed you how the transition code was all broken out. Let's combine that into a single line by first entering the property we're going to transition. In this is case, it's background-color and then space, and we'll enter in the duration.

Here I'm going to make it a little bit less than a second. We will do .75s, another space, and then for timing function. Now I always like to specify the timing functions just so I remember that I have that option. Let's enter in ease and then close the declaration with a semicolon. Now I'm going to copy that line so I can paste it in and change the vendor prefix to -moz, which is the vendor prefix for Mozilla browsers, AKA Firefox. Now let's paste that line again, and this time I'll change the prefix to -o, which is the vendor prefix for Opera.

Let's paste it one more time, and this time I'll get rid of the vendor prefix entirely so that I can keep the generic CSS3 property transition. You'll notice that I didn't put in a vendor prefix for Microsoft, as Internet Explorer does not support the property in any of their browsers yet, and when that support does come--as projected for Internet Explorer 10--they'll use the standard CSS3 property transition, okay. Now let's add something to transition to. I'll go to the CSS rule governing the hover state with a selector section.info li a:hover right below it, and add the following code, background color, and I'm going to enter in my color value in an RGBA syntax, (168, 88, 163, .6). This color matches the purple that is used throughout the site.

And by using RGBA syntax, I can give the background color an alpha or opacity value of .6, which will help it blend in with diagonal pattern. Let's save our page now and take a look in the browser. First, I'll click the Refresh icon to reload the page, and now when I hover over the links, the background color purple comes up, slightly transparent, in a nice smooth transition. And of course, when I move my mouse out, it fades away, also in a nice transition.

Not a bad little effect and definitely more readable now. Another series of page elements that could benefit from transition in the background color are the form input elements also found on this page. Let's me close out my Developer Tools here, and I'll scroll down a bit, so you can see them. Right now, there is a border that's added when a text field gets focus, but I think we can enhance that look a bit. So let me reopen the Developer Tools, and I'll locate exactly where those input fields start, and I can see that the main.css:606 is where that focus is currently applied.

So I'll set back to our code editor and make some more changes. First I want to finish off this rule with a little semicolon. Now before I go down to line 600, because I want the transition to be consistent on my page, I'm going to go ahead and just copy this transition stack by pressing Command+C, Ctrl+C on the PC of course, and now we're going to head down to around line 600. I'll press Command+L to open up my Go to Line command, and let's just go right to 600. Now it turns out that that's a rule starting online 596 is where I want to put in my transitions.

So I'll make a little space and paste that in. Let's clean up that indent there and this extra lime. Now we're ready to modify the background-color when the input field has focus. So two rules down, on line 610, I find the exactly what I need. I'll click in, press Return and then add in my new declaration, which is background-color: rgba, paranthesis, and then my color declaration. And this time I'm going to make it fully opaque, I'll put in a 1, close it off with a semicolon and add one more declaration, and that's to change the color to white, and we'll use the hex value #fff.

I'll save the CSS file and switch back over to the browser. Let's drop out of our developer tools and click Reload, okay. So now when I give focus to the Name field, I still get the border, and I've also added a nice transition, and if I type in my name, it comes in a very nice contrasting white. If I tab out of that and give focus to the Email field, I get a transition out on the field I just left and a new transition in on my new field.

I think that's looks pretty dynamic, and it's very nice the way that transition reverses itself when the element loses focus, all of compliments of CSS3 Transitions. One of the really nice things about color transitions is that they degrade so gracefully Any browser that didn't support transitions would still see the change in the background, just not so elegantly presented.

Show transcript

This video is part of

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

26 video lessons · 14310 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.