# Transitions

> Smoothly interpolate CSS property changes over time.

Subject: CSS | Lesson 30 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/transitions
Previous: [object-fit & aspect-ratio](https://codewithanimation.in/css/object-fit-and-aspect-ratio.md) | Next: [Keyframe Animations](https://codewithanimation.in/css/keyframe-animations.md)

## What are CSS Transitions?

CSS Transitions allow you to change property values smoothly, over a given duration, rather than having them snap instantly.

They are the easiest way to add polish and interaction feedback to your interface, such as a button gently highlighting when a user hovers over it.

## Why they matter

Without transitions, state changes (like `:hover`, `:focus`, or toggling a class) feel abrupt and unnatural.

Transitions provide crucial visual feedback, confirming to the user that the interface has registered their action.

## How they work

You define a transition on the element's base state, not on the hover state. This ensures the animation plays both when the state is applied and when it is removed.

The `transition` shorthand takes four values: the property to animate, the duration, the timing function (the pacing), and an optional delay.

For example, `transition: transform 0.3s ease-out;` tells the browser to smoothly interpolate any changes to the `transform` property over 0.3 seconds.

## Common mistake: Animating layout triggers jank

The most common mistake is transitioning layout properties like `width`, `height`, `margin`, or `top`. Changing these forces the browser to recalculate the entire page layout on every single frame, causing choppy performance.

For buttery smooth 60fps animations, stick exclusively to transitioning `transform` and `opacity`. They run on the GPU and do not trigger layout recalculations.

## Tip: Avoid the 'all' keyword

While it is tempting to write `transition: all 0.3s;`, this forces the browser to watch every single animatable property for changes.

Always be explicit about what you are animating (e.g., `transition: background-color 0.2s, transform 0.2s;`) to ensure predictable behavior and optimal performance.

## Try it: Try it yourself

Hover over the button in the lab to see the transition in action, then tweak the duration and timing function.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/transitions

## Check yourself

### Question 1

Which of the following is the most performant property to transition?

- A. width
- B. margin-left
- C. transform

Correct answer: C

For smooth 60fps animations, stick exclusively to transitioning `transform` and `opacity`. They run efficiently on the GPU without triggering layout recalculations.

- Why not A: Animating width triggers a layout recalculation on every frame, which is terrible for performance.
- Why not B: Animating margins forces the browser to reflow the layout, causing choppy performance.

### Question 2

Where is the correct place to write the transition rule for a hover effect?

- A. Inside the :hover pseudo-class.
- B. On the base element rule.
- C. On the body element.

Correct answer: B

You define the transition on the base element. This ensures the browser knows how to interpolate the property both when applying the hover state and when reverting back to the base state.

- Why not A: If you put it here, the transition only applies when entering the hover state. When the user moves the mouse away, the element snaps back instantly.
- Why not C: This would apply the transition to the body itself, not the specific element you want to animate.

### Question 3

What is the default duration of a CSS transition if none is specified?

- A. 1 second
- B. 0 seconds (instant)
- C. auto

Correct answer: B

If you declare a transition property but forget the duration, it defaults to 0s, meaning the change happens instantly.

- Why not A: The browser does not default to 1 second. You must explicitly define a duration.
- Why not C: There is no 'auto' keyword for transition duration.

## Remember this

- Transitions only work on properties with calculable intermediate values (like colors or numbers, but not `display: none`).
- You must specify a duration; if you omit it, the default is 0s (instant).
- Place the `transition` rule on the base element, not inside the `:hover` pseudo-class, so the reverse animation plays when the hover ends.
