What Is the CSS @keyframes Rule and Syntax?

The CSS @keyframes at-rule controls intermediate steps in a CSS animation sequence by defining styles at specific milestones along a timeline. By establishing keyframes, developers gain precise control over CSS property shifts without relying on JavaScript, enabling fluid transitions, looped micro-interactions, and complex multi-stage UI motion.

Understanding the Syntax of @keyframes

The @keyframes rule requires a custom identifier (an animation name) followed by a block containing style declarations mapped to specific progress markers.

@keyframes slideAndFade {
  0% {
    opacity: 0;
    transform: translateX(-50px);
  }
  50% {
    opacity: 0.5;
    transform: translateX(10px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}

Keyframe Selectors

Keyframe progress is defined using percentages or keywords:

Binding Keyframes to Elements

Declaring an @keyframes block defines the animation logic, but it does not run until it is assigned to an element using CSS animation properties.

.notification-card {
  animation-name: slideAndFade;
  animation-duration: 0.8s;
  animation-timing-function: ease-out;
  animation-fill-mode: forwards;
}

Alternatively, the animation shorthand combines these properties into a single rule:

.notification-card {
  animation: slideAndFade 0.8s ease-out forwards;
}

Core Animation Properties

Practical Utility in Modern Web Development

The @keyframes rule provides distinct advantages over traditional transition-based or script-driven animation methods: