Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

animation-name: connect an element to keyframes

Select named @keyframes rules and coordinate several animations on the same element.

Version: CSS Modules

Syntax

animation-name: Fade, lift;

animation-name selects the @keyframes rule for each animation. Names are case-sensitive: Fade and fade are different. A missing matching rule creates no animation, even if other timing properties are present. The initial keyword none also means no animation; an existing name still needs appropriate timing, since a zero-duration cycle does not show an ordinary gradual change. A keyframe name can use an allowed custom identifier or a quoted string; the unquoted none remains the disabling keyword.

Use commas for multiple animations. The name list determines their number, and shorter lists of duration or other animation settings repeat to match it; the animations run together rather than queueing automatically. Under the default replace composition, when two animation effects target the same property, the later one in the name list takes precedence while it applies. Naming an animation does not make it inherited by child elements. Removing its name removes its effect, including any fill.

The example defines Fade, lift and dim. The misspelled fade produces zero animation objects; Fade at 500ms gives opacity 0.5. Fade, lift creates two animations that share the single 1s duration setting, and lift moves the element by 50px. Fade, dim also creates two objects, but dim supplies opacity 0.3 at the sampled time. Changing animation-name to none leaves zero animations and restores the underlying opacity 0.3.

Examples

const style=document.createElement('style');style.textContent='@keyframes Fade{from{opacity:0}to{opacity:1}} @keyframes lift{from{transform:translateX(0)}to{transform:translateX(100px)}} @keyframes dim{from{opacity:0.2}to{opacity:0.4}}';document.head.append(style);
const el=document.createElement('div');el.style.cssText='opacity:0.3;animation-duration:1s;animation-timing-function:linear;animation-fill-mode:both;animation-play-state:paused';document.body.append(el);
const apply=names=>{el.style.animationName=names;const animations=el.getAnimations();for(const animation of animations)animation.currentTime=500;return animations};
const missing=apply('fade').length,matched=apply('Fade');const opacity=getComputedStyle(el).opacity;
const pair=apply('Fade, lift');const movement=new DOMMatrixReadOnly(getComputedStyle(el).transform).m41;
const conflict=apply('Fade, dim');const winner=getComputedStyle(el).opacity;
const none=apply('none').length;
console.log('missing='+missing+'; matched='+matched.length+'; opacity='+opacity);
console.log('names='+pair.map(animation=>animation.animationName).join(',')+'; duration='+pair.map(animation=>animation.effect.getComputedTiming().duration).join(',')+'; movement='+movement);
console.log('conflict='+winner+'; count='+conflict.length+'; none='+none+'; restored='+getComputedStyle(el).opacity);

Result

missing=0; matched=1; opacity=0.5
names=Fade,lift; duration=1000,1000; movement=50
conflict=0.3; count=2; none=0; restored=0.3

Executed in Chromium 157.0.8103.51. Actual CSSAnimation objects and computed styles confirm name matching, repeated list settings and default replacement. The example does not test additive composition, quoted names or shadow-tree name scoping.

Sources