Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

animation: one declaration for a complete animation setup

Combine a keyframe name and timing settings without overlooking omitted values and resets.

Version: CSS Modules

Syntax

animation: travel 1s linear 500ms 2 alternate both paused;

The animation shorthand describes one or more animations, separated by commas. Each item supplies a name and settings such as duration, easing, delay, repeat count, direction, fill and play state. Define the referenced @keyframes separately. The first time is the duration and the second the delay; a negative duration is invalid, but a negative delay is allowed. An ambiguous keyword is assigned to another applicable setting before being treated as an animation name, so distinctive names reduce surprises.

Omitted subproperties return to their initial values rather than preserving previous declarations. In the ordinary time-based case, omitting duration means zero seconds, so there is no gradual visible run. Later longhand declarations can override the shorthand. Newer timeline and range controls need their own support checks; place their explicit settings after the shorthand to avoid resets. Animation is not inherited, and reduced-motion preferences still need an appropriate alternative.

The example defines a one-second movement with linear easing, a 500ms delay, two alternating iterations, both-side fill and a paused state. Explicit local times 0,1000,2000,2500ms produce opacity/translation pairs 0.20/0, 0.50/50, 0.50/50 and 0.20/0. The delay and reverse second iteration explain the repeated midpoint and final return. Replacing the declaration with only the name resets a previously assigned five-second duration to 0s, as well as the delay, direction, fill and running state.

Examples

const style=document.createElement('style');
style.textContent='@keyframes travel{from{opacity:.2;transform:translateX(0px)}to{opacity:.8;transform:translateX(100px)}}';document.head.append(style);
const box=document.createElement('div');box.style.animation='travel 1s linear 500ms 2 alternate both paused';document.body.append(box);
const animation=box.getAnimations()[0];
const samples=[0,1000,2000,2500].map(time=>{animation.currentTime=time;const s=getComputedStyle(box);return Number(s.opacity).toFixed(2)+','+new DOMMatrix(s.transform).m41});
const s=getComputedStyle(box);console.log('settings='+[s.animationName,s.animationDuration,s.animationDelay,s.animationIterationCount,s.animationDirection,s.animationPlayState].join('|'));
console.log('samples='+samples.join(' / '));
box.style.animationDuration='5s';box.style.animation='travel';
const reset=getComputedStyle(box);console.log('reset='+[reset.animationDuration,reset.animationDelay,reset.animationDirection,reset.animationFillMode,reset.animationPlayState].join('|'));

Result

settings=travel|1s|0.5s|2|alternate|paused
samples=0.20,0 / 0.50,50 / 0.50,50 / 0.20,0
reset=0s|0s|normal|none|running

The browser evaluates real CSS keyframes. Setting currentTime on the CSSAnimation makes sampling deterministic; no waiting, frame-rate measurement or scroll-timeline behavior is claimed.

Sources