CSS: animation

The animation property is a shorthand for running a CSS keyframe animation.

What you will learn

Basic example

.notice {
  animation: fade-in 300ms ease-out both;
}

@keyframes fade-in {
  from {
    opacity: 0;
    transform: translateY(.5rem);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

The shorthand usually includes the animation name, duration, timing function, and fill mode. The name must match a @keyframes rule.

Useful options

Reduced motion

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms;
    animation-iteration-count: 1;
    scroll-behavior: auto;
  }
}

Keep important information available without motion. Do not make animation the only way to communicate a state.

Common mistakes