CSS Transitions

transition: property duration timing makes a style change happen gradually, for example transition: background 0.3s ease for a background colour that changes…

Move the cursor onto a button on a well-made site. Its colour changes smoothly instead of switching at once. With nothing extra, a change in :hover (while the cursor is over the element) always jumps: blue, then suddenly red.

The thing that makes a style change happen gradually is called a transition. With a transition, the button's colour changes smoothly, for example in 0.3 seconds.

A transition, part by part

This example has two rules for one box, one for its normal state and one for its state on hover:

.box {
  background: #3b82f6;
  transition: background 0.5s ease;
}

.box:hover {
  background: #ef4444;
}

The result: when you move the cursor onto the box, the blue changes slowly into red over half a second. When you move the cursor away, the red changes slowly back to blue.

Write it on the normal rule, not on :hover

The browser uses the transition of the state it is going to: the hover state when the cursor enters, and the normal state when the cursor leaves. The .box rule always applies, also during hover. So if transition is on .box, the change is smooth both when the cursor enters and when it leaves. If transition is only on .box:hover, the change is smooth when the cursor enters. But when the cursor leaves, the state the box goes to is .box, which has no transition, so the colour jumps back at once.

So the style that changes goes in :hover, and the transition goes on the normal rule.

Name the property that actually changes

There is no colon inside the transition value: write all 0.3s ease, not all: 0.3s ease. background and background-color both animate the background colour. But in the exercise, the checker compares the property name, so write exactly what the exercise instructions ask for.

Timing functions

The third part of the transition value sets when the change is fast and when it is slow:

⚠️ No transition at all, or one that feels slow?

But since 2024, all major browsers can fade an element that appears or disappears through display. This example is for a small notification (a toast) that is hidden with the hidden attribute:

.toast { transition: opacity 0.3s, display 0.3s allow-discrete; }
.toast[hidden] { display: none; opacity: 0; }
@starting-style { .toast { opacity: 0; } }

This works only for display: height: auto still jumps.

💡 Smoothest of all: transform and opacity. transform changes the size or position an element is drawn at, and opacity sets how see-through the element is. Both are easy for the browser to animate. Animating width, height or margin makes the browser recalculate the page layout at every step, so the animation may not run smoothly.

TL;DR: transition: property duration timing makes a style change happen gradually, for example transition: background 0.3s ease for a background colour that changes on hover (the cursor is over the element). Write it on the element's normal rule (for example .box), not on its :hover rule, so the change is smooth both when the cursor enters and when it leaves. Name the property that actually changes, and write the duration with s (seconds) or ms (milliseconds). The timing is how the change speeds up and slows down: ease (the default) is slow at the start and at the end, and linear keeps the same speed.

⚡ A gradual change

Write:

.box {
  background: #3b82f6;
  transition: background 0.5s ease;
}

.box:hover {
  background: #ef4444;
}

Meaning: .box:hover applies while the cursor is over the box, and it changes the colour from blue to red. transition: background 0.5s ease makes that colour change happen slowly, over half a second. Its value has three parts: the property that changes, the duration, then the timing (ease: slow at the start and at the end).

⚡ Put it on the normal rule

Guess first: if transition is only on .box:hover, what happens when the cursor leaves the box?

The box jumps back to its first colour at once. The change is smooth when the cursor enters. When the cursor leaves, the .box:hover rule stops applying. Only .box is left, and it has no transition.

Meaning: The style that changes goes in :hover, and the transition goes on the normal rule (.box). The .box rule always applies, also during hover, so the change is smooth both when the cursor enters and when it leaves.

⚡ Name what changes

Guess first: does transition: background 0.3 ease work?

Not at all. The duration has no unit, so it is not valid. Write 0.3s.

Meaning: Name the property that actually changes, for example background. all animates every property that changes: transition: all 0.3s ease.

In the exercise, the checker compares the property name, so write exactly what the exercise instructions ask for.

What you will learn