The Wayback Machine - https://web.archive.org/web/20250401023925/https://www.geeksforgeeks.org/css-animation-iteration-count-property/
Open In App

CSS animation-iteration-count Property

Last Updated : 23 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The animation-iteration-count property in CSS specifies the number of times an animation should be repeated. It can also be set to infinite to repeat the animation indefinitely.

Syntax

animation-iteration-count: number | infinite | initial | inherit;

Property Value

  • number: Specifies the number of times the animation should be played. The default is 1.
  • infinite: The animation will repeat indefinitely.
  • initial: Sets the property to its default value.
  • inherit: Inherits the property from its parent element.

Example: HTML program to illustrate animation-iteration-count 

html
<!DOCTYPE html>
<html>
  
<head>
    <title>
        CSS | animation-iteration-count Property
    </title>
    <style>
        .geeks {
            font-size: 40px;
            text-align: center;
            font-weight: bold;
            color: #090;
            padding-bottom: 5px;
            font-family: Times New Roman;
        }

        .geeks1 {
            font-size: 17px;
            font-weight: bold;
            text-align: center;
            font-family: Times New Roman;
        }

        #one {
            animation-name: example;
            animation-duration: 2s;

            /* Animation will be repeated twice */
            animation-iteration-count: 2;
        }

        #two {
            animation-name: example;
            animation-duration: 2s;

            /* Animation will be repeated infinitely */
            animation-iteration-count: infinite;
        }

        @keyframes example {
            from {
                background-color: orange;
            }

            to {
                background-color: white;
            }
        }
    </style>
</head>

<body>
    <div class="geeks">
        GeeksforGeeks
    </div>

    <div class="geeks1">
        A computer science portal for geeks
    </div>

    <!-- Animation of the text inside the h2 tag
            below will be repeated twice only -->
    <h2 id="one">
        This text changes its color two times.
    </h2>

    <!-- Animation of the text inside the h2 tag
            below will be repeated infinitely -->
    <h2 id="two">
        This text changes its color infinite times.
    </h2>
</body>
  
</html>

Output:

Best Practices for Using Animation-Iteration-Count:

  • Avoid Overuse: Excessive repetition can be distracting.
  • Performance Considerations: Test animations on various devices to ensure smooth performance.
  • Enhance User Experience: Use animations to make your interface more engaging without overwhelming users.

Supported Browser: The browser supported by animation-iteration-count property are listed below:

  • Google Chrome 43.0 and above
  • Edge 12.0 and above
  • Firefox 16.0 and above
  • Opera 30.0 and above
  • Safari 9.0 and above

CSS animation-iteration-count Property – FAQs

What is the animation-iteration-count property in CSS?

The animation-iteration-count property in CSS specifies how many times an animation should repeat. You can set it to a specific number or make the animation repeat infinitely.

What is the default value of animation-iteration-count?

The default value is 1, meaning the animation will play once and then stop.

What happens if I set a fractional number for animation-iteration-count?

You can set fractional values (e.g., 2.5) for animation-iteration-count, which will play the animation partially. In this example, the animation would run twice completely and then halfway through the third cycle.

How do I control the speed of each iteration of an animation?

The speed of each iteration is controlled by the animation-duration property. The animation-iteration-count determines how many times that duration repeats.

Does animation-iteration-count affect the timing function or delay?

The animation-iteration-count property does not directly affect the timing function or delay; it only controls the number of repetitions. The timing of each iteration is governed by animation-timing-function and animation-delay.



Next Article

Similar Reads

three90RightbarBannerImg