CSS Transitions: Animating State Changes

CSS transitions turn abrupt style changes into smooth animations. Instead of a button's color snapping on hover, it fades gracefully. Use this for visual feedback on state changes like :hover or when adding classes with JavaScript.
Why it exists
Web interfaces feel jarring when states change instantly. A button appearing or a menu sliding out without animation can feel cheap or broken. Transitions provide a simple, declarative way to create motion and guide the user's eye, making UIs feel more polished and intuitive.
The mental model
Think of a CSS transition as giving instructions to the browser on how to get from point A to point B. Without a transition, the browser teleports an element's property from its start value to its end value. With a transition, you tell it to travel smoothly over a specific duration, with a certain speed curve, and maybe after a short delay.
How it works
The transition property is a shorthand for several constituent properties: transition-property (which property to animate, e.g., background-color or all), transition-duration (how long it takes, e.g., 300ms), transition-timing-function (the speed curve, e.g., ease-in-out), and transition-delay (how long to wait before starting). You define these on an element's base state. When a property you're watching changes (due to a pseudo-class like :hover or a JavaScript style change), the browser automatically creates the in-between frames.
When to use it
Use transitions for simple state changes to provide user feedback. Examples include: changing a link's color on hover, sliding a mobile menu into view, or fading in a tooltip. They are ideal for micro-interactions that make an interface feel responsive. They are most performant for properties like transform and opacity.
When not to use it
Transitions are not for complex, multi-stage animations; use CSS Animations (@keyframes) for that. Avoid transitioning properties that are expensive to render, like width, height, or margin, as they can cause layout recalculations on every frame, leading to janky performance. Instead, prefer animating transform: scale() or transform: translate().
One canonical example
To make a button's background color fade smoothly when a user hovers over it, define the transition on the base button class. button { background-color: blue; transition: background-color 0.3s ease; } button:hover { background-color: darkblue; } When the user hovers, the background changes from blue to darkblue over 0.3 seconds. When they move the mouse away, it smoothly transitions back.
Interview question
Which scenario best illustrates an appropriate use case for CSS transitions?
- a.Smoothly fading a button's background color when a user hovers over it.Correct
- b.Optimizing page load times by pre-rendering all animated elements.
- c.Implementing a complex loading animation with multiple sequential steps.
- d.Instantly changing an element's display property from 'none' to 'block'.
Why? this is the answer
CSS transitions are designed for smoothly animating simple property changes, such as a button's background color on hover, as described in the card's canonical example. Complex, multi-stage animations are better handled by CSS Animations (@keyframes), and transitions are not for instant changes or page load optimization.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles