A transition only actually animates when the property it's watching changes value, typically triggered by something like a :hover state, a class toggle via JavaScript, or a media query changing which styles apply — a transition declared but never triggered by an actual property change produces no visible animation at all. Not every CSS property can be smoothly transitioned; only properties with sensibly interpolatable intermediate values, like color, width, or opacity, support transitions, while properties like display, which have no meaningful in-between state, cannot be transitioned.
1Understanding Transition
A transition only actually animates when the property it's watching changes value, typically triggered by something like a :hover state, a class toggle via JavaScript, or a media query changing which styles apply — a transition declared but never triggered by an actual property change produces no visible animation at all. Not every CSS property can be smoothly transitioned; only properties with sensibly interpolatable intermediate values, like color, width, or opacity, support transitions, while properties like display, which have no meaningful in-between state, cannot be transitioned.
A transition can't animate a property change that happens simultaneously with the element's initial appearance in the DOM — a newly-added element already rendered with its end styles has nothing to visibly transition from, since there was no earlier state present in the DOM for the browser to animate away from.
.button {
background-color: blue;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: darkblue;
}2Practical Example
Here is a real-world application of Transition showing how it is used in production CSS code.
.card {
transform: scale(1);
transition: transform 0.2s ease-out;
}
.card:hover {
transform: scale(1.05);
}3Best Practices
Follow these guidelines when working with Transition:
1. Declare transition on an element's default/base state, not just its hover or active state, so the transition applies smoothly in both directions, going into and out of that state
2. Specify transition-property explicitly, rather than the catch-all keyword all, when only specific properties actually need to animate, for both clarity and slightly better performance
3. Remember not every CSS property can be transitioned — properties without a sensible in-between value, like display, simply switch instantly regardless of a transition declaration
Tip: A transition can't animate a property change that happens simultaneously with the element's initial appearance in the DOM — a newly-added element already rendered with its end styles has nothing to visibly transition from, since there was no earlier state present in the DOM for the browser to animate away from.
.button {
background-color: blue;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: darkblue;
}