- View Offline
- 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
Skill Level Intermediate
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.
Sign up for a Premium Membership to download courses for Internet-free viewing.
Watch offline with your iOS, Android, or desktop app.Start Your Free Trial
After signing up, download the course here or from the iOS/Android App.