Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › transition

CSS Reference

transition

Animates changes between two states. The simplest way to add motion to any UI element.

Last updated: October 2026

Definition: The transition property smoothly animates a change between two states of a CSS property. It requires a state change to trigger — like a hover, focus, or class toggle. It supports four sub-properties: property, duration, timing-function, and delay.

🎬 Live Example

Hover each box to see the transition. Each one uses a different timing function.

ease
ease-in-out (0.5s)
bounce

💡 The third box uses a custom cubic-bezier for a bouncy effect. Notice how it overshoots before settling.

📝

Basic Syntax

/* Shorthand: property duration timing-function delay */
.button {
    transition: background-color 0.3s ease;
}

.button:hover {
    background-color: #8b5cf6;
}

The transition sits on the base state — not on the :hover state.

🎯 All transition Sub-Properties

Property Purpose
transition-propertyWhich property to animate. all, opacity, transform, etc.
transition-durationHow long the transition takes. 0.3s, 200ms.
transition-timing-functionEasing curve. ease, linear, ease-in-out, cubic-bezier().
transition-delayWait time before the transition starts.
1

The Four Shorthand Values

/* Property duration timing-function delay */
.element {
    transition: opacity 0.3s ease 0s;
}

/* All four values are optional except duration defaults */
.transition-a {
    transition: opacity 0.3s;             /* duration only */
}

.transition-b {
    transition: opacity 0.3s ease;        /* + timing function */
}

.transition-c {
    transition: opacity 0.3s ease 100ms;  /* + delay */
}
2

The Easing Functions

Value Behavior
linearConstant speed
easeSlow start, fast middle, slow end (default)
ease-inSlow start, fast end
ease-outFast start, slow end
ease-in-outSlow start and end, fast middle
cubic-bezier()Custom curve. 4 values between 0 and 1 (or beyond for overshoot)
steps(n, end)Instant transitions at N steps — good for sprite animations
3

Common transition Recipes

/* Button hover */
.button {
    transition: background-color 0.2s ease, transform 0.2s ease;
}

/* Card lift */
.card {
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

/* Link underline */
.link {
    transition: color 0.15s ease;
}

/* Input focus */
.input {
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

/* Modal fade in */
.modal {
    transition: opacity 0.2s ease, transform 0.2s ease;
}

/* Menu slide */
.menu {
    transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}
4

Transitioning Multiple Properties

/* Comma-separate for different timings */
.card {
    transition:
        background-color 0.2s ease,
        transform 0.3s ease,
        box-shadow 0.3s ease;
}

/* Same timing for all */
.card {
    transition: all 0.3s ease;
}

/* Custom durations per property */
.button {
    transition:
        opacity 0.15s ease-out,
        transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

transition: all works but is usually less performant and less predictable. Prefer listing exact properties.

5

Which Properties Are Animatable?

Not all CSS properties can be transitioned. The most common animatable properties:

  • opacity — fades
  • transform — GPU accelerated, ideal
  • color / background-color — smooth color shifts
  • border-color — highlight effects
  • box-shadow — depth animation
  • filter — blur, brightness
  • width, height, top, left — triggers layout (slower)

💡 Performance tip: transform and opacity are the fastest properties to animate because they don't trigger reflows. Prefer them for hover effects and animations.

🎯 transition vs animation

Feature transition animation
TriggerState change (hover, focus)Runs automatically
KeyframesNoYes (@keyframes)
Intermediate stepsNo — just start and endYes — unlimited
LoopNoYes (infinite)
ReversibleAutomatic (transition back)Manual
ComplexitySimpleMore powerful

Use transition for hover effects, focus states, and simple state changes. Use animation for loaders, attention-grabbing effects, and multi-step animations.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the CSS transition property?

The transition property animates changes between two states of an element. When a property changes (for example, on hover), the transition smoothly interpolates between the old value and the new value over a specified duration.

What is the difference between transition and animation?

transition animates between two states — a start and an end — triggered by a state change. animation can loop, use keyframes, and control multiple intermediate steps. Transitions are simpler; animations are more powerful.

What does transition: all do?

transition: all applies the transition to every animatable property on the element. It works, but it's usually better to specify exact properties for performance and predictability. Use all for simple cases and be explicit elsewhere.

How do I use easing functions in CSS transitions?

Set the timing function as the third value: transition: opacity 0.3s ease-in-out. Common values are ease, ease-in, ease-out, ease-in-out, linear, and cubic-bezier(x1, y1, x2, y2) for custom curves.

Should I always use transitions?

No. Respect the prefers-reduced-motion media query. Users who've opted out of motion should not see transitions or animations. Always disable non-essential motion for them.

🎯 What's Next?

← All CSS Reference