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"
Example
#.button {
background: #7c3aed;
transition: background-color 0.2s, transform 0.2s;
}
.button:hover {
background: #6d28d9;
transform: translateY(-2px);
}<style>
.hover-btn { background: #7c3aed; color: #fff; border: 0; padding: 10px 18px; border-radius: 10px; transition: background-color .2s, transform .2s; }
.hover-btn:hover { background: #6d28d9; transform: translateY(-2px); }
</style>
<button class="hover-btn" type="button">Hover over me</button>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
#transition: <property> <duration> <timing-function>? <delay>? [, …]*;Values
#- No transition: the change happens immediately.
- All changing properties over 0.6 seconds.
- Only the movement is smooth, the colour changes abruptly.
background 0.2s, transform 1s 0.3s
multiple transitions
A fast colour change and a slower, delayed movement.
Values in practice
#none
.demo { transition: none; }all 0.6s
.demo { transition: all 0.6s; }transform 0.8s ease-out
.demo { transition: transform 0.8s ease-out; }background 0.2s, transform 1s 0.3s
.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
#- transition vs animationtransition animates a change between two states after a trigger, e.g. :hover. animation plays back steps from @keyframes on its own, even in a loop.
- transition-propertySpecifies which CSS properties should change smoothly.
- transition-durationSets how long a transition lasts.
- transition-timing-functionSets the transition’s easing curve: accelerating, decelerating, or stepped motion.
- animationA shorthand that sets all the parameters of an animation based on @keyframes.
- transformMoves, rotates, scales, or skews an element without affecting the page layout.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.