Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

animation-iteration-count: full and partial repetitions

Control the total number of cycles, including fractional counts and indefinite repetition.

Version: CSS Modules

Syntax

animation-iteration-count: 2.5;

animation-iteration-count accepts a nonnegative number or infinite; its initial value is 1. The number is the total number of iterations, not the number of extra repeats after a first cycle. A fractional value such as 2.5 means two full cycles and half of the next one. Negative numbers are invalid. Comma-separated counts pair with animation-name entries, with a shorter count list repeated as needed.

For an ordinary time-driven animation with finite values, active duration is animation-duration multiplied by the count; animation-delay is not repeated between cycles. Direction determines which way each cycle runs, so forwards fill need not retain the 100% keyframe. A count of 0 is valid and creates an instantaneous active interval; 0s duration also remains instantaneous with infinite. A genuinely repeating animation needs a positive duration, and infinite does not create a natural final point at which forwards fill begins.

The example samples 1s opacity cycles at 2750ms with forwards fill. A normal 2.5 count has finished at opacity 0.5 and active duration 2500ms. Two alternate cycles finish at opacity 0 and 2000ms. A zero count fills with the starting value 0, while infinite is still in its third cycle at opacity 0.75. The reported current-iteration indexes are zero-based, giving 2, 1, 0 and 2; they are not the CSS count values themselves.

Examples

const style=document.createElement('style');style.textContent='@keyframes count-demo{from{opacity:0}to{opacity:1}}';document.head.append(style);
const make=(count,direction='normal')=>{const el=document.createElement('div');el.style.cssText='opacity:0.3;animation-name:count-demo;animation-duration:1s;animation-timing-function:linear;animation-fill-mode:forwards;animation-play-state:paused';el.style.animationIterationCount=count;el.style.animationDirection=direction;document.body.append(el);return {el,animation:el.getAnimations()[0]}};
const rows=[make('2.5'),make('2','alternate'),make('0'),make('infinite')];
for(const row of rows)row.animation.currentTime=2750;
console.log('opacity='+rows.map(({el})=>getComputedStyle(el).opacity).join(','));
console.log('active='+rows.map(({animation})=>animation.effect.getComputedTiming().activeDuration).join(','));
console.log('iterations='+rows.map(({animation})=>animation.effect.getComputedTiming().currentIteration).join(','));
console.log('negative='+CSS.supports('animation-iteration-count','-1')+'; fractional='+CSS.supports('animation-iteration-count','2.5'));

Result

opacity=0.5,0,0,0.75
active=2500,2000,0,Infinity
iterations=2,1,0,2
negative=false; fractional=true

Executed in Chromium 157.0.8103.51 using actual CSS animations and computed timing. The example verifies finite/fractional/zero/infinite behavior at one sampled time, not endless execution or event counts. Provide a pause or reduced-motion alternative when repeating motion could distract users.

Sources