# Keyframe Animations

> Build complex, multi-step motion sequences using @keyframes.

Subject: CSS | Lesson 31 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/keyframe-animations
Previous: [Transitions](https://codewithanimation.in/css/transitions.md) | Next: [Transforms (2D & 3D)](https://codewithanimation.in/css/transforms.md)

## What are Keyframe Animations?

While CSS Transitions let you smoothly change between two states (like hovering over a button), they are limited to a single step from point A to point B.

Keyframe animations give you total control. Using the `@keyframes` rule, you can define a complex timeline of style changes at specific percentages (0%, 50%, 100%). You then attach this timeline to an element using the `animation` properties.

## Why are they important?

Keyframes are essential for creating continuous loops (like a loading spinner), complex chained sequences (like an element bouncing, then fading out), or animations that trigger as soon as the page loads without waiting for a user interaction.

## How do you use them?

First, define the timeline using `@keyframes` and give it a custom name (like `slide-and-fade`). Inside, use percentages to define the styles at different points in time.

Then, apply it to an element using `animation-name` and `animation-duration`. You can also control the loop count with `animation-iteration-count`, or the play direction with `animation-direction`.

## Common mistake: The silent failure

Defining a `@keyframes` block does nothing on its own. You must explicitly attach it to an element using both the `animation-name` and `animation-duration` properties, otherwise the animation will not play.

## Tip: Keep it in the final state

By default, when an animation finishes, the element snaps back to its original un-animated state. If you want it to permanently stay at the 100% keyframe, add `animation-fill-mode: forwards`.

## Try it: Try it

Tweak the animation properties and watch how the keyframe timeline drives the motion of the target block.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/keyframe-animations

## Check yourself

### Question 1

Which property is absolutely required for a keyframe animation to play?

- A. animation-iteration-count
- B. animation-duration
- C. animation-timing-function

Correct answer: B

Without an `animation-duration`, the animation completes instantly (in 0 seconds) and you won't see anything happen.

- Why not A: It defaults to 1 if you don't set it.
- Why not C: It defaults to 'ease' if omitted.

### Question 2

What does animation-direction: alternate do?

- A. It plays the animation backwards forever.
- B. It swaps the 0% and 100% styles randomly.
- C. It plays forwards, then backwards, then forwards again.

Correct answer: C

`alternate` is perfect for looping animations (like a swinging pendulum) because it seamlessly reverses the animation on every even cycle.

- Why not A: That would be the 'reverse' value.
- Why not B: CSS animations are predictable, not random.

### Question 3

How do you stop an element from snapping back to its original state when the animation ends?

- A. animation-fill-mode: forwards
- B. animation-play-state: paused
- C. animation-iteration-count: infinite

Correct answer: A

`animation-fill-mode: forwards` tells the browser to retain the styles calculated by the final keyframe (usually 100%) after the animation finishes.

- Why not B: This pauses the animation, but doesn't prevent snapping when it naturally finishes.
- Why not C: This loops it forever, which means it technically never 'ends', but it still snaps back to 0% on every loop.

## Remember this

- Define the timeline with @keyframes and a custom name.
- Attach it to an element using animation-name and animation-duration.
- Use infinite to loop it forever.
- Use animation-fill-mode: forwards to prevent snapping back at the end.
