Skip to content

CSS Animation Tutorial: Transitions, Keyframes and Performance

A CSS animation tutorial — transitions, @keyframes, animation properties, easing, practical examples, performance tips and accessible reduced-motion support.
CSS Animation Tutorial: Transitions, Keyframes and Performance — WebDesigningPune guide
Quick answer

CSS offers two animation tools: transitions, which animate a change between two states (such as hover), and @keyframes animations, which run multi-step sequences. For smooth performance, animate only transform and opacity, keep durations short (150–400ms for interface feedback) and respect users who prefer reduced motion with the prefers-reduced-motion media query.

Good CSS animation on a website is almost invisible. A button that gently responds when you hover, a menu that slides in smoothly, a card that lifts slightly — these small touches make a site feel polished and responsive. Bad animation does the opposite: it slows people down, distracts them, or even makes some visitors feel unwell.

This guide shows you how to use CSS animation with purpose, keep it fast, and make sure it works for everyone.

CSS transitions

CSS animation: CSS transitions
.button {
  background: #1D5FB4;
  transform: translateY(0);
  transition: transform 200ms ease, background-color 200ms ease;
}
.button:hover {
  background: #164A8C;
  transform: translateY(-2px);
}

CSS animation with keyframes

@keyframes fade-up {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}
.card {
  animation: fade-up 500ms ease-out both;
}
.card:nth-child(2) { animation-delay: 100ms; }
.card:nth-child(3) { animation-delay: 200ms; }

Animation properties

Property Example
animation-name fade-up
animation-duration 500ms
animation-timing-function ease-out, cubic-bezier(…)
animation-delay 100ms
animation-iteration-count 1, infinite
animation-direction normal, alternate
animation-fill-mode both, forwards

Practical examples

Real-world example screenshot for “Practical examples”

Loading spinner

@keyframes spin { to { transform: rotate(360deg); } }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid #E2E8F0;
  border-top-color: #1D5FB4;
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

Scroll-driven animation (modern browsers)

@supports (animation-timeline: view()) {
  .reveal {
    animation: fade-up linear both;
    animation-timeline: view();
    animation-range: entry 0% cover 30%;
  }
}

Performance tips

  • Animate transform and opacity; avoid width, height, top and left.
  • Use will-change sparingly, only on elements about to animate.
  • Avoid animating many elements at once on mobile.
  • Never hide important content until an animation runs.

Respect reduced motion

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Tip: For complex timelines and scroll-linked effects, JavaScript libraries such as GSAP give more control, but the same rules apply: transform and opacity, short durations and reduced-motion support.

When to animate — and when not to

Good uses Avoid
Feedback on hover, focus and click Animating everything on the page
Showing where something came from (menus, modals) Long entrance animations that delay content
Loading indicators Flashing or rapidly moving elements
Subtle scroll reveals below the fold Hiding key content until animation runs
Code editor or browser screenshot supporting “When to animate — and when not to”

Timing guidelines

  • Micro-interactions (buttons, toggles): about 100–200 ms.
  • Small transitions (dropdowns, tooltips): about 150–300 ms.
  • Larger movements (modals, panels): about 250–400 ms.
  • Easing: use ease-out for elements entering, ease-in for elements leaving.

Debugging animations

  1. Slow it downUse the Animations panel in Chrome DevTools to replay at 10% speed.
  2. Check performanceRecord in the Performance panel and look for layout or paint warnings.
  3. Test reduced motionEmulate prefers-reduced-motion in DevTools rendering settings.
  4. Try on a low-end phoneAnimations that are smooth on a laptop may stutter on older devices.

Build layouts first with flexbox and grid, then explore JavaScript guides.

Frequently asked questions

What is the difference between CSS transitions and animations?

Transitions animate between two states when a property changes; keyframe animations run defined sequences and can loop or start automatically.

Which CSS properties are best to animate?

Transform and opacity, because browsers can animate them smoothly without recalculating layout.

How do I make CSS animations accessible?

Keep motion subtle, avoid flashing, and reduce or remove animation with the prefers-reduced-motion media query.

Can CSS animate on scroll without JavaScript?

Yes, with scroll-driven animations using animation-timeline in supporting browsers; provide a fallback for others.

Should I use CSS or JavaScript for animations?

Use CSS for simple transitions and keyframes. Use JavaScript libraries such as GSAP for complex sequences, scroll-linked effects and precise control.

Do animations affect SEO?

Not directly, but heavy animations can slow pages and hurt Core Web Vitals, and hidden content may be harder for users to reach.

How do I stop an animation from repeating?

Set animation-iteration-count to 1 (the default) and use animation-fill-mode: forwards to keep the final state.


Further reading: MDN’s web development learning area.

First published November 17, 2026 · Last updated November 26, 2026