How to Use animation-iteration-count: infinite in CSS

This article explains the purpose, functionality, and implementation of the CSS declaration animation-iteration-count: infinite. You will learn how this property controls animation cycles, why it is essential for creating continuous loops, how to apply it in standard and shorthand CSS syntax, and best practices for web accessibility and performance.

What Does animation-iteration-count: infinite Do?

In CSS, the animation-iteration-count property defines how many times an animation cycle should play before stopping. By default, animations have an iteration count of 1, meaning they play from start to finish once and then halt.

Setting the value to infinite instructs the browser to replay the keyframe sequence continuously without end. As long as the animated element remains in the DOM and retains the applied CSS rule, the animation will restart from the beginning each time it reaches the final keyframe.

Syntax and Implementation

You can define an infinite animation either by using the standalone animation-iteration-count property or within the animation shorthand property.

Standalone Property

.spinner {
  animation-name: spin;
  animation-duration: 1.5s;
  animation-timing-function: linear;
  animation-iteration-count: infinite;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

Shorthand Property

In the shorthand animation property, the value infinite can be placed anywhere, though it conventionally appears toward the end of the declaration:

.spinner {
  animation: spin 1.5s linear infinite;
}

Common Use Cases

The infinite keyword is used for UI elements that require constant motion to communicate ongoing activity or status, including:

  • Loading indicators and spinners: Visual cues that indicate a process is active in the background.
  • Pulsing notifications: Badges or alerts designed to draw user attention until dismissed.
  • Ambient background animations: Subtle, repeating patterns like floating clouds, gradient shifts, or wave effects.
  • Continuous tickers or marquees: Elements that continuously scroll text or media across a screen.

Best Practices and Accessibility

Continuous motion can be distracting and can cause vestibular disorders or motion sickness for some users. When using infinite animations, follow these best practices:

  • Respect Reduced Motion: Provide an alternative for users who have requested minimal motion at the operating system level using the prefers-reduced-motion media query.
@media (prefers-reduced-motion: reduce) {
  .spinner {
    animation-iteration-count: 1;
    /* or stop the animation completely */
    animation: none;
  }
}
  • Optimize Performance: Infinite animations consume processing power. Animate properties handled by the GPU—such as transform and opacity—rather than layout-triggering properties like width, height, or top to prevent unnecessary repaints and battery drain.