Skip to content

CSS Animations

Create smooth CSS animations and transitions with keyframes, transforms, and timing.

What you'll learn

  • The difference between transitions and animations
  • How @keyframes define multi-step animations
  • Transform and opacity as the performant properties to animate
  • Timing functions, delays, and the animation shorthand

Concept

CSS Animations

CSS can animate elements without any JavaScript. There are two tools: transitions for smooth changes between states (like a button slowly changing color on hover), and animations for complex, multi-step, or repeating sequences (like a loading spinner). Both run on the browser's compositor thread, so they can be smoother than JavaScript-driven animation.

Transitions

A transition smoothly interpolates a property when its value changes. Define which properties to transition and how long the change takes:

.button {
  transition: transform 0.2s ease, background-color 0.3s ease;
}
.button:hover {
  transform: scale(1.05);
  background-color: #1d4ed8;
}

When the user hovers, the button grows and changes color over 0.2–0.3 seconds instead of snapping. Transitions are perfect for hover states, focus states, and any property change triggered by a class toggle.

@keyframes Animations

@keyframes define a sequence of styles at points along the timeline (0% to 100%). An animation applies those keyframes to an element:

@keyframes spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

.spinner {
  animation: spin 1s linear infinite;
}

Animations can repeat (infinite), alternate direction, delay start, and use multi-step keyframes (0%, 50%, 100%).

Performant Properties

Not all properties animate smoothly. Animating width, height, top, or margin triggers layout recalculation on every frame, which is expensive. The two properties that animate buttery-smooth are transform (translate, scale, rotate) and opacity — they are handled by the compositor without recalculating layout. If you need to move an element, animate transform: translateX(), not left.

Timing Functions

The timing function controls the pace: linear (constant speed), ease (slow start and end — the default), ease-in, ease-out, ease-in-out, and cubic-bezier() for custom curves. ease-out is generally the most natural-feeling for UI — it starts fast and decelerates, giving a sense of responsiveness. For spring-like effects, use a custom cubic-bezier or the steps() function for a stuttered, frame-by-frame look.

Example

              /* A card that lifts on hover with a smooth transition */
.card {
  transition: transform 0.25s ease-out, box-shadow 0.25s ease-out;
}
.card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

/* A fade-in-up entrance animation */
@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.notification {
  animation: fadeInUp 0.4s ease-out forwards;
}

/* A pulsing loading dot */
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.3; }
}
.loading-dot {
  animation: pulse 1.5s ease-in-out infinite;
}
            

The card uses a transition for a hover lift effect — only transform and box-shadow change. fadeInUp is a keyframe animation for entrance. pulse uses multi-step keyframes (0%, 50%, 100%) with infinite repetition for a loading indicator. All animate transform/opacity for performance.

Try it

  • SVG Optimizer

    SVGs can be animated with CSS — optimize the SVG before animating its paths.

  • Image Color Picker

    Pick colors for animated gradients and keyframe color stops.

Common mistakes

The mistake

Animating layout properties like width, height, or top, causing janky frames.

The fix

Animate transform and opacity only — they run on the compositor thread without triggering layout. To move an element, use transform: translateX/Y. To resize, use transform: scale. To fade, use opacity.

The mistake

Using linear timing for UI animations, which feels mechanical and unnatural.

The fix

Use ease-out for most UI animations — it starts fast and decelerates, feeling responsive and natural. Reserve linear for mechanical things like spinners. For spring effects, use a custom cubic-bezier curve.

Related Cheatsheets

  • CSS Cheatsheet

    Reference for transitions, animations, keyframes, and timing functions.

Practice CSS Animations

5 exercises

Practice CSS Animations

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors