0Pricing
React Academy · Lesson

CSS Keyframe Animations with React

Apply and remove @keyframes animations dynamically based on component state and events.

CSS Keyframe Animations with React is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Defining @keyframes

A @keyframes rule describes a multi-step animation by naming property values at points along a timeline, using from/to or percentages like 0%, 50%, 100%.

Unlike transitions, keyframes can describe complex multi-stage motion that runs on its own without a state change at every step.

The animation Shorthand

You apply a keyframe set with the animation property: animation: slideIn 400ms ease forwards. It bundles the name, duration, timing function, and fill mode.

The forwards fill mode keeps the element at its final keyframe values after the animation ends, preventing a snap back to the start.

Applying an Animation Class via State

In React you typically add a class that carries the animation declaration when state becomes true. For instance, adding is-entering starts the keyframes the moment the element mounts or a flag flips.

This keeps the animation logic in CSS while React just decides when to attach the class.

animation-fill-mode

animation-fill-mode controls what styles apply before and after the animation runs. forwards retains the last keyframe, backwards applies the first keyframe during any delay, and both does both.

Without forwards, an element animating to a faded-in state would jump back to invisible when the animation completes.

animation-direction alternate

animation-direction: alternate plays the animation forward then backward on each iteration. Combined with animation-iteration-count: infinite it creates a smooth back-and-forth like a pulsing or breathing effect.

This is handy for loading indicators or subtle attention-drawing motion.

Pausing with animation-play-state

animation-play-state can be running or paused. You can drive it from React state, for example pausing a marquee on hover by switching a class.

Because it pauses rather than resets, the animation resumes from exactly where it stopped.

Re-Triggering by Changing the key Prop

A keyframe animation only plays once when its class is first applied; re-adding the same class will not restart it. The cleanest React trick is to change the element key so React remounts a fresh node, replaying the animation from the start.

For example key={count} on a toast remounts it each time count increments.

Respecting prefers-reduced-motion

Some users enable reduced motion in their OS for comfort or accessibility. Wrap nonessential animations in @media (prefers-reduced-motion: reduce) and disable or shorten them there.

Honoring this preference makes your interface more inclusive and avoids triggering discomfort.

Triggering on Scroll with IntersectionObserver

To animate elements as they enter the viewport, use an IntersectionObserver in a useEffect. When the entry becomes visible you set state, which adds the animation class.

This produces reveal-on-scroll effects without heavy scroll listeners running on every frame.

Orchestrating with animation-delay

animation-delay staggers when each keyframe animation begins. Giving a list of items increasing delays makes them animate in sequence for a choreographed reveal.

You can compute the delay per item in React with style={{ animationDelay: i * 80 + 'ms' }}.

Keyframes vs Transitions

Use transitions for simple two-state changes driven by interaction, and keyframes when you need multi-step motion, looping, or animation that runs independently of a state toggle.

Both are pure CSS, so React only decides when classes attach and the browser handles the frames.

Quick Check

Test your understanding of keyframe behavior.

Recap

You learned to define @keyframes, apply them with the animation shorthand, control fill mode and direction, pause and re-trigger animations, and reveal elements on scroll with IntersectionObserver.

You also saw how to respect prefers-reduced-motion for accessible motion.

Frequently asked questions

Is the “CSS Keyframe Animations with React” lesson free?

Yes — the full text of “CSS Keyframe Animations with React” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “CSS Keyframe Animations with React”?

Apply and remove @keyframes animations dynamically based on component state and events. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CSS Keyframe Animations with React” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CSS Transitions with React State Changes
  2. CSS Keyframe Animations with React
  3. useLayoutEffect for Pre-Animation Measurements
  4. Enter and Exit Animations Without a Library
← Back to React Academy