CSS Reference ยท Transforms, Transitions and Animations
The transition property
Reference for the CSS transition shorthand: property, duration, easing, delay and behavior in one declaration, with syntax, examples and browser support.
The transition property makes a change in a CSS value happen gradually instead of instantly. When a property that has a transition changes (for example on :hover, when a class is added, or when a media query starts to match), the browser computes the in-between values over the given time.
It is a shorthand for transition-property, transition-duration, transition-timing-function, transition-delay and transition-behavior. Put it on the element's normal rule, not only on the state that triggers the change, so it works in both directions.
.button { transition: background-color 200ms ease-out; } .button:hover { background-color: #1e40af; }
Try it
Each dot gets the same class change (.go on the page), but a different transition value, so each one moves in its own way. Press the button again to see the transitions run backward.
Syntax
transition: <single-transition>#
<single-transition> =
[ none | <property-name> ] || <duration> || <easing-function> || <delay> || <transition-behavior>
- Initial value:
all 0s ease 0s normal(so by default nothing animates, because the duration is zero) - Applies to: all elements, and the
::beforeand::afterpseudo-elements - Inherited: no
- Animatable: no
- Computed value: as specified, for each of the longhand properties
Values
| Value | Description |
|---|---|
<property-name> |
A property to animate, such as opacity, or the keyword all. See transition-property. |
none |
No transition. Must be the only item in the list. |
First <time> |
The duration: 300ms or 0.3s. |
Second <time> |
The delay before the transition starts. |
<easing-function> |
The speed curve: ease, linear, cubic-bezier(), steps(), linear(). |
normal / allow-discrete |
Whether non-animatable (discrete) properties flip partway. See transition-behavior. |
| Comma-separated list | Several transitions on one element, each with its own settings. |
Usage notes
- Two times, two meanings. The first time value is always the duration, the second is the delay.
transition: opacity 1s 2swaits two seconds, then fades for one. - A duration is required. With the default duration of
0s,transition: opacitydoes nothing visible. - Declare it on the base rule. If it only appears on
:hover, the change in is smooth and the change back snaps, because the transition belongs to the state the element is entering. - Prefer listing properties.
transition: allis convenient but animates every change, including ones you did not intend (such as layout properties), and can cause jank. Nameopacity,transformorbackground-colorexplicitly. - Cheap properties.
transformandopacityusually animate smoothly; animatingwidth,heightortopforces layout work on every frame. - No transition on first render. An element that is just inserted has no earlier value to change from. Use
@starting-stylefor entry animations. - Interrupting. If the value changes again mid-transition, the browser starts a new transition from the current value toward the new target.
- Events.
transitionrun,transitionstart,transitionendandtransitioncancelfire on the element, once per property. - Reduced motion. Shorten or remove transitions inside
@media (prefers-reduced-motion: reduce)when they involve large movement.
Browser support
The transition shorthand is supported in every browser. The transition-behavior part is newer; check a compatibility table before depending on it.
Related
transition-property,transition-duration,transition-timing-function,transition-delay,transition-behaviorโ the longhandsanimationโ keyframe animations@starting-styleโ styles for first render- Lessons: Transitions, Transforms