Transitions and CSS Animations
Add motion that communicates state changes using transitions, keyframes and transforms, while staying fast and respectful of user preferences.
Intermediate · about 3 hours · difficulty 2/5
Learn first: Custom Properties and Modern CSS Features
Motion helps users understand what changed: a menu slides from the button that opened it, a toast fades in, a loading state pulses. Bad motion is slow, janky or nauseating.
Transitions
transition: transform 200ms ease-out, opacity 200ms; animates between two states when a property changes (on hover, focus or a class toggle). Keep UI transitions short: roughly 150 to 300ms.
Keyframe animations
@keyframes define multi-step sequences, applied with animation: spin 1s linear infinite. Use them for loaders, attention cues and entrance effects. animation-fill-mode controls what happens before and after.
Transforms and performance
transform (translate, scale, rotate) and opacity can be animated on the compositor without layout or paint, so they stay smooth at 60fps. Animating width, height, top or box-shadow triggers layout or paint on every frame. will-change is a hint for the browser; use it sparingly.
Easing and accessibility
ease-outfeels responsive for things entering;ease-insuits exits.cubic-bezier()and the newerlinear()allow custom curves.- Wrap non-essential motion in
@media (prefers-reduced-motion: no-preference)or reduce it underreduce. - Never convey information only through animation.
Move on when you can build an animated disclosure, a skeleton loader and a hover effect that all run smoothly in the Performance panel and switch off under reduced motion.
Key concepts
- transition property
- @keyframes and animation
- transform functions
- Compositor-friendly properties: transform and opacity
- Easing functions
- prefers-reduced-motion
- will-change
Mini project
Create a notification toast system in plain HTML/CSS/JS: toasts slide in, stack, auto-dismiss with a progress bar, and use a simple fade under reduced motion.
Check yourself
Why does animating height often stutter while animating transform: scaleY does not?
Free resources
- Learn CSS: Animations (web.dev)
- Using CSS animations (MDN)