Transitions: Animating Between States With transition
Making property changes gradual: the transition properties and shorthand, where to declare them, different speeds in and out, which properties can transition and how values interpolate, transition events, and how interrupted transitions reverse.
kvi5hnuCode Sprout team member7 min readOn this page
Normally, when a property's value changes, because of :hover, a class added by JavaScript or a media query, the new value applies instantly. A transition makes the change gradual: the browser animates from the old value to the new one over a time you choose.
.button {
background: #8a3b12;
transition: background-color 0.2s ease-out;
}
.button:hover { background: #b4521c; }
Transitions only run when a value changes. They don't run when the page first loads, and they can't loop. For animations that run by themselves, use keyframes (article 109).
The transition properties

| Property | Default | Meaning |
|---|---|---|
transition-property | all | which properties to transition: a comma-separated list, all or none |
transition-duration | 0s | how long it takes; needs a unit, even for 0 |
transition-timing-function | ease | how progress speeds up and slows down (article 107) |
transition-delay | 0s | how long to wait before starting; negative values start partway |
transition-behavior | normal | allow-discrete lets properties like display transition (article 108) |
Tested in Chrome, those were the computed defaults. Because the default duration is 0s, nothing transitions until you set one.
The shorthand
transition takes, for each property: the property name, a duration, a timing function, a delay and allow-discrete, in any order. The first time is the duration, the second is the delay:
.card { transition: transform 0.3s ease-out, box-shadow 0.3s ease-out 0.1s; }
.fade { transition: opacity 0.2s; } /* the delay defaults to 0s */
Watch out when listing several transitions. Tested in Chrome, transition: color, opacity 200ms ease-in 50ms gave color a duration of 0s, because each comma-separated item has its own values. Only opacity was transitioned.
Lists of values
With the longhands, the lists are matched by position. If there are fewer durations, delays or timing functions than properties, the list is repeated; extra values are ignored:
.panel {
transition-property: opacity, translate, background-color;
transition-duration: 0.2s, 0.4s; /* background-color gets 0.2s again */
}
Avoid all
transition: all 0.3s is tempting, but it animates every property that changes, including ones you didn't intend, like width changing in a media query, and it makes unrelated changes slower. List the properties you mean to animate.
transition: none removes all transitions. none can't be part of a list.
Where to put the transition
A transition uses the transition values of the state it's moving to. Put it on the base rule to get the same transition both ways:
.menu { opacity: 0; transition: opacity 0.2s; }
.menu.open { opacity: 1; } /* fades in and out in 0.2s */
To use different speeds in each direction, set a different transition on each state:
.tooltip { opacity: 0; transition: opacity 0.15s 0.3s; } /* closing: a 0.3s delay, so it doesn't vanish when the pointer slips off */
.trigger:hover + .tooltip { opacity: 1; transition: opacity 0.15s; } /* opening: immediately */
Putting the transition only on the :hover rule makes the change animate in but snap back instantly when the hover ends.
What can transition
A property transitions if its values can be interpolated: lengths, numbers, percentages, colors, transforms, shadows, and lists of these. Each property's definition says its animation type. Gradients (background-image) are discrete in Chromium and Firefox: animate registered custom properties used inside the gradient instead (article 28).
- Discrete properties, like
display,font-familyoralign-items, can't be interpolated; normally they just switch immediately. Article 108 shows howtransition-behavior: allow-discretemakes them switch at the right moment. visibilityis a special case: it interpolates as "visible if either end is visible". Tested in Chrome, it stayedvisibleuntil the end when changing tohidden, and becamevisibleimmediately when changing fromhidden. That's whyopacitywithvisibilitymakes a fade-out that also removes the element from the tab order at the end.autoand keywords likefit-contentcouldn't historically be interpolated;height: autotransitions are now possible withinterpolate-size(article 108).- Colors interpolate in the sRGB space when both are legacy colors, like
rgb(), hex or names, and in Oklab otherwise. Tested in Chrome, halfway fromrgb(255, 0, 0)torgb(0, 0, 255)wasrgb(128, 0, 128), while between twooklch()colors the midpoint was anoklab()color (article 32). - Shadows interpolate offset, blur, spread and color, but not between
insetand outset. Tested in Chrome, changing an outsetbox-shadowto aninsetone created no transition. - Custom properties are discrete unless they're registered with a type using
@property(article 28). - Shorthands transition as their longhands. Tested in Chrome, transitioning
paddingcreated four transitions:padding-top,padding-right,padding-bottomandpadding-left.
If one property in transition-property can't be animated, the others still transition.

The last two rows are the special cases from this section: visibility holds visible for the whole fade, and a shadow that switches between outset and inset can't be interpolated, so it jumps.
Transition events
Four events fire on the element, each with propertyName, elapsedTime and pseudoElement:
| Event | Fires when |
|---|---|
transitionrun | the transition is created, before its delay |
transitionstart | the delay ends and the value starts changing |
transitionend | it completes |
transitioncancel | it's removed before completing, for example by another change |
transitionend fires once per longhand property, so a padding transition fires four times, and it doesn't fire for a cancelled transition. Check propertyName, or wait for all of them with the Web Animations API:
el.classList.add('open');
await Promise.all(el.getAnimations().map(a => a.finished)); // every running transition and animation
Interrupted transitions
When a value changes back before its transition finishes, like moving the pointer away halfway through a hover effect, the new transition starts from the current value. To avoid a slow return from a short distance, the reverse transition is shortened in proportion to how far the first one got. Tested in Chrome: a 1s linear transition from 100px to 200px, reversed after 30%, at 130px, got a reverse duration of 0.3s.
This applies only when it's a reversal back to the original value. A change to a third value starts a new full-length transition from wherever the value currently is.
A negative delay starts the transition partway: tested in Chrome, transition: width 1s linear -0.5s from 100px to 200px jumped straight to 150px and finished the remaining half in 0.5s.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.btn {
font: inherit; padding: 0.5rem 1rem; border: 0; border-radius: 8px; color: white;
background: #8a3b12; box-shadow: 0 2px 0 rgb(0 0 0 / 0.25);
transition: background-color 0.2s, translate 0.2s ease-out, box-shadow 0.2s;
}
.btn:is(:hover, :focus-visible) { background: #b4521c; translate: 0 -2px; box-shadow: 0 6px 12px rgb(0 0 0 / 0.25); }
.wrap { position: relative; display: inline-block; }
.trigger { font: inherit; }
.tip {
position: absolute; inset-block-start: 110%; inset-inline-start: 0; inline-size: max-content;
padding: 0.3rem 0.5rem; border-radius: 6px; background: #1d3557; color: white;
opacity: 0; visibility: hidden;
transition: opacity 0.2s 0.4s, visibility 0.2s 0.4s; /* closing: wait, then fade */
}
.trigger:is(:hover, :focus-visible) + .tip {
opacity: 1; visibility: visible;
transition: opacity 0.2s, visibility 0.2s; /* opening: no delay */
}
.track { block-size: 1.4rem; border-radius: 999px; background: #eee6d8; overflow: hidden; margin-top: 2.2rem; }
.bar { block-size: 100%; inline-size: 10%; background: #2f7d32; transition: inline-size 2s linear; }
.track:hover .bar { inline-size: 100%; }
.note { color: #555; }
@media (prefers-reduced-motion: reduce) { * { transition-duration: 0.01s !important; } }
The button transitions three properties together. The tooltip opens immediately but waits 0.4s before closing, because each state has its own transition. Hover the bar and leave halfway: it returns in about the time it took to get there, not the full 2s.
What's current
- Transitions and their events: all browsers.
transition-behavior: allow-discrete: all browsers (article 108).- Colors in modern syntax interpolate in Oklab.
Common questions
Why doesn't my transition run?
Usually the duration is missing, since the default is 0s. Other causes: the property isn't animatable, the value changes from auto or display: none, or the element was just created, so there was no old value.
Why does my hover effect animate in but snap back?
The transition is only on the :hover rule. Move it to the base rule.
Why does transitionend fire several times?
Once per longhand: padding or border-radius fire four events. Check event.propertyName.
How do I transition height: auto?
See article 108: interpolate-size: allow-keywords or calc-size().
Summary
transitionanimates changes between values, using the transition on the destination state.- Set a duration; the first time in the shorthand is the duration, the second the delay.
- List specific properties instead of
all. - Only interpolable values transition;
visibilitystays visible during the transition, discrete properties needallow-discrete. transitionendfires per longhand; reversed transitions are shortened.
Practice questions
Write a transition that fades a panel in over 0.3s and out over 0.15s.
Answer
.panel { opacity: 0; transition: opacity 0.15s; } /* used when closing */
.panel.open { opacity: 1; transition: opacity 0.3s; } /* used when opening */
What does this shorthand do?
.box { transition: opacity 0.5s 1s; }
Answer
It transitions opacity over 0.5s, starting after a 1s delay. The first time is the duration, the second the delay.
A developer listens for transitionend to remove an element after transition: padding 0.3s, opacity 0.3s. The code runs five times. Why?
Answer
padding is a shorthand for four longhands, each with its own transition and event, plus one for opacity. Check event.propertyName === 'opacity', or wait for getAnimations() to finish.
A tooltip fades out with opacity, but keyboard users can still tab to a link inside it. How do you fix it?
Answer
Also transition visibility: hidden along with the opacity. visibility stays visible until the end of the transition, so the fade still shows, and then the tooltip leaves the tab order:
.tip { opacity: 0; visibility: hidden; transition: opacity 0.2s, visibility 0.2s; }
.tip.show { opacity: 1; visibility: visible; }
Previous: 3D Transforms: Perspective, preserve-3d and Backfaces
Next: Timing Functions: Easing Keywords, cubic-bezier(), steps() and linear()

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.