Skip to main content

What is animation in CSS? How do you set it?

Animation in CSS is a mechanism that lets you smoothly change CSS property values over time (color, size, position, opacity, etc.) without JavaScript.

A CSS animation is defined using two parts:


1) Describing keyframes with @keyframes

css
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

from → the starting state to → the ending state (percentages also work: 0%, 50%, 100%)


2) Applying the animation to an element

css
.box { animation: fade 2s ease-in-out forwards; }

Where:

PropertyWhat it sets
animation-namethe animation's name (fade)
animation-durationduration (2s, 500ms)
animation-timing-functionthe easing curve (e.g. ease, linear, ease-in-out)
animation-iteration-countnumber of repeats (1, infinite)
animation-delaydelay
animation-directiondirection (normal, reverse, alternate)
animation-fill-modebehavior before/after the animation (forwards, backwards, both)

Summary

CSS animation is created like this:

  1. Describe the keyframes with @keyframes
  2. Assign the animation to an element with animation

No JS, just CSS.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.