CSS Transitions: How to Animate Changes
In this article
CSS transitions let you smoothly animate changes to CSS property values, such as color, size, or position, over a specified duration. You define the transition on the element’s base state, and when a property changes (e.g., on hover or via JavaScript), the browser animates the change instead of applying it instantly.
Background: Why Transitions Matter
Before CSS transitions, any change to an element’s style—whether from a hover state, a class toggle, or a script—happened instantly. That abrupt jump can feel jarring and makes a page seem unpolished. Transitions were introduced in CSS3 (around 2010) and are now supported in all modern browsers. They allow you to add subtle motion that guides the user’s eye, indicates interactivity, and creates a more engaging experience without requiring JavaScript animation libraries.
How CSS Transitions Work
A transition is defined by four main properties, which you can set individually or with the shorthand transition:
- transition-property: the CSS property you want to animate (e.g.,
background-color,transform). - transition-duration: how long the animation takes (e.g.,
0.3s). - transition-timing-function: the speed curve (e.g.,
ease,linear,cubic-bezier()). - transition-delay: an optional delay before the animation starts.
For example, to animate a button’s background color on hover:
button {
background-color: #3498db;
transition: background-color 0.3s ease;
}
button:hover {
background-color: #2ecc71;
}When you hover, the background fades from blue to green over 0.3 seconds. The transition applies to the change—it works both ways, so when you move the mouse away, it animates back to blue.
Choosing What to Animate
Not every CSS property can be transitioned. The browser can only interpolate values that have a clear intermediate state. Common animatable properties include:
- Color properties:
color,background-color,border-color - Dimensions:
width,height,padding,margin - Position:
top,left,right,bottom - Transform:
translate,scale,rotate - Opacity
- Box-shadow
- Filter
Properties like display (none to block) or visibility cannot be transitioned because they don’t have intermediate values—they simply flip. If you need to animate something like a dropdown appearing, you can transition opacity and transform while toggling display after the transition ends (using JavaScript or a small delay).
For performance, it’s best to animate transform and opacity rather than properties like width or top. The browser can handle these on the GPU, avoiding layout recalculations and making the animation smoother.
Common Problems and Solutions
Transitions are simple, but a few pitfalls trip up developers:
- Transition not firing: Make sure the property you’re changing is listed in
transition-property. If you use the shorthandtransition: all 0.3s, it works, but it’s less efficient. - Immediate jump on page load: If an element starts with a transition and you change its style via JavaScript right after load, the browser may not animate. Use a small timeout or requestAnimationFrame to let the initial state render first.
- Transition on
displaydoesn’t work: As noted,displayis not animatable. Useopacityandvisibility(with a delay) or a max-height trick for accordions. - Unexpected easing: The default timing function is
ease, which starts fast and slows down. If you want a constant speed, uselinear. For more control, usecubic-bezier().
One common misconception is that transitions work for any property change. They only work when the property is explicitly listed and the values are numeric or color-based. For instance, animating background-image is not possible; you’d need to use multiple layers or a sprite.
Transitions vs. Animations
Transitions are often confused with CSS animations, but they serve different purposes. A transition runs when a property changes from one value to another—it needs a trigger (like :hover or a class change). An animation, using @keyframes, runs automatically and can have multiple steps, loops, and more complex timing.
Use transitions for simple two-state changes (e.g., hover effects, toggling a menu). Use animations for continuous effects (e.g., a spinner, a bouncing icon) or when you need to control intermediate states.
Accessibility and Best Practices
Motion can be problematic for users with vestibular disorders. Always consider the prefers-reduced-motion media query to disable or minimize transitions for those users:
@media (prefers-reduced-motion: reduce) {
* {
transition-duration: 0.01ms !important;
}
}Also, keep durations short—generally between 150ms and 500ms. Longer transitions feel sluggish and can frustrate users. Use a consistent easing curve across your site to create a cohesive feel.
Frequently Asked Questions
Can I use CSS transitions on all properties?
No, only properties that have an intermediate value can be transitioned. Numeric values (like width, opacity) and colors work. Properties like display or font-family cannot be animated because they don’t have a numeric range.
What is the difference between transition and transform?
transform is a property that changes the visual appearance of an element (move, scale, rotate). A transition is the mechanism that animates changes to any CSS property, including transform. You can transition a transform, but you don’t need a transform to use a transition.
How do I make a transition trigger on page load?
Transitions need a state change. On page load, you can add a class to an element after the page renders using JavaScript (e.g., setTimeout or requestAnimationFrame). The element must have the transition defined on its initial state, and then you change the property with the new class.
Why is my transition not smooth?
It could be because you’re animating a property that causes layout thrashing, like width or top. Switch to transform and opacity for smoother performance. Also, check your timing function—ease-in-out often feels more natural than linear.
Can I transition multiple properties at once?
Yes, list them in the transition-property (or use all), or use the shorthand: transition: color 0.3s, transform 0.5s;. Each property can have its own duration and timing function.
Do transitions work on mobile browsers?
Yes, all modern mobile browsers support CSS transitions. However, performance can vary, so stick to transform and opacity for mobile to ensure smooth 60fps animations.