CSS animation-direction Property
The animation-direction property specifies whether a CSS animation plays forward, backward, or alternates direction between iterations.
It controls the order in which keyframes are played without changing the keyframe definitions. This property is useful for reversing animations and creating smooth back-and-forth effects.
animation-direction Syntax
selector {
animation-direction: normal;
}
Replace normal with reverse, alternate, or alternate-reverse to change the playback direction.
.forward {
animation-direction: normal;
}
.backward {
animation-direction: reverse;
}
.back-and-forth {
animation-direction: alternate;
}
.reverse-first {
animation-direction: alternate-reverse;
}
For multiple animations, specify a comma-separated list of direction values.
.multiple {
animation-direction: normal, alternate;
}
The property also accepts the global CSS values inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
normal |
Plays the animation from its starting keyframe to its ending keyframe on every iteration. This is the initial value. |
reverse |
Plays the animation from its ending keyframe to its starting keyframe on every iteration. |
alternate |
Plays forward on the first iteration, backward on the second, and continues alternating. |
alternate-reverse |
Plays backward on the first iteration, forward on the second, and continues alternating. |
The property applies to all elements, is not inherited, and has an initial value of normal. Its computed value is the specified list of direction keywords.
The animation-direction property itself is not animatable. It controls the playback behavior of other animated properties.
How animation-direction Works
CSS animations use keyframes to define how an element changes during an animation cycle. The animation-direction property determines the order in which those keyframes are played.
Consider an animation that moves an element horizontally:
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
.box {
animation-name: slide;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-direction: alternate;
}
With alternate, the element moves from left to right during the first iteration and returns from right to left during the second.
The animation continues alternating as long as it repeats. If the iteration count is 1, only the first direction is used.
Reversing an animation also reverses how its timing function is applied. For example, an ease-in animation played in reverse behaves like an ease-out animation.
Forward and Reverse Playback
The normal and reverse values determine whether each iteration follows the keyframes in their original order or in reverse.
Normal Playback
.normal-box {
animation: slide 2s ease-in-out infinite;
animation-direction: normal;
}
The element moves from the from keyframe toward the to keyframe on every iteration. At the beginning of the next iteration, it returns to the starting keyframe.
Reverse Playback
.reverse-box {
animation: slide 2s ease-in-out infinite;
animation-direction: reverse;
}
The element begins at the ending keyframe and moves toward the starting keyframe on every iteration.
The reverse value changes the playback direction without requiring a second set of keyframes.
Alternating Animation Directions
The alternate and alternate-reverse values are useful for continuous motion that should move back and forth without jumping to its starting position between iterations.
Using alternate
.alternate-box {
animation: slide 2s ease-in-out infinite alternate;
}
The first iteration plays forward, the second plays backward, and subsequent iterations continue alternating.
Using alternate-reverse
.alternate-reverse-box {
animation: slide 2s ease-in-out infinite alternate-reverse;
}
The first iteration plays backward, the second plays forward, and subsequent iterations continue alternating.
For animations that repeat a fixed number of times, the final visual state depends on the direction and number of iterations. A fractional iteration count can also cause an animation to finish partway through a cycle.
Multiple Animation Directions
When an element has several animations, you can specify a different playback direction for each one.
.animated-element {
animation-name: move, rotate;
animation-duration: 2s, 4s;
animation-iteration-count: infinite, infinite;
animation-direction: alternate, reverse;
}
The move animation alternates between forward and backward playback, while the rotate animation runs backward on every iteration.
Direction values are matched to animations in the order specified by animation-name.
If fewer direction values are provided than animations, the browser repeats the direction list as needed.
.three-animations {
animation-name: move, rotate, fade;
animation-direction: normal, alternate;
}
Here, move uses normal, rotate uses alternate, and fade uses normal.
animation-direction Example
The following example compares all four animation directions. Each square moves along the same horizontal path, but the direction setting changes how it travels.
<div class="direction-demo">
<div class="direction-row">
<strong>normal</strong>
<div class="direction-track"><div class="direction-box normal"></div></div>
</div>
<div class="direction-row">
<strong>reverse</strong>
<div class="direction-track"><div class="direction-box reverse"></div></div>
</div>
<div class="direction-row">
<strong>alternate</strong>
<div class="direction-track"><div class="direction-box alternate"></div></div>
</div>
<div class="direction-row">
<strong>alternate-reverse</strong>
<div class="direction-track"><div class="direction-box alternate-reverse"></div></div>
</div>
</div>
.direction-demo {
display: grid;
gap: 12px;
}
.direction-row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.direction-row strong {
flex: 0 0 130px;
}
.direction-track {
flex: 1 1 140px;
min-width: 0;
height: 40px;
padding: 5px;
background-color: #f1f3f5;
border: 1px solid #ccc;
box-sizing: border-box;
}
.direction-box {
width: 30px;
height: 28px;
background-color: #1769aa;
animation: direction-move 3s linear infinite;
}
.direction-box.normal {
animation-direction: normal;
}
.direction-box.reverse {
animation-direction: reverse;
}
.direction-box.alternate {
animation-direction: alternate;
}
.direction-box.alternate-reverse {
animation-direction: alternate-reverse;
}
@keyframes direction-move {
from {
transform: translateX(0);
}
to {
transform: translateX(100px);
}
}
@media (prefers-reduced-motion: reduce) {
.direction-box {
animation: none;
}
}
All four squares use the same keyframes and three-second duration. Their direction values determine whether they move forward, backward, or alternate between the two.
The normal and reverse animations restart from their respective starting positions on each iteration. The alternate and alternate-reverse animations move back and forth without that restart.
If reduced motion is enabled, the squares remain stationary.
Direction and Fill Mode
The animation-fill-mode property controls whether keyframe styles remain in effect before or after an animation's active period.
When an animation plays in reverse, the starting and ending keyframes used for fill behavior also change.
.box {
animation: slide 2s ease-in-out 1s 1 reverse both;
}
In this example, the animation has a one-second delay and plays backward once. The both fill mode applies the appropriate starting keyframe during the delay and retains the ending state after completion.
Because the direction is reverse, the animation starts at the 100% keyframe and finishes at the 0% keyframe.
For alternating animations, the final fill state depends on the iteration count and whether the last iteration plays forward or backward.
Important Considerations
Keep the following points in mind when using animation-direction:
- Iteration count: Alternating directions become most apparent when an animation repeats at least twice.
- Timing functions: Reverse playback also reverses the timing curve, so acceleration and deceleration occur in the opposite order.
- Keyframes: The property changes playback order without modifying the
@keyframesdefinition. - Fill modes: The direction and iteration count affect which keyframe styles remain after a finite animation finishes.
- Shorthand declarations: The
animationshorthand can specify the direction directly, as inanimation: slide 2s ease infinite alternate;. - Accessibility: Use
prefers-reduced-motionto reduce or disable continuous movement when requested.
Use alternate or alternate-reverse when you want an animation to move smoothly between its endpoints without restarting from the same side on every iteration.
Browser Support
The animation-direction property has Baseline Widely available status and has been supported across major browser engines since September 2015, including Chrome, Edge, Firefox, and Safari.
All four direction values are broadly supported in modern browsers.
Visit Can I Use? to check current browser compatibility.
Specifications
The animation-direction property is defined in the CSS Animations Level 1 specification.
