The animation-direction Property
What you'll learn: How to control whether your CSS animations play forward, backward, or alternate directions with each cycle.
What Is animation-direction?
When you create an animation with @keyframes, it normally plays from start to finish, then jumps back to the beginning if it repeats. The animation-direction property lets you change this default behavior—making animations run in reverse, or smoothly alternate back and forth like a pendulum.
Think of it like a music player: normally a song plays from beginning to end (normal), but you can also play it backwards (reverse), or have it ping-pong between forward and backward playback (alternate).
The Four Direction Values
.box {
animation-name: slide;
animation-duration: 2s;
animation-iteration-count: infinite;
animation-direction: normal; /* default: 0% → 100% → jump back → 0% → 100%... */
}
normal– Plays forward every time (0% to 100%)reverse– Plays backward every time (100% to 0%)alternate– Plays forward, then backward, then forward... (smooth back-and-forth)alternate-reverse– Plays backward first, then forward, then backward...
Practical Example
@keyframes slide {
from { transform: translateX(0); }
to { transform: translateX(200px); }
}
.alternate-box {
animation-name: slide;
animation-duration: 1s;
animation-iteration-count: infinite;
animation-direction: alternate; /* Smoothly slides back and forth */
}
The alternate value is especially useful because it creates seamless looping animations without the jarring "jump back" effect.
Key Takeaway: The animation-direction property controls playback direction—use normal or reverse for one-way animations, and alternate or alternate-reverse for smooth back-and-forth effects.