Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Animate Changes With Transitions

The garden’s Sign up button and nav links switch colors the instant a visitor points at them. This lesson makes those changes fade, and shows why the transition belongs on the element’s own rule rather than in the hover rule.

Key Points

  • A transition tells the browser to animate a property’s change over time instead of switching it at once. You give it the property to watch and how long the change takes: transition: background-color 1s;.
  • Watch out: with the transition in the form button:hover rule, next to the new color, the Sign up button fades to dark green on the way in and snaps straight back to green on the way out.
  • Why: each time a property changes, the browser uses the transition of the rules that match the element once the pointer has arrived or left, in the new state. When the pointer arrives, the hover rule matches, so its transition applies. When the pointer leaves, the hover rule stops matching, and its transition goes with it. The button’s own rule has none, so the color switches at once.
  • States, not events: if you thought of hover as an event with the transition attached to it, CSS doesn’t work that way. It matches states, and once the pointer leaves, the hover state is gone, transition and all.
  • The fix: put the transition on the element’s own rule, here button, which matches in both states, and keep only the new values in the state rule. Then the change fades both ways.
  • The four parts this lesson uses: the property, the duration, the timing function, and the delay.
  • The default timing function, ease, does most of the change early, then slows into the new color. Half a second into a one-second fade, the button is already about four fifths of the way to dark green. linear moves at an even speed, so it’s halfway there at halfway: background-color 1s linear.
  • A delay, such as 0.5s, makes the button wait before it fades, on the way in and on the way back. Of the two times, the first is always the duration, the other the delay: background-color 1s ease 0.5s.
  • For the site: one second is long enough to watch but slow for a button you click, so the button and the nav links use 0.3s with the default ease, which you don’t need to write. The nav links’ hover and focus rule changes two properties, so the links’ own rule, a inside .site-nav, lists both, one transition per property, separated by a comma.
  • Nav links: they fade as the pointer passes and as you reach them with Tab. The focus ring still appears at once, because only the background and text colors have a transition.
  • Watch out: if you give an answer in the FAQ a height transition from 0 to auto, it still jumps open. With transition alone, the browser can’t step between a length and auto. A smooth opening needs techniques beyond this course.
  • The rule: put transition on the element’s own rule, and keep only the new values in the state rule.

Final Code

In the components layer of style.css, the button rule gets the transition, and the new hover rule keeps only the new color:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your garden site can now fold its FAQ answers away and float the opening hours over the page. In this video, you’ll make its changes move, starting with the Sign up button on the work day page.

00:12 First, give it a hover rule with the darkest green. Point at the button, and it turns dark green the instant the pointer arrives. Move away, and it’s green again as fast.

00:22 Rosa would like something gentler, a short fade each way.

00:28 A transition tells the browser to animate a property’s change, instead of switching it at once. You give it the property to watch and how long the change takes.

00:38 The color changes in the button’s hover rule, so the natural place for it is right there, next to the new color. Add a transition on background-color, lasting one second.

00:48 Save, and point at the button. It fades to dark green over a second.

00:54 What will the button do when the pointer moves away? Your turn: pause the video and predict.

01:05 It snaps straight back to green, with no fade at all.

01:11 That was a detour, but the reason it happened is what you’re here to learn. Each time a property changes, the browser looks at the rules that match the element once the pointer has arrived or left, and uses their transition.

01:24 When the pointer arrives, the hover rule starts to match. Its transition applies, so the color fades. When the pointer leaves, the hover rule stops matching, and its transition goes with it.

01:36 The button’s own rule has none, so the color switches at once.

01:41 If you thought of hover as an event, with the transition attached to it, CSS doesn’t work that way. It matches states, and once the pointer leaves, the hover state is gone, transition and all.

01:55 So move the transition up into the button’s own rule, the one that sets its green. That rule matches in both states. The hover rule keeps only the new color.

02:06 Save, and point at the button. It fades to dark green. Move away, and it fades back to green.

02:14 This lesson uses four parts of a transition. You’ve used two of them, the property and the duration. The third is the timing function, which sets how the speed changes along the way.

02:26 The default, ease, does most of the change early, then slows into the new color. Half a second in, the button is already about four fifths of the way to dark green.

02:36 With linear, it moves at an even speed, so it’s halfway there at halfway.

02:43 The fourth value is a delay. Of the two times, the first is always the duration, the other the delay. With half a second, the button waits, then fades, and it waits on the way back too.

02:57 One second is long enough to watch, but slow for a button you click. For the site, use 0.3s, with the default ease, which you don’t need to write.

03:08 The nav links get a hover and focus rule too, with a pale green background and dark text. Then give the links’ own rule a transition for their background and one for their text color.

03:19 A comma separates one transition per property. They fade as the pointer passes, and as you tab through them. The focus ring still appears at once, since only the colors have a transition.

03:33 You might want the FAQ answers to slide open the same way. Watch out: if you give an answer a height transition from zero to auto, it still jumps open. With transition alone, the browser can’t step between a length and auto.

03:47 A smooth opening needs techniques beyond this course.

03:52 So put the transition on the element’s own rule, and keep only the new values in the state rule. Then every change fades, on the way in and on the way out.

04:03 Background color is one property you can animate, but some properties cost the browser far more than others. The next lesson, Animate Cheaply and Respect Reduced Motion, shows which ones to choose, and how to tone motion down for visitors who ask for less.

Become a Member to join the conversation.