Transitions
How a CSS transition eases a property change, how to control it per property and direction, and how @starting-style and allow-discrete animate entry and exit.
Transitions
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A transition does not move anything. It watches a property, and when that property's computed value changes it plays the journey from the old value to the new one instead of jumping.
- Four longhands control it: `transition-property`, `transition-duration`, `transition-timing-function` and `transition-delay`. Duration defaults to `0s`, which is why a transition with no duration does nothing.
- The transition that runs is the one declared on the element's new state, so a slow fade-in on `:hover` and a quick fade-out on the base rule is a legitimate design.
- Prefer listing properties (`transition: opacity 200ms, translate 200ms`) over `transition: all`, which animates anything that happens to change, including properties you never meant to move.
- Display and other discrete properties only transition with `transition-behavior: allow-discrete`, and an element's first frame needs `@starting-style` to have something to transition from.
Why a property needs help changing
Say a button’s background goes from white to navy when the user hovers it. Without any extra instruction, the browser computes the new style and paints it on the next frame. The change is a cut, like a light switch.
A transition is you telling the browser, “when this particular property changes, don’t cut, play the in-between frames.” The in-between values are real: for a colour the browser mixes the two colours, for a length it slides the number, for a transform it interpolates the matrix. That only works for values that have a halfway point, which becomes important at the end of this lesson.
The frontend roadmap has a short overview of transitions and keyframes together. This lesson goes through the transition side in more detail.
The four longhands
.button {
background-color: white;
transition-property: background-color;
transition-duration: 200ms;
transition-timing-function: ease-out;
transition-delay: 0s;
}
.button:hover {
background-color: navy;
}
transition-propertyis what to watch: a property name, a comma list, orall.transition-durationis how long the journey takes. The default is0s, so a transition with no duration is switched off.transition-timing-functionis the pacing:ease,linear,ease-in-out,cubic-bezier(...), orsteps(...)for a stop-motion feel.transition-delaywaits before starting. A negative delay starts the transition part-way through.
The shorthand lists them in the order property, duration, timing function, delay. When two time values appear, the first is the duration and the second is the delay. You can comma-separate several transitions with different timings:
.card {
transition:
translate 250ms ease-out,
box-shadow 250ms ease-out,
opacity 150ms linear;
}
The new state decides how long it takes
This is the part people trip over. The browser reads the transition declaration from the state the element is moving into. Hover-in uses the :hover rule’s transition. Hover-out uses the base rule’s, because the base rule is what applies again.
.menu-item {
transition: background-color 150ms; /* used when leaving :hover */
}
.menu-item:hover {
background-color: #eef;
transition-duration: 0s; /* used when entering :hover */
}
This gives you an instant highlight that fades away gently, a common feel for menus. If you only put transition on :hover, you will get an animated hover-in and an instant snap back, which looks like a bug.
Mid-flight reversals
If the user hovers and leaves before the transition finishes, the browser does not jump to the end and play the reverse. It starts a new transition from wherever the property currently is, back toward the original. You get smooth reversals for free, so long as both ends of the change are just values on the same property.
Avoid all
transition: all 0.3s is convenient and eventually wrong. It animates every property that changes, now and in the future. If a media query changes width, a theme switch changes color, or a framework toggles display, each will animate in surprising ways. List the one or two properties you mean. For movement and fades, prefer the properties the browser can animate cheaply, which the animation performance lesson explains.
Entering and leaving: @starting-style and allow-discrete
Two problems used to force JavaScript timers. A freshly inserted element has no previous style to transition from, and display: none cannot be interpolated.
.toast {
opacity: 1;
translate: 0 0;
transition:
opacity 250ms,
translate 250ms,
display 250ms allow-discrete;
@starting-style {
opacity: 0;
translate: 0 1rem;
}
}
.toast[hidden] {
display: none;
opacity: 0;
translate: 0 1rem;
}
@starting-style declares the values to use for the element’s first style update, so when .toast appears it transitions from opacity: 0 to 1. allow-discrete (also available as the transition-behavior property) lets display take part: when hiding, the old display: block is kept until the fade ends, then none applies. I checked in Chrome that a transition of a registered property really runs mid-way and that an inserted element with @starting-style creates a transition while one without it does not. @starting-style and transition-behavior are in Chrome 117, Firefox 129 and Safari 17.4 or later (17.5 for @starting-style) according to MDN’s compatibility data.
Animating to height: auto is still a special case. interpolate-size: allow-keywords enables it, but as of this data only Chromium supports it, so treat it as progressive enhancement and keep a layout that works without the animation (the supports lesson shows how).
Respect reduced motion
Some people get dizzy from large movement. Wrap bigger movement in a media query:
@media (prefers-reduced-motion: reduce) {
.card { transition-duration: 0.01ms; }
}
The accessible CSS lesson comes back to this.
What the video covers
Kevin Powell’s “Animating with CSS Transitions - A look at the transition properties” is from January 2018 and runs about 18 minutes. From its title it goes through the individual transition properties one at a time. The video predates @starting-style and allow-discrete, so those come from the section above, and his later “We can now transition to and from display: none” (under further viewing) covers them from the other direction. I could not read a transcript, so the specific examples are not described here.
Where this fits
Transitions need two states and a trigger. When you want motion with its own timeline and several steps, you want keyframe animations, and when the trigger should be scroll position, scroll-driven animations. The property you transition most often is translate or transform, from transforms and 3D.
Check yourself
A button has `transition: background-color 300ms` on the base rule and `:hover { background-color: navy; transition-duration: 1s }`. How long does the colour take to change on hover-in, and how long on hover-out?
One second going in, 300 ms coming out. The transition used is the one declared in the state the element is moving into. On hover the `:hover` rule applies, so its `1s` wins. When the pointer leaves, the base rule applies again, so its `300ms` is used for the return trip.
You write `.panel { transition: all 0.3s }` and later add `width: 100%` for a layout change. What is the risk?
`all` transitions every property that changes, so the width change now animates too, along with anything else that changes under that selector later (colour, border, padding). Layout properties like `width` are also expensive to animate. Naming the properties you want keeps the animation intentional.
Why does `.modal { transition: opacity 200ms }` fade a modal in when a class is toggled, but not when the element is freshly inserted into the DOM?
A transition needs a before value and an after value. A newly rendered element has no previous style, so there is nothing to animate from. `@starting-style { opacity: 0 }` supplies the before value for that first frame, and the transition then runs to the element's real `opacity`.
What does `transition-behavior: allow-discrete` change?
By default properties like `display` and `visibility` flip at the start of a transition because they have no in-between values. With `allow-discrete` they take part in the transition, and for `display` the element stays rendered for the whole duration, so you can fade it out and have `display: none` apply only after the fade completes.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.