Skip to content
Retr0 Academy
Advanced CSS

Advanced CSS · 3 min read

Animations and Transitions

Transitions vs keyframes, timing functions, composited properties for 60fps, and respecting reduced motion.

  • A transition animates between two states when a property changes (hover, a class toggle).
  • A keyframe animation runs a timeline you define, can loop, and needs no state change.
Same distance, same duration: the timing function changes the feel.
Same distance, same duration: the timing function changes the feel.

Transitions

css

.button {
  transition: transform 150ms ease-out, background-color 150ms;
}
.button:hover { transform: translateY(-2px); }
.button:active { transform: translateY(1px); }

Never write transition: all: it animates properties you did not intend and costs performance.

Keyframes

Retro loading bar, glitch title and press effect
<h1 class="glitch" data-text="LOADING">LOADING</h1>
<div class="bar"><span></span></div>
<button class="press">Mint</button>
<label><input type="checkbox" id="rm"> Simulate reduced motion</label>

Edit this example in the Playground.

Open in Playground

Notice that the loading bar animates transform: scaleX(), not width.

Performance: animate transform and opacity

The browser renders in stages: style → layout → paint → composite.

AnimatingTriggersCost
width, height, top, marginlayout + paint + composite every frameHigh
background-color, box-shadowpaint + compositeMedium
transform, opacitycomposite only (often on the GPU)Low

So move things with translate(), grow them with scale(), and fade them with opacity.

Respect reduced motion

Some people get dizzy or nauseous from motion. The OS setting prefers-reduced-motion: reduce is your cue to remove large movement. Every animated diagram on this site honours it.

Animating from JavaScript

The Web Animations API gives you the keyframe engine with promises:

js

const anim = el.animate(
  [{ transform: "scale(1)" }, { transform: "scale(1.2)" }, { transform: "scale(1)" }],
  { duration: 300, easing: "ease-out" }
);
await anim.finished;

Summary

Use transitions for state changes and keyframes for timelines. Animate only transform and opacity for smooth 60fps, pick easing deliberately, and always provide a reduced-motion path.