πŸ”„ 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

ValueDescriptionMotion
normalAnimation runs forward (0% β†’ 100%)➑️ Default direction
reverseAnimation runs backward (100% β†’ 0%)⬅️ Rewinds
alternateForward on first run, backward on next↔️ Ping-pong effect
alternate-reverseBackward 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 PartMeaning
moveanimation-name
1sanimation-duration
ease-in-outanimation-timing-function
infiniteanimation-iteration-count
alternateanimation-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.” ✨