Keyframe Animations: @keyframes and the animation Properties
Animations that run by themselves: @keyframes with percentages, implicit start and end keyframes, what keyframes ignore, per-keyframe easing, the animation properties and shorthand, iterations, delays and staggering, pausing, and the cascade.
kvi5hnuCode Sprout team member9 min readOn this page
Transitions animate between two states when a value changes. Keyframe animations run on their own as soon as they're applied, can pass through any number of intermediate states, and can repeat. They're used for loading spinners, attention effects, entrances and decorative motion.
An animation has two parts: a named @keyframes rule that describes the states, and the animation properties on an element that run it.
@keyframes spin {
to { rotate: 1turn; }
}
.spinner { animation: spin 1s linear infinite; }
Motion that flashes or moves a lot can trigger seizures or nausea; article 112 covers prefers-reduced-motion and accessible motion.
@keyframes

Each block in a @keyframes rule has a keyframe selector, a percentage of the animation's duration, and the declarations that apply at that point. from means 0% and to means 100%:
@keyframes pop {
30% { scale: 1.25; }
60% { scale: 0.9; }
}
@keyframes blink {
0%, 49.9% { opacity: 1; }
50%, 100% { opacity: 0; }
}
- Several selectors can share a block, separated by commas, and fractions like
33.33%are fine. Negative values or values over 100% are invalid and are ignored. - The order of blocks doesn't matter, but writing them in time order keeps them readable.
- Missing
0%or100%keyframes are built by the browser from the property's value without this animation.popabove starts and ends at the element's normal scale. Tested in Chrome: on a 100px-wide element,@keyframes grow { to { width: 200px } }was 150px halfway through. This lets one animation start from wherever each element is. - Blocks with the same selector are merged. Tested in Chrome, two
50%blocks, one settingwidthand one settingheight, both applied. - If two
@keyframesrules have the same name, the last one wins entirely; they aren't merged. Tested in Chrome: with twoduprules, the element animated to the second one's value. - The name is an identifier such as
fade-in, or a quoted string. Avoid names that are also animation keywords, likeease,infiniteornone, since the shorthand would misread them.
What keyframes ignore
- Properties that can't be animated are ignored, such as
animation-nameortransitionproperties inside keyframes. Discrete properties, likedisplayorfont-family, switch halfway between keyframes, so for a clean switch, put the same value on both sides of the change, asblinkdoes. !importantdeclarations in a keyframe are ignored entirely. Tested in Chrome:to { width: 200px !important; height: 50px }animated the height but not the width.animation-timing-functioncan be set inside a keyframe (so cananimation-composition, for that keyframe's composite operation). It sets the easing from that keyframe to the next, overriding the element's timing function for that segment:
@keyframes drop {
0% { translate: 0 -100px; animation-timing-function: ease-in; } /* falling speeds up */
60% { translate: 0 0; animation-timing-function: ease-out; } /* bounce slows down */
80% { translate: 0 -20px; animation-timing-function: ease-in; }
to { translate: 0 0; }
}
Tested in Chrome: with animation-timing-function: steps(1) on a 50% keyframe, the value held at its 50% value until the 100% keyframe.
The element's animation-timing-function applies to each segment between keyframes, not to the whole animation. ease on a five-keyframe animation slows down and speeds up four times.
The animation properties
| Property | Default | Values |
|---|---|---|
animation-name | none | @keyframes names, comma-separated |
animation-duration | auto, which is 0s | a time; one iteration's length |
animation-timing-function | ease | any timing function (article 107) |
animation-delay | 0s | a time; negative starts partway through |
animation-iteration-count | 1 | a number, including fractions, or infinite |
animation-direction | normal | reverse, alternate, alternate-reverse (article 110) |
animation-fill-mode | none | forwards, backwards, both (article 110) |
animation-play-state | running | paused |
animation-composition | replace | add, accumulate (article 110) |
animation-timeline | auto | scroll and view timelines (article 113) |
An animation needs a name and a duration to be visible.
- Durations need units. An invalid value in a list makes the whole declaration invalid. Tested in Chrome,
animation-duration: 1s, 0, 0.5swas dropped because of the unitless0. - Iteration counts can be fractions:
2.5plays two and a half times, ending in the middle. Negative counts are invalid: tested in Chrome,-1was dropped, leaving1. - Negative delays start the animation partway through, as if it had already been running. That's the standard way to desynchronize repeated elements:
.dot { animation: bounce 1s ease-in-out infinite; }
.dot:nth-child(2) { animation-delay: -0.66s; }
.dot:nth-child(3) { animation-delay: -0.33s; }
With sibling-index() (article 30), one rule can stagger any number of items: animation-delay: calc(sibling-index() * 0.1s).
animation-play-state: pausedfreezes an animation where it is, andrunningcontinues it. For example,.card:hover .marquee { animation-play-state: paused; }.
The shorthand
animation takes, per animation: name, duration, timing function, delay, iteration count, direction, fill mode and play state, in any order. It also resets animation-timeline, animation-range and animation-composition to their initial values, but can't set them (article 113). As in transition, the first time is the duration and the second the delay:
.banner { animation: slide-in 0.6s ease-out 0.2s both; }
.loader { animation: spin 1s linear infinite, fade 2s ease-in-out infinite alternate; }
Tested in Chrome, animation: 3s ease-in 1s infinite reverse both running slide parsed with the name slide, a duration of 3s and a delay of 1s. Like all shorthands, it resets the properties you leave out.
The longhand lists are matched by position, repeating shorter lists as for transitions (article 106). animation-name sets the number of animations. If a name has no matching @keyframes, that animation does nothing, and it starts if a matching rule is added later.

The fourth row previews this section: both animations target width, and only the later one in the list shows. In the last row, the !important width is dropped while the rest of the keyframe still runs.
Multiple animations and the cascade
- When two animations on one element set the same property, the later one in the
animation-namelist wins. Tested in Chrome: withanimation: grow 1s, widen 1s, both changingwidth, the later one controlled it. Its implicit0%keyframe started from the value the earlier animation had produced. - Animations rank above all normal declarations in the cascade (article 17), whatever their specificity, so a running animation overrides even inline styles. Tested in Chrome: an element with an inline
width: 50pxanimating to 200px was 125px halfway through. !importantdeclarations beat animations. Tested in Chrome,width: 100px !importanton the element stopped a width animation from showing. So aprefers-reduced-motionoverride can't simply set the animated property back; it has to change the animation itself, for exampleanimation: noneor a near-zeroanimation-duration. Resets often add!importantto thoseanimation-*declarations, but that's ordinary specificity: it lets a broad*rule beat more specific selectors that set the animation.- An animation restarts when it's removed and reapplied: changing
animation-name, or switching the element todisplay: noneand back. Changing other animation properties updates the running animation without restarting it: tested in Chrome, changinganimation-durationkept the same animation object and its current time.
From JavaScript, element.getAnimations() returns CSSAnimation objects with animationName, currentTime, pause() and play(). You can also create animations without CSS through element.animate(keyframes, options), the Web Animations API.
body { font: 0.85rem system-ui, sans-serif; margin: 1rem; }
.row { display: flex; align-items: center; gap: 1.5rem; }
@keyframes spin { to { rotate: 1turn; } }
.spinner {
inline-size: 2rem; block-size: 2rem; border-radius: 50%;
border: 4px solid #eee6d8; border-top-color: #8a3b12;
animation: spin 0.9s linear infinite;
}
@keyframes bounce { 0%, 100% { translate: 0 0; } 50% { translate: 0 -0.6rem; } }
.dots { display: flex; gap: 0.3rem; }
.dots i { inline-size: 0.6rem; block-size: 0.6rem; border-radius: 50%; background: #1d3557; animation: bounce 0.9s ease-in-out infinite; }
.dots i:nth-child(2) { animation-delay: -0.6s; } /* negative delays desynchronize */
.dots i:nth-child(3) { animation-delay: -0.3s; }
@keyframes pop { 30% { scale: 1.25; } 60% { scale: 0.9; } } /* 0% and 100% are implicit */
.pop-btn { font: inherit; padding: 0.3rem 0.8rem; }
.pop-btn.go { animation: pop 0.5s ease-out; }
@keyframes typing { from { inline-size: 0; } }
@keyframes caret { 50% { border-color: transparent; } }
.type {
font: 700 1rem ui-monospace, monospace; inline-size: 17ch; overflow: hidden; white-space: nowrap;
border-inline-end: 2px solid #8a3b12;
animation: typing 2.5s steps(17) infinite alternate, caret 0.6s step-end infinite;
}
@keyframes drop {
0% { translate: 0 0; animation-timing-function: ease-in; }
50% { translate: 0 3rem; animation-timing-function: ease-out; }
to { translate: 0 0; }
}
.track { block-size: 4.5rem; }
.ball { inline-size: 1.4rem; block-size: 1.4rem; border-radius: 50%; background: #2f7d32; margin-inline: auto; animation: drop 1.2s infinite; }
.paused * { animation-play-state: paused; }
@media (prefers-reduced-motion: reduce) { * { animation-duration: 0.01s !important; animation-iteration-count: 1 !important; } }
The spinner and dots loop forever; the dots are offset with negative delays. The Pop button runs an animation with implicit start and end keyframes. The text types with steps(17), one step per character, and the ball changes easing at each keyframe. The checkbox pauses everything.
What's current
@keyframesand the animation properties: all browsers, unprefixed.animation-composition: all browsers (article 110).animation-timelineand scroll-driven animations: article 113.animation-duration: autois the new default (Chrome 115, Safari 18.4, Firefox 159):0sfor time-based animations, and filling the timeline for scroll-driven ones.
Common questions
Why doesn't my animation run?
Check that the name matches a @keyframes rule, that there's a duration with a unit, and that the properties are animatable.
How do I restart an animation from JavaScript?
Remove the class, force a style update (for example by reading offsetWidth), then add it again. Or call animation.currentTime = 0 on the CSSAnimation.
Why doesn't my :hover style change a property that's being animated?
Animations override normal declarations. Pause the animation, or use !important.
Can I animate from the current value?
Yes, leave out the from keyframe; the browser uses the element's value.
Can I use custom properties in keyframes?
Yes, in two ways. A var() inside a keyframe is resolved against the animated element, so to { translate: var(--d) 0; } lets each element set its own distance; with --d: 200px, Chrome reports 50px a quarter of the way through. Animating a custom property itself only interpolates if it's registered with @property and a type (article 28). Unregistered, it flips from the start value to the end value halfway through: tested in Chrome, it was 0px at 25% and 100px at 75%, while the registered <length> version was 25px at 25%.
Summary
@keyframesdefines states at percentages; missing0%and100%keyframes are built from the element's own values.- Keyframes ignore
!importantand non-animatable properties, but can setanimation-timing-functionfor the next segment. - The timing function applies to each segment between keyframes.
- The shorthand's first time is the duration, the second the delay; negative delays start partway.
- Animations override normal declarations, the later of two animations on the same property wins, and
!importantbeats both.
Practice questions
Write an animation that makes a notification badge pulse twice, growing to 1.2 times its size and back.
Answer
@keyframes pulse { 50% { scale: 1.2; } }
.badge { animation: pulse 0.4s ease-in-out 2; }
The 0% and 100% keyframes are implicit, using the badge's normal scale.
This animation fades out, but its opacity doesn't change. Why?
@keyframes fade { to { opacity: 0 !important; } }
Answer
!important declarations inside keyframes are ignored. Remove !important.
Three loading dots share animation: bounce 0.9s infinite. How do you make them bounce one after another without a pause before they start?
Answer
Give the second and third dots negative delays, such as -0.6s and -0.3s. Negative delays start them partway through, so all three move at once, out of phase.
What do the two times mean in animation: slide 0.5s 2s?
Answer
A duration of 0.5s, then a delay of 2s before it starts.
Previous: Transitioning the Hard Cases: display, Height auto, @starting-style and allow-discrete
Next: Animation in Depth: Fill Modes, Direction, Composition, Events and Interruptions

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.