CSS Styling & Effects

CSS Animations

CSS Animations enable complex, multi-step animations using keyframes to define intermediate steps, providing precise control over timing, duration, iteration, and direction of animated property changes.

How do you create an animation in CSS?

Define the steps with @keyframes and attach them to an element with the animation property: animation: spin 1s linear infinite;. Keyframes describe the styles at points from from (0%) to to (100%), and the browser interpolates between them. Unlike transitions, animations can run automatically, loop and alternate.

@keyframes fade-up {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: none; }
}
.card { animation: fade-up .5s ease-out both; }

@keyframes spin { to { transform: rotate(360deg); } }
.loader { animation: spin 1s linear infinite; }

Interactive CSS Animations Demo

Code Editor

<div class="loader"></div>
<div class="pulse">Pulse</div>
<div class="slide">Slides in once</div>

Preview

Animation Fundamentals

CSS animations use keyframes to define how elements change over time. Unlike transitions, animations can have multiple steps, repeat infinitely, and run automatically without user interaction.

Complete Animation Syntax

@keyframes animationName {
  0% { /* starting state */ }
  50% { /* middle state */ }
  100% { /* ending state */ }
}

.element {
  animation: animationName 2s ease-in-out 0.5s infinite alternate both;
}

Name: Reference to @keyframes rule

Duration: How long animation takes

Timing Function: Speed curve

Delay: When to start

Iteration Count: How many times to repeat

Direction: Forward, reverse, alternate

Fill Mode: Before/after animation state

Play State: Running or paused

Keyframe Techniques

Keyframes define the intermediate steps of an animation. You can use percentages or the from/to keywords to create smooth or stepped animations.

Simple Keyframes

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

Two-step animations using from/to keywords

Multi-step Keyframes

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

Multiple keyframes with percentage values

Keyframe Tips
  • • Use comma-separated percentages for same properties: 0%, 100% { opacity: 1; }
  • • Keyframes at 0% and 100% define start and end states
  • • Missing 0% or 100% will use element's original styles
  • • Order keyframes by percentage for better readability

Animation Properties Deep Dive

Iteration & Direction

animation-iteration-count: 3; /* or infinite */
animation-direction: normal; /* reverse, alternate, alternate-reverse */

Control how many times and in which direction animations play

Fill Mode

animation-fill-mode: none; /* forwards, backwards, both */

Determines element state before/after animation

Fill Mode Explained

none

Default styles before/after

forwards

Retains final keyframe styles

backwards

Applies first keyframe during delay

both

Combines forwards + backwards

Performance & Best Practices

Best Practices

  • • Animate transform and opacity for best performance
  • • Use will-change property for complex animations
  • • Keep animations under 300ms for UI feedback
  • • Use animation-play-state for user control
  • • Provide fallbacks for older browsers
  • • Test on lower-powered devices

Performance Tips

  • • Avoid animating layout properties (width, height)
  • • Use transform: translate3d() for GPU acceleration
  • • Reduce animation complexity on mobile
  • • Consider prefers-reduced-motion media query
  • • Batch animations with single keyframe rules
  • • Remove unused animations from DOM
Accessibility Consideration

Always respect the prefers-reduced-motion media query. Some users have vestibular disorders and need reduced motion for accessibility. Provide alternative static states or simplified animations.

CSS Animations — step by step

1 / 4
1

@keyframes Syntax

Define animation frames with @keyframes, using from/to or percentage stops to describe how properties change over time.

1 of 4
💡Animate Transform and opacity Only
Animating width, height, top, or left triggers expensive layout recalculations. Use transform: translate() and scale() instead — they are GPU-accelerated and never cause reflow.

🎯 Quick Check

Which two CSS properties are safest to animate for 60fps performance?

CSS Animations: summary

Key points

  • The shorthand order is name, duration, timing function, delay, iteration count, direction, fill mode.
  • animation-fill-mode: both keeps the first keyframe during the delay and the last one after the animation ends.
  • animation-timeline: view() drives an animation by scroll position instead of time, with no JavaScript.
  • Use @media (prefers-reduced-motion: reduce) to disable non-essential animation.

Common mistakes

  • Animating layout properties (width, margin, top) instead of transform, which causes jank on slow phones.
  • Content that starts at opacity: 0 and never appears if the animation fails or is disabled.

Frequently asked questions

›How do I make a CSS animation loop forever?

Set animation-iteration-count: infinite, or include infinite in the shorthand, as in animation: pulse 2s ease-in-out infinite;.

›How do I pause a CSS animation?

Set animation-play-state: paused, for example on hover or with a class toggled from JavaScript.

›Are CSS animations better than JavaScript animations?

For simple UI motion, yes: they're declarative and can run off the main thread when they only change transform and opacity. Use JavaScript, such as the Web Animations API, for interactive or physics-based motion.