CSS propertyTransitions

transition

A shorthand that makes CSS value changes happen smoothly instead of abruptly.

Initial value
all 0s ease 0s
Inherited
No
Animatable
no
Applies to
all elements and the ::before and ::after pseudo-elements
JavaScript
element.style.transition = "transform 0.3s ease-out"
On this page

Example

#
CSS
.button {
  background: #7c3aed;
  transition: background-color 0.2s, transform 0.2s;
}

.button:hover {
  background: #6d28d9;
  transform: translateY(-2px);
}
Preview

Definition and usage

#

The transition property combines four transition settings: which property to animate (transition-property), for how long (transition-duration), with what easing (transition-timing-function), and with what delay (transition-delay).

A transition starts by itself when a property value changes, for example in the :hover state, after JavaScript adds a class, or after an @media query changes. You don’t need an @keyframes rule, two states are enough.

Separate transitions for different properties with commas, for example a fast one for colour and a slower one for movement. transform and opacity animate most smoothly.

Syntax

#
Syntax
transition: <property> <duration> <timing-function>? <delay>? [, …]*;

Values

#

Values in practice

#

none

No transition: the change happens immediately.
.demo { transition: none; }

all 0.6s

All changing properties over 0.6 seconds.
.demo { transition: all 0.6s; }

transform 0.8s ease-out

Only the movement is smooth, the colour changes abruptly.
.demo { transition: transform 0.8s ease-out; }

background 0.2s, transform 1s 0.3s

A fast colour change and a slower, delayed movement.
.demo { transition: background 0.2s, transform 1s 0.3s; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Avoid transition: all. List specific properties so you don’t accidentally animate something expensive.
  • Durations of 150-300 ms feel fast and natural. Longer transitions in an interface start to become annoying.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.