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;
}
.boxis the box's normal state: its colour is blue (#3b82f6)..box:hoveris the state while the cursor is over the box: its colour is red (#ef4444). This rule is what changes the colour.transition: background 0.5s easemakes that colour change happen slowly. Its value has three parts, separated by spaces:background: the property to animate, which is the property that changes in:hover.0.5s: how long it takes, 0.5 seconds. The unit iss(seconds) orms(milliseconds), and you must write it.ease: how the change speeds up and slows down, explained below.
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
- The background colour changes? Write
transition: background 0.3s ease. transform: scale(1.2)changes (the box grows, covered in the next lesson)? Writetransition: transform 0.5s ease-in-out.- Two properties at once? Separate them with a comma:
transition: background 0.3s ease, transform 0.2s ease-out. allanimates every property that changes:transition: all 0.3s ease.
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:
ease: starts slow, speeds up in the middle, then slows down at the end. This is the default.linear: the same speed from start to finish.ease-in: starts slow, then keeps getting faster.ease-out: starts fast, then slows down at the end.ease-in-out: slow at the start and at the end.
⚠️ No transition at all, or one that feels slow?
- A duration without a unit.
transition: background 0.3 easeis not valid, so nothing animates at all. Write0.3s. - Naming a property that doesn't change.
transition: color 0.3sdoes nothing if onlybackgroundchanges in:hover. - A duration that's too long. Around 150 to 300ms feels right. One second or more makes people feel they are waiting.
- Not every property can be smooth.
displayandheight: autousually jump at once and are not transitioned.
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; } }
.toast[hidden]picks a.toastthat has thehiddenattribute.allow-discreteletsdisplaytake part in the transition. When the toast disappears,display: noneis applied only after 0.3 seconds, soopacityhas time to fade.@starting-stylegives the starting style for the moment an element first appears. Without@starting-style, the toast shows fully at once, because there is no earlieropacityvalue to start from.
This works only for display: height: auto still jumps.
💡 Smoothest of all:
transformandopacity.transformchanges the size or position an element is drawn at, andopacitysets how see-through the element is. Both are easy for the browser to animate. Animatingwidth,heightormarginmakes 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.