CSS · Layout and visual systems

How do CSS animations and keyframes work?

MediumanimationskeyframesperformanceUpdated Oct 2026
Quiz this track

Short answer

CSS animations run keyframes over time without needing a state change. You define stages with @keyframes and control duration, iteration, direction, delay and fill mode on the animated element.

An animation is applied with the animation property and runs automatically when the element matches the rule. Unlike a transition, it does not need a property value to change. Keyframes can define from and to, percentages, or both.

css
@keyframes fade-up {
  from {
    opacity: 0;
    transform: translateY(0.5rem);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.card {
  animation: fade-up 300ms ease-out both;
}

The controls

  • animation-name and animation-duration set the keyframes and timing
  • animation-iteration-count controls how many times it runs, including infinite
  • animation-direction supports normal, reverse, alternate and alternate-reverse
  • animation-fill-mode keeps the first or last keyframe styles before or after the run
  • animation-play-state can pause and resume
  • Transitions need a value change; animations run from their keyframes

Animations are best for entrance effects, loading indicators and small delight. They should not block interaction or run forever when the user has asked for reduced motion. Pair them with a prefers-reduced-motion query that removes non-essential movement.

The both fill mode applies the from styles before the animation starts and the to styles after it ends. That prevents a flash of the unanimated state, but it also means the final styles persist until the animation is removed.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1What is the difference between a transition and an animation?
  2. 2What does animation-fill-mode: both do?
  3. 3How do you pause an animation?
  4. 4Why should infinite animations respect reduced motion?

Say it in the interview

CSS animations run keyframes over time and do not need a value change to start. I define the stages with @keyframes and control duration, iteration, direction, delay and fill mode. Transitions are simpler when a property changes, while animations are better for multi-step or looping effects. I still restrict movement for users who prefer reduced motion.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live CSS mock interview and get scored feedback on depth, clarity and confidence.