Mastering CSS Animations

Bring your website to life by learning to control motion with `@keyframes`, timing functions, and the powerful `animation` shorthand property.

Lesson ProgressStep 1 of 9
0 EXP

Welcome! Let's bring this box to life. The foundation of all CSS animation is the `@keyframes` rule.

/* Awaiting instructions... */

The Foundation: What are `@keyframes`?

The `@keyframes` at-rule is the heart of CSS animations. It's where you define the "script" for your animation, specifying what the element should look like at different points in time.

You create a named animation (e.g., `pulse`) and define "stops" along its timeline using percentages, from `0%` (the start) to `100%` (the end). You can also use the keywords `from` (for `0%`) and `to` (for `100%`).

@keyframes pulse {
  from {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.1);
    opacity: 0.7;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

System Check

What are the two-word aliases for 0% and 100% in a @keyframes rule?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🎨
Keyframe Creator

Successfully define a @keyframes rule and apply it.

✍️
Shorthand Pro

Correctly use the 'animation' shorthand property.

⚙️
Syntax Expert

Prove your mastery of animation syntax and properties.

Mission: Create a Pulse

Create a `@keyframes` rule named `pulse` that scales from `1` (at 0%) to `1.1` (at 50%) and back to `1` (at 100%). Then, apply it to the `.box` class to run for 2 seconds, infinitely.

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Shorthand

The `animation` shorthand has a common order. Drag the values below into the correct sequence after the animation-name.

animation: slide-in ... ;
ease-in-out
3s
alternate
infinite

Challenge: Complete the Syntax

Fill in the missing parts of this animation rule.

@keyframes pulse {
{ transform: scale(1); }
{ transform: scale(1.1); }
}
.box {
animation: pulse 2s;
}

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Pulse" animation project for feedback from other Net-Runners.

Beyond Movement: The Art and Science of CSS Animations

CSS animations are more than just eye candy. When used correctly, they are a powerful tool for communication, feedback, and user engagement. They can guide a user's eye, explain a component's state, and create a delightful, polished experience. But mastering them requires understanding not just the *how* (the syntax) but the *why* (the performance and user experience).

The Performance Golden Rule: Animate `transform` and `opacity`

Not all CSS properties are created equal. When you animate properties like `width`, `height`, `left`, or `margin`, the browser must re-calculate the layout of the page ("Layout" or "Reflow") and then repaint the pixels ("Paint"). This is computationally expensive and can lead to jerky, laggy animations, especially on mobile devices.

However, properties like **`transform`** (e.g., `translateX`, `scale`) and **`opacity`** are special. Modern browsers can hand off the animation of these properties entirely to the GPU (Graphics Processing Unit). The browser creates a separate layer for the element (like a layer in Photoshop) and simply moves it around or fades it. This is called **hardware acceleration**.

✔️ Good (Performant)

@keyframes slide-in {
  from { transform: translateX(-100px); }
  to   { transform: translateX(0); }
}

Animates `transform`. Smooth and efficient.

❌ Bad (Janky)

@keyframes slide-in {
  from { left: -100px; }
  to   { left: 0; }
}

Animates `left`. Causes layout changes on every frame.

The Art of `cubic-bezier()` and `steps()`

The `animation-timing-function` is the "soul" of your animation. While keywords like `ease-in` and `ease-out` are great, you can achieve incredibly natural and custom effects with `cubic-bezier()` and `steps()`.

  • `cubic-bezier(x1, y1, x2, y2)`: This function defines a curve. `(x1, y1)` and `(x2, y2)` are the coordinates of two control points. By pulling these points, you can create "bouncy" effects (e.g., `cubic-bezier(0.68, -0.55, 0.27, 1.55)`) or sharp "ease-out" curves.
  • `steps(n, [start|end])`: This function is completely different. It creates a "stepped" animation, not a smooth one. It divides the animation into `n` equal steps. This is the secret behind **sprite sheet animations**, where you move a background image to create a flipbook-like effect.

Accessibility: `prefers-reduced-motion`

For some users, especially those with vestibular disorders, large-scale motion can cause dizziness, nausea, or migraines. As responsible developers, we must respect their preferences.

CSS provides a media query, `@media (prefers-reduced-motion: reduce)`, which checks if the user has enabled this setting in their operating system. Inside this block, you should disable or significantly tone down your animations.

/* Standard animation */
.box {
  animation: slide-in 1s;
}

/* Remove animation for users who prefer reduced motion */
@media (prefers-reduced-motion: reduce) {
  .box {
    animation: none;
  }
}
Key Takeaway: Animate responsibly. Prioritize performance by using `transform` and `opacity`. Use timing functions to give your animations personality. And always respect your users by implementing `prefers-reduced-motion`.

CSS Animations Glossary

`@keyframes`
The at-rule used to define the sequence of styles for an animation. It is composed of "stops" (e.g., `0%`, `50%`, `100%` or `from`, `to`) that specify the CSS styles at that point.
`animation`
The shorthand property that combines all other animation properties into a single declaration.
`animation-name`
Specifies the name of the `@keyframes` rule to apply to the element.
`animation-duration`
Defines the length of time an animation takes to complete one cycle (e.g., `2s`, `500ms`).
`animation-timing-function`
Controls the speed curve of the animation. Common values are `linear`, `ease-in`, `ease-out`, `ease-in-out`, or a custom `cubic-bezier()` function.
`animation-delay`
Sets a pause before the animation begins (e.g., `1s` wait).
`animation-iteration-count`
Specifies the number of times the animation should play. Can be a number (e.g., `3`) or `infinite`.
`animation-direction`
Defines whether the animation should play forwards, backwards, or alternate on each cycle. Values include `normal`, `reverse`, `alternate`, and `alternate-reverse`.
`animation-fill-mode`
Controls the styles applied to the element before the animation starts (from `animation-delay`) and after it ends. `forwards` is a common value to make the element retain its end-of-animation state.
`animation-play-state`
Allows you to pause (`paused`) or resume (`running`) an animation, often controlled via JavaScript or a `:hover` state.
Hardware Acceleration
The browser offloads the animation work to the computer's GPU instead of the CPU. This results in much smoother animations. It is typically triggered when animating `transform` and `opacity`.
`prefers-reduced-motion`
A CSS media query used to detect if the user has requested the system minimize the amount of non-essential motion it uses. It's an essential tool for accessibility.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building robust and accessible web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS Animations specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!