CSS Reference ยท At-rules
The @keyframes at-rule
Reference for the CSS @keyframes at-rule: name an animation, define from/to and percentage keyframes, and drive it with the animation properties.
The @keyframes at-rule defines the steps of an animation. It gives the animation a name and lists keyframes: the style an element should have at certain points of the timeline. The browser interpolates the values in between.
A @keyframes block does nothing alone. An element must reference its name through the animation shorthand (or animation-name) and set a duration.
CSS
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.2); } } .dot { animation: pulse 1s ease-in-out infinite alternate; }
Try it
Syntax
CSS
@keyframes <name> { from | to | <percentage> [, <percentage>]* { /* declarations */ } } /* using it */ animation: <name> <duration> <timing> <delay> <count> <direction> <fill-mode> <play-state>;
- Name: a custom identifier, or a quoted string.
noneand the CSS-wide keywords are not allowed. - Selectors:
fromequals0%,toequals100%; several offsets may share a block (0%, 100% { }). - Missing endpoints: if there is no
0%or100%, the browser uses the element's own style there. - Timing within a block:
animation-timing-functioncan be set inside a keyframe and then applies from that keyframe to the next one.
Values
| Syntax and arguments | Description |
|---|---|
from { ... } |
The starting state (0%). |
to { ... } |
The ending state (100%). |
35% { ... } |
A state at a point in the animation. |
0%, 100% { ... } |
The same state at several points. |
animation-fill-mode: forwards |
Keeps the last keyframe after the animation ends. |
animation-direction: alternate |
Plays forward then backward on each repeat. |
Usage notes
- Animate
transformandopacityfor smooth results; these usually run on the compositor without re-layout. Animatingwidth,toporbox-shadowis more expensive. - Respect motion preferences. Disable large or looping motion in
@media (prefers-reduced-motion: reduce). - Invalid declarations are ignored inside keyframes, and
!importantdeclarations there are dropped. Properties that cannot be interpolated change abruptly at the halfway point (or persteps()). - Later definitions win. If two
@keyframesshare a name, the last one in the cascade is used. - Custom properties in keyframes only animate smoothly if registered with
@property. - Scroll-driven animations use the same
@keyframesbut attach them to a scroll timeline withanimation-timeline. - Name collisions. Prefix names in large projects (
card-fade-in).
Browser support
Supported in every browser; the unprefixed form has been available for many years.
Related
@property@starting-style@media- Lesson: Animations
- Lesson: Scroll-driven Animations
- Lesson: Motion and Performance