ComparisonComparisons

transition vs animation

transition animates a change between two states after a trigger, e.g. :hover. animation plays back steps from @keyframes on its own, even in a loop.

On this page

Side-by-side comparison

#
  • Needs a trigger (e.g. :hover)

    transition
    Yes, nothing happens without a state change
    animation
    No, it starts on its own once the rule is assigned
  • Number of steps

    transition
    Two states: starting and ending
    animation
    As many as you like, defined in @keyframes
  • Looping

    transition
    Not possible
    animation
    Yes, with animation-iteration-count: infinite
  • Starts without user interaction

    transition
    No
    animation
    Yes
  • Pausing and resuming

    transition
    No
    animation
    Yes, with animation-play-state
  • Typical use

    transition
    Hover on buttons, smooth menu openings
    animation
    Spinners, pulsing, loading indicators

Example

#
CSS
.btn {
  background: #7c3aed;
  transition: background-color 0.2s;
}

.btn:hover {
  background: #6d28d9;
}

.spinner {
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { rotate: 360deg; }
}
Preview

Definition and usage

#

The transition property needs a trigger: a state change, for example :hover, a class added by JavaScript, or a change in a @media query. It only interpolates between two values, the one before the change and the one after it, and plays once per change.

The animation property relies on an @keyframes rule, where you describe as many steps as you like at different percentages of the timeline. It starts on its own, right after the rule is assigned to an element, with no user interaction at all. Thanks to animation-iteration-count it can loop, and animation-direction and animation-play-state give you control over direction and pausing.

In practice, transition is enough for simple interface effects: highlighting a button, opening a menu, changing color on focus. animation works well where motion needs to happen on its own, without end: loading spinners, pulsing badges, welcome entrance effects for elements on the page.

Syntax

#
Syntax
transition: <property> <duration> <timing-function>? <delay>?;
animation: <keyframes-name> <duration> <timing-function>? <delay>? <iteration-count>?;

More examples

#
The animation starts on its own, with no interaction
CSS
@keyframes pulse {
  50% { transform: scale(1.15); }
}

.badge {
  animation: pulse 1.2s ease-in-out infinite;
}
Preview

There’s nothing to click or hover: the animation starts right away once the page loads.

Without a state change, transition does nothing
CSS
.card {
  transform: scale(1);
  transition: transform 0.3s;
}
Preview

On its own, transition doesn’t animate anything. It needs a second rule, e.g. :hover, that changes the transform value.

Good practices

#
  • If something needs to happen without user involvement and in a loop, reach for animation.
  • transition is simpler and lighter for hover or focus effects on one or two properties.
  • You can combine them: an entrance animation with @keyframes, then transition on :hover for the same element.

Spotted a mistake or missing an example? Write to us.