Transitions and animations
Motion makes interfaces feel alive and helps users understand changes: a button responding to a click, a menu sliding open, a loading spinner. CSS can do all of this without JavaScript.
Transitions: smooth changes between states
A transition animates a property when it changes, for example on :hover or when a class is added.
transition: <property> <duration> <timing-function> <delay>;Timing functions
UI motion guideline: keep it short (150–300ms). Use
ease-outfor things entering andease-infor things leaving.
Transforms
transform moves, rotates, scales and skews elements without affecting the layout, so neighbouring elements don't move.
Keyframe animations
@keyframes defines a multi-step animation that can loop, run on page load, or play many times.
| Property | Example |
|---|---|
animation-name |
pulse |
animation-duration |
1.2s |
animation-timing-function |
ease-in-out |
animation-delay |
300ms |
animation-iteration-count |
3, infinite |
animation-direction |
alternate (play forwards, then backwards) |
animation-fill-mode |
both (keep the first/last frame styles) |
Performance: animate only transform and opacity
Browsers can animate transform and opacity on the GPU, smoothly, at 60fps. Animating width, height, top, left or margin forces the browser to recalculate the layout on every frame, which causes jank.
/* ✗ Janky */
.panel { transition: left 300ms; }
/* ✓ Smooth */
.panel { transition: transform 300ms; }Respect reduced motion
Some users get dizzy or nauseous from motion and turn on "reduce motion" in their OS. Honour it:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
scroll-behavior: auto !important;
}
}Practice
Create:
- A card that lifts and grows its shadow on hover (a transition).
- A three-dot "typing…" loading indicator where each dot bounces with a staggered
animation-delay(keyframes). - Make sure both respect
prefers-reduced-motion.
1.Which properties are cheapest to animate?
2.What's the difference between a transition and a keyframe animation?