Skip to content
Velloreth
Web Development 9 min read

CSS Animations: A Practical Introduction

CSS animations run without JavaScript and perform well when you animate the right properties. Learn keyframes, timing, and accessibility.

By

Keyframes

An animation is defined with @keyframes and applied with the animation property.

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.card {
  animation: fade-in 0.3s ease-out;
}

from and to are aliases for 0% and 100%. You can define any number of steps.

@keyframes slide-and-fade {
  0%   { opacity: 0; transform: translateY(20px); }
  60%  { opacity: 1; transform: translateY(-4px); }
  100% { opacity: 1; transform: translateY(0); }
}

The 60% step adds a slight overshoot, which makes the motion feel more natural.

The animation shorthand

animation: name duration timing-function delay iteration-count direction fill-mode play-state;
animation: fade-in 0.3s ease-out 0.1s 1 normal forwards running;

Most of these are optional. The two required values are the name and the duration.

animation: fade-in 0.3s;

Duration

animation-duration: 0.3s;
animation-duration: 300ms;

Guidance: 150–300ms for small UI feedback, 300–500ms for larger transitions. Anything over a second feels slow for interface feedback.

Timing function

animation-timing-function: linear;      /* constant speed */
animation-timing-function: ease;        /* default, slow-fast-slow */
animation-timing-function: ease-in;     /* slow start */
animation-timing-function: ease-out;    /* slow end */
animation-timing-function: ease-in-out; /* slow both ends */
animation-timing-function: cubic-bezier(0.34, 1.56, 0.64, 1); /* overshoot */

ease-out is usually the best default for elements entering the screen, because it decelerates into place. ease-in suits elements leaving.

Delay

animation-delay: 0.2s;

A negative delay starts the animation partway through, which is useful for staggering.

.item:nth-child(2) { animation-delay: -0.1s; }

Iteration count

animation-iteration-count: 1;
animation-iteration-count: infinite;
animation-iteration-count: 2.5;

Direction

animation-direction: normal;           /* forward each cycle */
animation-direction: reverse;          /* backward each cycle */
animation-direction: alternate;        /* forward, then backward */
animation-direction: alternate-reverse;

alternate is useful for looping animations, because it avoids the jump back to the start.

Fill mode

animation-fill-mode: none;      /* default */
animation-fill-mode: forwards;  /* keep the final state */
animation-fill-mode: backwards; /* apply the first state during the delay */
animation-fill-mode: both;

forwards is the one you will use most. Without it, the element snaps back to its original styles when the animation ends.

Play state

animation-play-state: running;
animation-play-state: paused;

Useful for pausing an animation on hover, or for controlling it from JavaScript.

Performance

The single most important rule: animate transform and opacity.

These two properties are handled by the compositor, which runs on the GPU and does not require the browser to recalculate layout or repaint.

/* Fast: composited */
@keyframes slide {
  from { transform: translateX(-100%); }
  to   { transform: translateX(0); }
}
/* Slow: triggers layout on every frame */
@keyframes slide {
  from { left: -100%; }
  to   { left: 0; }
}

Properties that trigger layout: width, height, top, left, right, bottom, margin, padding, font-size.

Properties that trigger paint: color, background-color, box-shadow, border-radius.

Properties that are composited: transform, opacity.

The practical translation:

Instead ofUse
left / toptransform: translate()
width / heighttransform: scale()
margintransform: translate()

will-change

will-change hints that a property will animate, so the browser can prepare.

.card {
  will-change: transform;
}

Use it sparingly. Every will-change consumes memory, and applying it to many elements can make performance worse. Add it just before the animation and remove it after, or apply it only to elements that animate frequently.

Transitions versus animations

Transitions animate between two states when a property changes.

.button {
  background: #2563eb;
  transition: background 0.2s ease;
}

.button:hover {
  background: #1d4ed8;
}

Animations define multiple steps and can run automatically.

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.5; }
}

.loading {
  animation: pulse 1.5s ease-in-out infinite;
}

The rule: transitions for state changes, animations for sequences that run on their own.

Common patterns

Fade in on load

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.hero {
  animation: fade-in 0.5s ease-out both;
}

Slide in from the side

@keyframes slide-in {
  from { transform: translateX(-100%); opacity: 0; }
  to   { transform: translateX(0);     opacity: 1; }
}

Spinner

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

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

linear is correct here, because a spinner should rotate at a constant speed.

Staggered list

.list > * {
  animation: fade-in 0.3s ease-out both;
}

.list > *:nth-child(1) { animation-delay: 0ms; }
.list > *:nth-child(2) { animation-delay: 50ms; }
.list > *:nth-child(3) { animation-delay: 100ms; }

The stagger makes the list feel like it is assembling rather than appearing all at once.

Skeleton loading

@keyframes shimmer {
  from { background-position: -200% 0; }
  to   { background-position: 200% 0; }
}

.skeleton {
  background: linear-gradient(90deg, #eee 25%, #f5f5f5 50%, #eee 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}

Accessibility

Animation can cause discomfort, dizziness, or nausea for some users. Respect the preference.

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

This is one of the few justified uses of !important, because it must override everything.

The alternative, wrapping animations in a no-preference query, is more explicit:

@media (prefers-reduced-motion: no-preference) {
  .card {
    animation: fade-in 0.3s ease-out;
  }
}

Other accessibility considerations:

  • Do not flash more than three times per second. Flashing can trigger seizures.
  • Do not rely on animation alone to convey information.
  • Keep essential content visible without the animation.
  • Avoid large parallax and zoom effects, which are the most likely to cause discomfort.

Choreographing multiple elements

A single animation is simple. Coordinating several is where the craft is.

Stagger with delays. A list that fades in item by item feels more deliberate than one that appears all at once.

.list > * {
  animation: fade-in 0.3s ease-out both;
}

.list > *:nth-child(1) { animation-delay: 0ms; }
.list > *:nth-child(2) { animation-delay: 60ms; }
.list > *:nth-child(3) { animation-delay: 120ms; }
.list > *:nth-child(4) { animation-delay: 180ms; }

Keep the total under about 500ms. A stagger that takes two seconds feels slow, and the user is waiting for content.

Sequence with delays. A multi-part animation can be built from separate keyframes with offsets.

@keyframes slide-up {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.hero-title { animation: slide-up 0.4s ease-out both; }
.hero-sub   { animation: slide-up 0.4s ease-out 0.1s both; }
.hero-cta   { animation: slide-up 0.4s ease-out 0.2s both; }

The both fill mode keeps each element in its final state during the delay, so nothing flashes at the start.

Use a custom property for the delay. This makes the stagger configurable without editing each rule.

.list > * {
  animation: fade-in 0.3s ease-out both;
  animation-delay: calc(var(--i) * 60ms);
}
<li style="--i: 0">First</li>
<li style="--i: 1">Second</li>
<li style="--i: 2">Third</li>

Animating on scroll

The animation-timeline property links an animation to scroll position, so it advances as the user scrolls rather than on a timer.

@keyframes grow {
  from { transform: scaleX(0); }
  to   { transform: scaleX(1); }
}

.progress-bar {
  animation: grow linear;
  animation-timeline: scroll();
}

The bar fills as the page scrolls, with no JavaScript. Support is good in modern browsers, and the fallback is simply a static element, which is acceptable.

For reveal-on-scroll effects, the same technique works with view():

@keyframes reveal {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}

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

The section animates as it enters the viewport. This replaces the IntersectionObserver pattern that previously required JavaScript.

Debugging animations

Animations are harder to inspect than static styles, because the state changes over time.

Slow them down. DevTools has an animation inspector that lets you slow playback to 10% or 25%, which makes timing problems visible.

Step through keyframes. The animation inspector shows each keyframe on a timeline, so you can see exactly which properties change at which point.

Check the computed values. If an animation does not appear to run, check whether the property is being overridden by a more specific rule, or whether the element is display: none.

Look for the fill mode. An animation that runs and then snaps back is missing animation-fill-mode: forwards.

Check for a stacking context. An animated element with a transform creates a stacking context, which can change how it layers against siblings.

Verify the name matches. A typo in the @keyframes name means the animation silently does nothing. There is no error, which makes this a common and confusing bug.

Animating with JavaScript

CSS handles most animation, but JavaScript is needed when the target value is only known at runtime.

Prefer CSS custom properties as the bridge.

element.style.setProperty('--target-x', `${x}px`);
@keyframes move {
  to { transform: translateX(var(--target-x)); }
}

Use the Web Animations API for programmatic control.

element.animate(
  [{ opacity: 0 }, { opacity: 1 }],
  { duration: 300, easing: 'ease-out', fill: 'forwards' },
);

The API returns an object with pause(), play(), cancel(), and finished, which is useful for sequences and for cancelling an animation when the user navigates away.

Avoid animating in a loop with requestAnimationFrame when CSS can do the job. A CSS animation runs on the compositor and continues even when the main thread is busy; a JavaScript loop competes with everything else.

Practical guidance

  • Animate transform and opacity for performance.
  • Use ease-out for elements entering, ease-in for leaving.
  • Keep durations between 150ms and 500ms for UI feedback.
  • Use animation-fill-mode: forwards to keep the final state.
  • Use alternate for looping animations.
  • Respect prefers-reduced-motion.
  • Use will-change sparingly.
  • Prefer transitions for state changes and animations for sequences.

For the related guides, see css-transforms-guide and css-pseudo-classes-guide. For the broader context, improve-inp-core-web-vitals-static-site and css-custom-properties.

Quotable summary

A CSS animation is defined with @keyframes and applied with the animation shorthand, and it runs without JavaScript. Animate transform and opacity, because they are handled by the compositor and do not trigger layout or paint, while animating width, height, top, or left causes layout on every frame and is much slower. The shorthand order is name, duration, timing-function, delay, iteration-count, direction, fill-mode, play-state, and animation-fill-mode: forwards is needed to keep the final state. Transitions are for state changes and animations for multi-step sequences that run on their own. Always respect prefers-reduced-motion, because animation can cause discomfort, and never flash more than three times per second.

Frequently asked questions

What is the difference between a transition and an animation?
A transition animates between two states when a property changes, such as on hover. An animation uses @keyframes to define multiple steps and can run automatically, loop, and reverse.
Which CSS properties are safe to animate?
transform and opacity are the safest, because they are handled by the compositor and do not trigger layout or paint. Animating width, height, top, or left causes layout on every frame and is much slower.
What does animation-fill-mode do?
It controls the styles applied before and after the animation runs. forwards keeps the final keyframe's styles, backwards applies the first keyframe's styles during the delay, and both does both.
How do I make an animation loop forever?
Set animation-iteration-count: infinite. Use animation-direction: alternate to reverse on each cycle, which avoids a jump back to the start.
How do I respect reduced motion preferences?
Wrap animations in a @media (prefers-reduced-motion: no-preference) block, or disable them in a prefers-reduced-motion: reduce block. This respects users who have asked for less motion.
Why is my animation janky?
Usually because it animates a property that triggers layout, such as width or top. Switch to transform and opacity, and add will-change sparingly if the compositor needs a hint.