🔁 Mastering animation-iteration-count in CSS

📌 What Is animation-iteration-count?

The animation-iteration-count property controls how many times an animation repeats. This lets you loop animations, repeat them a specific number of times, or even run them infinitely. It pairs with animation-name and animation-duration to control playback. 🔄✨

>>“Repetition turns motion into pattern — and iteration count decides the pattern.”

🧩 Basic Syntax

Syntax

animation-iteration-count: 1;
animation-iteration-count: 3;
animation-iteration-count: infinite;

✔ The default value is 1 (animation plays once).
✔ Use infinite for endless looping.

🔢 Using Numbers

Repeat a Specific Number of Times

animation-iteration-count: 5;

✔ Animation repeats exactly 5 times and then stops.

♾️ Using infinite

Infinite Loop

animation-iteration-count: infinite;

✔ Great for loaders, spinners, and ongoing animations.

🧪 Example Animation

@keyframes Example

@keyframes bounce {
  0%   { transform: translateY(0); }
  50%  { transform: translateY(-20px); }
  100% { transform: translateY(0); }
}

.ball {
  animation-name: bounce;
  animation-duration: 1s;
  animation-iteration-count: infinite;
}

✔ The element will bounce continuously.

🎯 Combining iteration count with direction

When combined with animation-direction, you can create beautiful “ping-pong” effects.

Reverse Ping-Pong

animation: move 1s infinite alternate;

✔ Plays forward → backward → forward → backward forever.

⏳ Fractional Iteration Counts

You can even use decimal values! A fractional number means the animation plays partially.

Fractional Example

animation-iteration-count: 2.5;

✔ Plays fully twice, then half-way on the third loop.

🎨 Real-World Examples

1️⃣ Typing Effect Loop

Typing Example

@keyframes blink {
  0%, 50% { opacity: 1; }
  51%, 100% { opacity: 0; }
}

.cursor {
  animation: blink 1s infinite;
}

2️⃣ Pulsing Button

Pulse Loop

@keyframes pulse {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.button {
  animation: pulse 0.8s infinite ease-in-out;
}

✨ iteration count in Shorthand

Animation Shorthand

animation: bounce 1.2s ease-in-out 0s 3;
Shorthand PartMeaning
bounceanimation-name
1.2sanimation-duration
ease-in-outanimation-timing-function
0sanimation-delay
3animation-iteration-count

🛠️ Tips for Using Iteration Count

  • Use infinite sparingly — too much looping can distract users
  • Use fractional values for advanced sequences
  • Combine with delays for staggered effects
  • Use iteration count + alternate for fluid looping
  • Stop looping animations when they’re no longer needed for UX

🔍 Debugging Tips

  • Ensure animation-duration is not 0 (or animation won’t run)
  • Check the keyframe names carefully
  • Use DevTools Animation panel to visualize cycles
  • Ensure infinite loops don’t block transitions or user interaction

🔗 Helpful Resources

>>“Iteration count defines rhythm — use it to choreograph smooth, meaningful motion.” 🔁✨