CSS animation Property

The animation shorthand property controls the timing, duration, repetition, and playback behavior of CSS keyframe animations.

It combines several animation properties into one declaration. When used with the @keyframes at-rule, it allows elements to change their appearance or position over time without requiring JavaScript.

animation Syntax

selector {
  animation: animation-name duration timing-function delay iteration-count direction fill-mode play-state;
}

In this syntax, each component represents a value accepted by one of the animation longhand properties. You can omit optional components, and the browser uses their initial values.

A typical animation declaration looks like this:

.box {
  animation: slide-in 2s ease-in-out 0.5s infinite alternate;
}

This animation uses the slide-in keyframes, lasts two seconds per iteration, waits half a second before starting, repeats indefinitely, and alternates direction.

The property also accepts the global CSS values inherit, initial, revert, revert-layer, and unset.

The animation name is generally best placed last in the shorthand because some names can be interpreted as other animation values.

Property Values

The animation shorthand combines the following properties:

Property Description Initial Value
animation-name Specifies the name of the @keyframes animation. none
animation-duration Sets how long one animation iteration takes. 0s for ordinary time-based animations
animation-timing-function Controls the rate of change during the animation. ease
animation-delay Sets the waiting time before an animation begins. 0s
animation-iteration-count Specifies how many times the animation repeats. 1
animation-direction Controls whether an animation runs forward, backward, or alternates direction. normal
animation-fill-mode Controls how keyframe styles apply before or after the animation's active period. none
animation-play-state Specifies whether an animation is running or paused. running
animation-timeline Specifies the timeline controlling animation progress. In current interoperable shorthand usage, it is reset to auto. auto

The shorthand also resets certain related properties, including animation-composition, animation-range-start, and animation-range-end, without allowing their values to be specified directly in the shorthand. Newer animation features can introduce additional reset-only properties.

The animation property is not inherited. Because it is a shorthand, its initial behavior is determined by the initial values of its constituent properties.

How animation Works

A CSS animation requires a set of keyframes and an animation declaration that references them.

The @keyframes at-rule defines the styles at different points in the animation. The animation property then determines how those styles are applied over time.

@keyframes fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}
.fade-box {
  animation: fade-in 2s ease-in-out;
}

When the animation begins, the element transitions from transparent to fully visible over two seconds.

Unless a fill mode specifies otherwise, the element uses its normal CSS styles outside the animation's active period.

The animation starts when the browser applies the animation to the element. This commonly happens when the page loads or when a class or other style change introduces the animation.

Defining Animation Keyframes

Keyframes describe the styles an element should have at selected points during an animation.

You can use from and to, which represent 0% and 100%, or specify intermediate percentages for more complex effects.

@keyframes move-and-fade {
  0% {
    opacity: 0;
    transform: translateX(0);
  }
  50% {
    opacity: 1;
    transform: translateX(50px);
  }
  100% {
    opacity: 1;
    transform: translateX(100px);
  }
}
.animated-box {
  animation: move-and-fade 3s ease-in-out forwards;
}

This example starts with a transparent element, moves it horizontally, and makes it fully visible halfway through the animation.

The forwards fill mode allows the final keyframe styles to remain in effect after the animation finishes, subject to the normal CSS cascade.

Only properties that support animation can interpolate smoothly between keyframes. Other properties may change discretely or require specific conditions to animate.

Animation Timing and Repetition

The animation shorthand allows you to control animation speed, repetition, direction, and delays.

Animation Duration and Delay

.box {
  animation: fade-in 2s ease 1s;
}

The first time value, 2s, is the duration. The second, 1s, is the delay before the animation starts.

Negative delays are also valid. For example, -1s starts a time-based animation as though one second of its progress has already elapsed.

Animation Timing Functions

Timing functions determine how animation progress changes over time.

Value Description
ease Starts slowly, accelerates, and slows toward the end.
linear Uses a constant rate of progress.
ease-in Starts slowly and accelerates.
ease-out Starts quickly and slows toward the end.
ease-in-out Starts and ends slowly.
steps() Divides animation progress into discrete steps.
cubic-bezier() Defines a custom timing curve using control points.

Repetition and Direction

.repeating-box {
  animation: move 2s linear infinite alternate;
}

The infinite value repeats the animation continuously. The alternate value reverses its direction on every other iteration.

You can also specify a number of iterations, including fractional values such as 2.5.

Multiple Animations

An element can run multiple CSS animations simultaneously by separating animation definitions with commas.

.multi-animation {
  animation: fade-in 2s ease forwards, slide-in 3s ease-out forwards;
}

In this example, the first animation controls opacity while the second controls movement, assuming their keyframes affect those respective properties.

Each animation can have its own duration, delay, timing function, and repetition settings.

If multiple animations affect the same property, the final result depends on their composite behavior and their order in the animation list. The animation-composition property provides additional control over how animation effects combine.

animation Example

The following example animates a blue square horizontally while changing its background color. The animation repeats continuously and alternates direction.

<div class="animation-demo">
  <div class="animation-box"></div>
</div>
.animation-demo {
  position: relative;
  width: 320px;
  max-width: 100%;
  height: 90px;
  padding: 10px;
  border: 1px solid #ccc;
  box-sizing: border-box;
}
.animation-box {
  width: 50px;
  height: 50px;
  background-color: #1769aa;
  animation: move-box 3s ease-in-out infinite alternate;
}
@keyframes move-box {
  from {
    transform: translateX(0);
    background-color: #1769aa;
  }
  to {
    transform: translateX(240px);
    background-color: #198754;
  }
}
@media (prefers-reduced-motion: reduce) {
  .animation-box {
    animation: none;
  }
}

The move-box animation lasts three seconds per iteration. It moves the square from left to right, changes its color from blue to green, and reverses direction on alternating iterations.

Browser Result

The square moves repeatedly while the animation is active. If reduced motion is enabled in the user's system settings, the example remains stationary.

For responsive layouts, consider using percentage-based positioning or other flexible techniques rather than fixed movement distances that may extend beyond a narrow container.

Shorthand vs. Longhand Properties

The animation shorthand provides a compact alternative to writing each animation property separately.

For example, these longhand declarations:

.box {
  animation-name: fade-in;
  animation-duration: 2s;
  animation-timing-function: ease-in-out;
  animation-delay: 0.5s;
  animation-iteration-count: 3;
  animation-direction: alternate;
  animation-fill-mode: forwards;
  animation-play-state: running;
}

Can be expressed using one shorthand declaration:

.box {
  animation: fade-in 2s ease-in-out 0.5s 3 alternate forwards running;
}

Both examples configure the same ordinary time-based animation. The shorthand is useful for concise declarations, while longhand properties can make individual settings easier to read or override.

Important: The shorthand resets omitted animation settings to their initial values. When working with scroll-driven animations, declare animation-timeline and animation-range after the shorthand to preserve their intended values.

.scroll-animation {
  animation: fade-in 1s linear both;
  animation-timeline: view();
  animation-range: entry 0% cover 40%;
}

Support for scroll-driven animation features varies by browser version. The basic time-based animation shorthand has broader support.

Accessibility Considerations

Animations can help communicate changes in an interface, but excessive movement may distract users or cause discomfort.

Use the prefers-reduced-motion media feature to respect the user's system preference for reduced animation.

.animated-element {
  animation: fade-in 1s ease-out;
}
@media (prefers-reduced-motion: reduce) {
  .animated-element {
    animation: none;
  }
}

This example disables the animation when reduced motion is requested.

Keep essential information available without animation, and avoid effects that flash rapidly or interfere with reading and interaction.

Browser Support

The animation shorthand property has Baseline Widely available status and is supported in modern browsers, including Chrome, Edge, Firefox, and Safari.

Some newer animation features, including scroll-driven timelines, animation composition, and animation triggers, have different browser support levels.

Visit Can I Use? to check current compatibility.

Specifications

The animation shorthand property is defined in the CSS Animations specifications. CSS Animations Level 2 extends the animation model with additional features, including animation timelines and composition.

CSS Animations Level 1
CSS Animations Level 2