A CSS transition interpolates a property between two computed values over a set period, so a state change reads as movement instead of a jump. It needs four things — a property, a duration, a timing function and an actual change of value — and it only ever responds to a change that already happened.
This is the first lesson in the CSS Animations and Transitions module, and the next lesson in this module is about "What Can translate, scale and rotate Do to a Box?".
<div class="filters">
<div class="filters__row">
<button class="chip" type="button" aria-pressed="false">Vegetarian</button>
<button class="chip is-active" type="button" aria-pressed="true">Under 30 minutes</button>
<button class="chip" type="button" aria-pressed="false">High protein</button>
</div>
<div class="filters__panel">
<div class="filters-body">
<p>Cooking time, difficulty and equipment appear here.</p>
</div>
</div>
</div>
What does a transition actually need to run?
There are four longhand properties, and the shorthand takes them in a fixed order.
.chip {
transition-property: background-color, transform;
transition-duration: 180ms;
transition-timing-function: cubic-bezier(0.2, 0.8, 0.2, 1);
transition-delay: 0ms;
}
/* the same declarations collapsed into the shorthand */
.chip--shorthand {
transition: background-color 180ms cubic-bezier(0.2, 0.8, 0.2, 1),
transform 180ms cubic-bezier(0.2, 0.8, 0.2, 1);
}
The shorthand order is property, then duration, then timing function, then delay. Because duration and delay are both times, the first time value is the duration and the second is the delay. The most common mistake is writing transition: ease-in-out 200ms, where the easing function comes before the duration; that declaration is invalid and the whole shorthand is dropped, so the element snaps instead of animating.
A duration of zero means no animation at all, and the default duration is zero, which is why a transition needs to be declared before it does anything.
Which properties can be transitioned?
Only properties whose values can be interpolated between two points. Numbers, lengths, colours, percentages, transforms, opacity and filters all qualify. Values that flip between discrete states — display, visibility, pointer-events — have nothing to interpolate, so the browser switches them instantly.
.drawer {
visibility: hidden;
opacity: 0;
transform: translateY(-8px);
transition:
opacity 200ms ease,
transform 200ms ease,
visibility 0s linear 200ms;
}
.drawer.is-open {
visibility: visible;
opacity: 1;
transform: none;
transition:
opacity 200ms ease,
transform 200ms ease,
visibility 0s linear 0s;
}
This pattern hides a sliding panel without losing the ability to fade it. The visibility change is delayed by the length of the fade in the closed state, so it happens after the fade finishes, and delayed by nothing in the open state, so the drawer becomes visible immediately. Where browsers support transition-behavior: allow-discrete, display can be transitioned directly and this workaround is unnecessary.
Why does a transition not run when the page loads?
A transition describes how to get from one value to another, so it needs a starting value and a different ending value. When a page first renders, the element goes from having no style to having its style, and the browser treats that as the initial state rather than as a change. Nothing animates.
That is why a class added by a script after load does trigger a transition, while the same class present in the markup does not. For entrance animations on first paint, use a keyframe animation instead, which runs without needing a state change.
How do timing functions change the feel?
The timing function maps elapsed time onto progress between the two values. The named functions cover most needs, and cubic-bezier() lets you shape the curve yourself.
| Function | Shape | Where it suits |
|---|---|---|
| ease-out | Fast start, gentle landing | Elements arriving on screen |
| ease-in | Gentle start, fast exit | Elements leaving the screen |
| ease-in-out | Slow, quick, slow | Movement between two visible spots |
| linear | Constant rate | Loops and continuous motion |
| cubic-bezier() | You draw the curve | A brand-specific feel |
A cubic bezier takes two control points. The first two numbers set the direction of the start and the last two the direction of the end. The two horizontal numbers must sit between zero and one, while the vertical ones may go outside that range to create an overshoot. cubic-bezier(0.2, 0.8, 0.2, 1) is a snappy curve that overshoots nothing and settles quickly, which is why it appears so often on interface controls.
How do you transition a height you do not know?
Heights written as auto cannot be interpolated, which is why expanding panels traditionally jump. The cleanest pure-CSS answer animates a grid track instead of the height itself.
.filters__panel {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 260ms ease;
}
.filters.is-open .filters__panel { grid-template-rows: 1fr; }
.filters-body { overflow: hidden; }
The panel is a grid with a single row that is either zero tall or one fraction of the available space, and the content sits inside a child with overflow: hidden so it is clipped while the row collapses. Because the row is fractional, the transition ends at the content's natural height without any measurement in script. The trade-off is that animating a track size involves layout on each frame, so it is more expensive than animating a transform, which matters on long lists.
The mental model
- A transition needs two different computed values, so it only reacts to changes that already exist.
- Write the shorthand as property, duration, timing function, delay, or it will be ignored entirely.
- Name the properties you want to animate instead of using all, which also animates properties you did not mean to touch.
- Discrete properties such as display have nothing to interpolate; pair them with a fade and a delayed visibility change.
- Use ease-out for arrivals, ease-in for departures, and ease-in-out for movement between visible states.
- Give entrance effects on first paint to a keyframe animation, because no state change happens on load.
Try it yourself: Build a row of filter chips for a recipe site. Give each chip a background colour, a border radius and a subtle scale change, then transition the background and the transform over 180ms with a cubic-bezier curve. Add an is-active class that switches the background and applies transform: scale(1.04), and write the transition so that the chip also lifts 2px with a shadow. Toggle the class on three different chips in turn and confirm each one animates rather than snapping.