CSS animation / @keyframes
- type
- property + at-rule
- applies to
- any element
- inherits
- no
- common values
- name duration timing-function delay iteration-count; @keyframes name { from {} to {} }
Where it goes
@keyframes at the top level of the stylesheet. The animation property on the box. Stick to transform and opacity inside the steps for smoothness. Respect @media (prefers-reduced-motion: reduce) by shortening or removing animations for people who asked for less motion.
Example
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner { animation: spin 1s linear infinite; }
@media (prefers-reduced-motion: reduce) {
.spinner { animation-duration: 3s; }
}Common mistakes
- Spelling the @keyframes name differently from the animation name. A typo means nothing plays, with no error.
- Animating width, height, top or left. These force a layout recalculation every frame and stutter. Animate transform and opacity.
- Ignoring prefers-reduced-motion. Looping or large motion makes some people ill. One media query at the bottom of the file turns it off for them.