Sub topic 2
Writing a transition: property, duration, timing function and delay
This chapter assumes you know CSS selectors and the cascade, common properties such as color, opacity and background, and pseudo-classes such as :hover and :focus-visible. It covers how to write a transition. Later chapters cover easing in depth, which properties can animate, and keyframes.
The working model
A transition tells the browser: "When this property's computed value changes, don't jump. Move from the old value to the new one over time."
The browser needs two things to run a transition:
- A change in a computed value, such as a
:hoverrule taking effect. - A
transitiondeclaration on the element that describes how to move between values.
The transition declaration belongs on the element's base rule, not only on the hover rule. The base rule is in effect in both directions, so the element eases in and eases back out.
The four longhand properties
A transition has four parts. Each has its own longhand property.
| Longhand | Purpose | Default |
|---|---|---|
| `transition-property` | Which properties to transition | `all` |
| `transition-duration` | How long the change takes | `0s` |
| `transition-timing-function` | How progress is distributed over time | `ease` |
| `transition-delay` | How long to wait before starting | `0s` |
The default duration is 0s, so a transition with no duration never visibly runs. This is the most common reason "my transition does nothing".
The shorthand
The transition shorthand combines the four values for one property:
.button {
background-color: #2563eb;
transform: translateY(0);
transition: background-color 200ms ease-out, transform 200ms ease-out;
}
.button:hover,
.button:focus-visible {
background-color: #1d4ed8;
transform: translateY(-2px);
}Separate multiple transitions with commas. Each one can have its own duration, timing function and delay.
Inside one transition, the first time value is the duration and the second is the delay. So transform 200ms ease-out 50ms means a 200 ms duration after a 50 ms delay. Other parts can appear in any order.
Listing properties versus using `all`
transition: all 200ms is tempting, but it has costs:
- It also transitions properties you did not intend, such as
widthchanged by a media query orcolorchanged by a theme switch. - It can animate expensive properties like
heightorbox-shadow.
Naming the properties makes the behavior predictable and easy to review.
Delay, and different settings per direction
A positive delay postpones the start. A negative delay starts the transition partway through, as if it had already been running for that long.
The transition that applies is the one on the element's new state. You can use this to make entering and leaving differ:
.menu {
opacity: 0;
transition: opacity 100ms ease-in;
}
.menu.open {
opacity: 1;
transition: opacity 250ms ease-out 50ms;
}Opening the menu uses the .open rule: 250 ms with a 50 ms delay. Closing it uses the base rule: a quick 100 ms fade.
Common errors
- Duration is missing. The shorthand
transition: opacity easehas no time value, so the duration is0s. - The property is not animatable. A change to
displaydoes not interpolate in the usual way. Chapter 4 covers this. - No starting value. Transitioning from
height: autoto a fixed height does not work in the traditional model, becauseautohas no number to interpolate from. - Transition only on the hover rule. The element eases in but snaps back when the pointer leaves.
- A typo in the property name.
transition: backgroundColor 200msis invalid. Usebackground-color.
Exercise
Exercise
A card has .card { opacity: 0.6; transition: opacity ease-out; } and .card:hover { opacity: 1; }. Hovering changes opacity instantly. Why, and how do you fix it so the change takes 300 ms?
Show answer
The shorthand has no time value, so the duration defaults to 0s. Add a duration: transition: opacity 300ms ease-out;.
Exercise
Write a transition on .tag that changes background-color over 150 ms and transform over 250 ms with a 50 ms delay, both using ease-in-out.
Show answer
.tag { transition: background-color 150ms ease-in-out, transform 250ms ease-in-out 50ms; }