Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

@keyframes — CSS animation keyframes

The @keyframes rule gives a name to property values at selected offsets along a CSS animation’s progress.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

@keyframes <keyframes-name> {
  <qualified-rule-list>
}

@keyframes <name> { <keyframe-block-list> } contains blocks selected by from, to, or percentages. from equals 0% and to equals 100%. Declarations in each block describe values at that offset; the browser interpolates them according to each property’s animation type and timing function. animation-name connects the definition to an element.

Omitting 0% or 100% does not cancel the animation: the element’s underlying value supplies the missing endpoint. Duplicate offsets cascade their declarations within the rule. If multiple @keyframes rules share a name, their blocks are not merged; the last applicable definition is used. An !important declaration in a keyframe is ignored.

Not every value pair interpolates numerically; discrete properties switch state, and unsupported declarations cannot produce the expected effect. A timing function declared on a keyframe controls the segment beginning there. The example pauses an animation halfway using a negative delay and confirms its name, play state, and computed opacity 0.6 through the Web Animations API.

Examples

const style = document.createElement('style');
style.textContent = `@keyframes fade { from { opacity: 0.2; } 50% { opacity: 0.6; } to { opacity: 1; } }
.probe { animation: fade 1000ms linear -500ms both paused; }`;
document.head.append(style);
const probe = document.createElement('div');
probe.className = 'probe';
probe.textContent = 'CKTIS';
document.body.append(probe);
await new Promise(requestAnimationFrame);
const animation = probe.getAnimations()[0];
console.log('name=' + animation.animationName + '; play=' + animation.playState + '; opacity=' + getComputedStyle(probe).opacity);

Result

name=fade; play=paused; opacity=0.6

The @keyframes rule gives a name to property values at selected offsets along a CSS animation’s progress.

Sources