π Mastering animation-direction in CSS
π What Is animation-direction?
The animation-direction property controls which direction an animation plays during each cycle. With this property, animations can move forward, backward, alternate direction, or reset each time. This gives your animations more natural and dynamic movement. π¬β¨
>>βDirection changes turn simple animations into expressive, lifelike motion.β
π§© Basic Syntax
Syntax
animation-direction: normal | reverse | alternate | alternate-reverse;π― animation-direction Values Explained
| Value | Description | Motion |
|---|---|---|
| normal | Animation runs forward (0% β 100%) | β‘οΈ Default direction |
| reverse | Animation runs backward (100% β 0%) | β¬ οΈ Rewinds |
| alternate | Forward on first run, backward on next | βοΈ Ping-pong effect |
| alternate-reverse | Backward first, forward next | π Reverse ping-pong |
π§ͺ Simple Example
Keyframes
@keyframes move {
from { transform: translateX(0); }
to { transform: translateX(100px); }
}Using animation-direction
.box {
animation-name: move;
animation-duration: 1s;
animation-direction: alternate;
animation-iteration-count: infinite;
}β The box moves right, then left, repeatedly.
π¬ animation-direction Values in Action
1οΈβ£ normal
normal direction
animation-direction: normal;β‘οΈ Runs from 0% β 100% every time
2οΈβ£ reverse
reverse direction
animation-direction: reverse;β¬ οΈ Runs from 100% β 0% every cycle
3οΈβ£ alternate
alternate
animation-direction: alternate;βοΈ Forward β Backward β Forward β Backward
4οΈβ£ alternate-reverse
alternate-reverse
animation-direction: alternate-reverse;π Backward β Forward β Backward β Forward
π§ animation-direction with animation-iteration-count
These two properties work closely together. For example:
Iteration + Direction
.loader {
animation: pulse 1s infinite alternate;
}β Because of alternate, the animation reverses direction every loop.
π¨ Real-World Examples
1οΈβ£ Ping-Pong Loader
Loader Animation
.loader {
width: 40px;
height: 40px;
background: black;
animation: move 0.6s infinite alternate ease-in-out;
}2οΈβ£ Reverse Animation for Exit Motion
Useful when animating modals, tooltips, and dropdown exits.
Reverse Exit
.modal-leave {
animation: fade 0.4s reverse forwards;
}3οΈβ£ Alternate-Reverse for Organic Feel
Organic Wiggle
.wiggle {
animation: wiggle 0.8s infinite alternate-reverse ease-in-out;
}β¨ animation-direction in Shorthand
Shorthand Example
animation: move 1s ease-in-out 0s infinite alternate;| Shorthand Part | Meaning |
|---|---|
| move | animation-name |
| 1s | animation-duration |
| ease-in-out | animation-timing-function |
| infinite | animation-iteration-count |
| alternate | animation-direction |
π οΈ Tips for Using animation-direction
- Use alternate for natural looping motion
- Use reverse for exit or βundoβ animations
- Use alternate-reverse for irregular, lively effects
- Combine with animation-fill-mode for smooth start/end behavior
π Debugging Tips
- Check your keyframe direction (0% β 100%)
- Ensure animation-duration is not 0
- Use DevTools β Animations panel to preview direction changes
π Helpful Resources
>>βDirection brings intention β choose it based on how your UI should feel.β β¨