Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 19

CSS Transitions vs. Keyframe Animations

Recommended resources

Hello! Welcome to the first lesson of our module on advanced CSS topics.

In the last module, you mastered CSS layout architecture, learning how to strategically combine Grid and Flexbox to build robust, scalable structures. You now have the skills to define the "where" of your UI components. Today, we shift our focus from static structure to dynamic behaviorβ€”the "how" and "when" elements change. We'll explore how to bring your interfaces to life with motion.

This is a fundamental topic in creating modern, engaging user experiences. As a developer, you understand that user feedback is critical. Motion is one of the most powerful tools in our arsenal for providing that feedback.

In this lesson, you will learn to describe the difference between CSS transitions and keyframe animations and their use cases, giving you two distinct tools for adding motion to your web applications.

The Foundation: In-betweening

At its core, all CSS animation is about changing style properties over a set duration. The browser does the heavy lifting through a process called "in-betweening" or "tweening"β€”calculating the intermediate values for each frame between the start and end states to create the illusion of smooth motion.

CSS Animation in 100 Seconds

The video 'CSS Animation in 100 Seconds' provides a quick and effective overview of this core concept, along with the two methods we'll be discussing today: transitions and animations.

Watch the entire video (it's short!). Pay attention to: The explanation of 'in-betweening' (0:00 - 0:30). How a transition is applied to a selector to handle a state change (0:48 - 1:25). How @keyframes are defined independently to create a multi-step animation (1:25 - 1:53).

As the video demonstrates, CSS gives us two distinct mechanisms to control this process: transitions and animations.


CSS Transitions: Simple, State-to-State Motion

A CSS transition is the simpler of the two methods. It is designed to animate an element from a starting state to an ending state. It's not an animation you "run" explicitly; rather, you declare that if a certain property changes, the change should be animated over time instead of happening instantly.

This makes transitions ideal for handling changes triggered by user interaction or a change in the DOM.

Common Triggers for Transitions:

  • Pseudo-classes like :hover, :focus, or :active.
  • Adding or removing a class name via JavaScript (e.g., adding .is-open to a menu).

CSS and JavaScript animation performance

The MDN Web Docs provide the definitive technical distinction between transitions and animations. Let's read their explanation.

Read the section titled 'CSS transitions and animations'. Focus on the description of transitions as a way to animate between the current style and an end CSS state.

The key concept is that transitions are for two states: a beginning and an end. The browser simply needs to know which property to watch, how long the transition should take, and optionally, the timing function (e.g., ease-in-out).

What's the difference between CSS animations and JavaScript animations? ft Bruno Simon | Prismic

Let's watch a short clip where expert Bruno Simon explains this 'A to B' nature of transitions.

Watch from 0:44 to 1:17. Note how he frames transitions as the go-to tool when you know the start state and the end state, like a button's size changing on hover.

Here is a classic example of using transition for a button's hover effect:

.my-button {
  background-color: #3498db;
  color: white;
  padding: 10px 20px;
  border: none;
  /* 1. Declare the transition */
  transition: background-color 0.3s ease;
}

/* 2. Define the end state */
.my-button:hover {
  background-color: #2980b9;
}

In this code, we aren't "playing" an animation. We're just telling the browser: "If the background-color property ever changes for this element, take 0.3 seconds to get to the new value."


CSS Animations: Complex, Multi-Step Sequences

What if you need more than just a simple A-to-B change? What if you want an element to pulse, shake, or move through a complex path? This is where CSS animations and @keyframes come in.

Unlike transitions, which are tied to a property change, animations are self-contained sequences of steps that you define and then explicitly apply to an element.

Visual Comparison of CSS Transitions vs. Animations
This image perfectly illustrates the conceptual difference. A transition is a direct path from state A to state B. An animation is a journey through a defined sequence of states (A β†’ B β†’ C β†’ D).

The power of animations comes from the @keyframes at-rule. It allows you to define styles at specific points (or "frames") during the animation's timeline, using percentages from 0% (or from) to 100% (or to).

What's the difference between CSS animations and JavaScript animations? ft Bruno Simon | Prismic

Let's return to Bruno Simon's explanation, where he now describes the use case for animations.

Watch from 1:17 to 1:48. He describes animations as the tool for when you have multiple, predefined steps (A, B, C, D...), which you define with keyframes.

Here's an example of a simple loading spinner using @keyframes:

/* 1. Define the keyframe sequence */
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 2. Apply the animation to an element */
.spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite; /* name duration timing-function iteration-count */
}

Notice the key differences from the transition example:

  1. Definition: The spin animation is defined separately using @keyframes.
  2. Application: It's applied using the animation property.
  3. Control: We have more control, specifying that it should loop infinite-ly.

Summary: Transitions vs. Animations

The choice between them comes down to control and complexity.

Feature CSS Transitions CSS Animations (@keyframes)
Core Use Case Simple state changes (A β†’ B). Complex, multi-step sequences (A β†’ B β†’ C β†’ ...).
Trigger Implicit: Caused by a CSS property change. Explicit: Applied with the animation property.
Primary Use Interactive feedback (hover, focus, class changes). Looping effects, attention-seekers, scripted sequences.
Control Simpler to set up. transition property. Fine-grained control (iterations, direction, play-state).
Definition Defined directly on the element's selector. Defined separately using @keyframes and then referenced.
Code Structure transition: property duration timing-function; animation: name duration timing-function iteration-count;

Sometimes, the best approach is to use them together. For example, a button might have a continuous, subtle pulsing animation to draw attention, but also a separate transition for a background color change when the user hovers over it.

CSS Animation

For a clear summary and an interesting point on combining the two, let's look at an article from Handoff.design.

First, read the section 'Combining Transitions and Animations' to see a practical example of a hybrid approach. Then, read the FAQ entry for 'What is the difference between transitions and keyframe animations?' as a final summary.


Conclusion

You now have a clear mental model for the two primary ways of creating motion in CSS. This isn't just about making things look good; it's about building intuitive interfaces that communicate state and provide clear feedback to the user.

Key Takeaways:

  • CSS Transitions are for simple, two-state animations triggered by property changes (like hover or adding a class). They are perfect for providing direct feedback to user actions.
  • CSS Animations use @keyframes to define complex, multi-step sequences that are not necessarily tied to a state change. They are ideal for continuous effects, loops, and scripted cinematic sequences.
  • The choice depends on the task: use transitions for reactivity and simplicity, and animations for complexity and control.

What's Next?

We've learned about the two main methods for creating animations in CSS. However, knowing how to start an animation is only half the battle. The performance of your animationβ€”whether it's silky smooth or stutters and janksβ€”depends heavily on which CSS properties you choose to animate.

In our next lesson, we will implement performant animations by primarily using the transform and opacity properties to avoid triggering layout or paint. You'll learn why these properties are special and how animating them allows the browser to take shortcuts for maximum performance.