What are CSS transitions?
A CSS transition smoothly animates changes between two CSS property values over a specified duration. Instead of a property changing instantly, it gradually transitions from the old value to the new value.
Think of it like fading a light bulb: without a transition, it's on or off instantly. With a transition, it fades in or out smoothly. This creates a polished, professional feel.
Transitions are triggered by changes to CSS properties โ usually from hover states, focus states, or JavaScript that modifies classes or inline styles.
The four transition properties
Every transition needs four pieces of information. You can set them individually or use the shorthand.
Breaking it down
transition-propertyโ Which CSS property to animate. Examples:background-color,opacity,transform,width. Useallto animate every property that changes.transition-durationโ How long the animation takes. Examples:0.3s,500ms. Must include a unit (seconds or milliseconds).transition-timing-functionโ The curve of acceleration. Predefined values:ease,linear,ease-in,ease-out,ease-in-out. You can also usecubic-bezier()for custom curves.transition-delayโ Wait time before the animation starts. Useful for staggering animations. Examples:0.1s,0.2s.
transition: all 0.3s ease; to animate all property changes. But be specific when performance matters โ only animate what you need.
Understanding timing functions
Timing functions control how the animation progresses. Different functions create different feels:
When to use each:
linearโ Mechanical, robotic motion. Good for progress bars or technical animations.easeโ The most natural. Great for general UI interactions.ease-inโ Feels like something is accelerating. Good for objects leaving or gaining speed.ease-outโ Feels like something is decelerating. Good for objects arriving or slowing down.ease-in-outโ Smooth and polished. Good for buttons and interactive elements.cubic-bezier()โ Total control. Use tools like cubic-bezier.com to design custom curves.
Which properties can be transitioned?
Not all CSS properties can be transitioned. Only properties with numerical or color values can smoothly animate. Here's the distinction:
Performance-friendly transitions
For the smoothest animations with 60fps, prefer these properties:
transformโ Scale, rotate, translate, skew. Animates on GPU, very performant.opacityโ Fade in/out. Extremely efficient.filterโ Blur, brightness, contrast. GPU-accelerated.
Avoid animating: width, height, position, top, left โ these cause reflow and are slower.
transform: scale() instead of animating width. Much faster!
Try it yourself: Card hover effects
Here's a more complex example with multiple transitions. Hover over the cards to trigger lift, shadow, and color changes.
Common pitfalls
- Forgetting the duration โ
transition: color;doesn't work. You needtransition: color 0.3s;. - Trying to transition display โ
display: nonecan't be animated. Useopacityandvisibilityinstead. - Applying transition to :hover only โ If you write
.btn:hover { transition: ... }, the animation only applies on hover-in, not hover-out. Always apply the transition to the base state. - Forgetting units โ
transition: all 0.3;is invalid. It must betransition: all 0.3s;. - Over-animating โ Every property doesn't need a transition. Animate only what matters for UX. Too many transitions can feel sluggish.
Task: Create a card that on hover:
- Lifts up with a transform (translateY)
- Gets a bigger, darker shadow (box-shadow)
- Changes background to a light color
- The button inside changes color
- All transitions must be smooth (use transition shorthand)
Use the editors above as reference. Build it in the editor below:
Hint: Set transition: all 0.3s ease; on the card. Then in the :hover state, change background-color, box-shadow, and transform: translateY(-8px). For the button, add a separate transition and change its background-color in the card's :hover state.
Key takeaways
- Transitions smoothly animate property changes over time.
- Every transition needs: property, duration, timing function, and optional delay.
- Use the shorthand:
transition: property duration timing-function delay; - Timing functions (ease, linear, ease-in, ease-out, ease-in-out) control acceleration.
- Only properties with numerical/color values can be transitioned โ not
displayor keywords. transformandopacityare the most performant. Avoid width/height transitions.- Always apply transitions to the base state, not the :hover state.
Test your knowledge
You just built smooth animations.
Imagine what you'll create with transforms and animations.
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ