The animation-iteration-count
What you'll learn: How to control how many times your CSS animation plays before stopping—or make it loop forever.
What Is animation-iteration-count?
When you create an animation with @keyframes, it plays once by default and then stops. The animation-iteration-count property lets you decide exactly how many times that animation should repeat. Think of it like setting the number of laps a runner completes around a track—you can specify 3 laps, 10 laps, or tell them to keep running indefinitely.
The Syntax
.element {
animation-name: slide;
animation-duration: 2s;
animation-iteration-count: 3; /* plays 3 times */
}
You can use:
- A number (like
1,3,10.5) — the animation repeats that many times infinite— the animation loops forever without stopping
Infinite Loops
The most common use case for repeated animations is creating infinite loops for loading spinners, pulsing buttons, or continuous background effects:
.spinner {
animation-name: rotate;
animation-duration: 1s;
animation-iteration-count: infinite; /* never stops */
}
Fractional Counts
You can even use decimals like 2.5, which means the animation plays twice fully, then halfway through a third time before stopping. This gives you precise control over where the animation ends.
Real-World Analogy
Imagine a music playlist. animation-iteration-count: 1 is like playing a song once. Setting it to 3 replays the song three times. Using infinite is like putting the song on repeat—it keeps playing until you turn it off.
Key Takeaway: Use animation-iteration-count to specify how many times an animation repeats—use a number for a specific count or infinite to create never-ending animations.