CSS animation-iteration-count
The animation-iteration-count
property in CSS specifies the number of times an animation should repeat. You can define it to run a fixed number of times, indefinitely, or inherit its value from its parent element.
Syntax of animation-iteration-count
</>
Copy
animation-iteration-count: number | infinite | initial | inherit;
Values
Value | Description |
---|---|
number | Specifies the number of times the animation should repeat. For example, 3 means the animation runs three times. |
infinite | The animation will repeat indefinitely. |
initial | Sets the property to its default value (1 ). |
inherit | Inherits the value from the parent element. |
Default Value
1
Examples for animation-iteration-count
Using the CSS animation-iteration-count Property
The following examples demonstrate how the animation-iteration-count
property can be used to control the number of times an animation repeats.
</>
Copy
/* Play animation three times */
.element {
animation-iteration-count: 3;
}
/* Play animation indefinitely */
.element {
animation-iteration-count: infinite;
}
/* Inherit the value from the parent element */
.element {
animation-iteration-count: inherit;
}
Example 1: Applying animation-iteration-count
The following example demonstrates the use of animation-iteration-count
with different values for a simple animation that moves an element horizontally.
index.html
</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<style>
@keyframes slide {
from {
transform: translateX(0);
}
to {
transform: translateX(200px);
}
}
.single {
animation: slide 2s;
animation-iteration-count: 1;
background-color: lightgreen;
margin: 10px;
padding: 10px;
}
.multiple {
animation: slide 2s;
animation-iteration-count: 3;
background-color: lightblue;
margin: 10px;
padding: 10px;
}
.infinite {
animation: slide 2s infinite;
background-color: lightcoral;
margin: 10px;
padding: 10px;
}
div {
width: 200px;
}
</style>
</head>
<body>
<h1>CSS animation-iteration-count Example</h1>
<div class="single">Plays Once</div>
<div class="multiple">Plays Three Times</div>
<div class="infinite">Plays Indefinitely</div>
</body>
</html>
single
- The
animation-iteration-count
is set to1
. - The animation runs exactly one time from the
from
keyframe (translateX(0)
) to theto
keyframe (translateX(200px)
). - After completing the animation once, the element stops at its final position if combined with
animation-fill-mode: forwards
or reverts to its original state otherwise. - The result is a light green box that slides to the right only once and then stops.
multiple count:3
- The
animation-iteration-count
is set to3
. - The animation runs three times in succession, restarting at the
from
keyframe each time. - After completing the animation three times, the element stops at its final position if combined with
animation-fill-mode: forwards
or reverts to its original state otherwise. - The result is a light blue box that slides to the right three times and then stops.
infinite
- The
animation-iteration-count
is set toinfinite
. - The animation runs continuously without stopping, repeatedly transitioning from the
from
keyframe to theto
keyframe. - This creates a loop where the animation restarts as soon as it completes, providing an ongoing effect.
- The result is a light coral box that slides to the right repeatedly and never stops.
Browser Support
The animation-iteration-count
property is supported in most modern browsers. Below is a compatibility table:
Browser | Version |
---|---|
Chrome | 43 |
Edge | 10 |
Firefox | 16 |
Safari | 9 |
Opera | 30 |