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.

Key difference:
Transitions are triggered by property changes. They're different from animations (which run on their own timeline). Use transitions for interactive effects, animations for repeating or autonomous motion.

The four transition properties

Every transition needs four pieces of information. You can set them individually or use the shorthand.

transitions.css Individual properties

Breaking it down

  • transition-property โ€” Which CSS property to animate. Examples: background-color, opacity, transform, width. Use all to 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 use cubic-bezier() for custom curves.
  • transition-delay โ€” Wait time before the animation starts. Useful for staggering animations. Examples: 0.1s, 0.2s.
Pro tip:
Use 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:

timing.css Different timing functions

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.

Try it yourself: Button hover effects

Edit the code below to see different transition effects. Hover over the buttons to trigger the transitions.

โœ๏ธ Try It Yourself โ€” Button Transitions
Preview

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:

transition-properties.css Transitionable vs non-transitionable

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.

Pro tip:
If you need to expand an element, use 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.

โœ๏ธ Try It Yourself โ€” Card Transitions
Preview

Common pitfalls

  • Forgetting the duration โ€” transition: color; doesn't work. You need transition: color 0.3s;.
  • Trying to transition display โ€” display: none can't be animated. Use opacity and visibility instead.
  • 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 be transition: all 0.3s;.
  • Over-animating โ€” Every property doesn't need a transition. Animate only what matters for UX. Too many transitions can feel sluggish.
Challenge: Interactive Hover Card

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 display or keywords.
  • transform and opacity are the most performant. Avoid width/height transitions.
  • Always apply transitions to the base state, not the :hover state.

Test your knowledge

Question 1 of 5
What does transition-duration control?
Question 2 of 5
Which timing function starts slow and speeds up?
Question 3 of 5
Can you use CSS transitions to animate the display property?
Question 4 of 5
What's the correct order for the transition shorthand?
Question 5 of 5
Which properties are best for performance when creating transitions?
0
/5
Great work! You understand CSS transitions.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’