Motion, used sparingly, makes an interface feel responsive and polished. CSS handles it without JavaScript: transitions animate a change from one state to another, and animations run multi-step sequences. The art is restraint — subtle beats flashy.
Transitions
A transition smoothly animates a property when its value changes (on hover, focus, or a class toggle). Declare what to animate and how long:
.button {background: #2563eb;transition: background 0.2s ease, transform 0.2s ease;}.button:hover {background: #1e3a8a;transform: translateY(-2px);}/* transition: property duration timing-function delay; */
Timing functions
| Function | Feel |
|---|---|
ease | Default — gentle start and end |
linear | Constant speed (good for spinners) |
ease-out | Fast then slow (great for entrances) |
cubic-bezier(…) | Custom curves — even overshoot |
Keyframe animations
For multi-step or looping motion, define @keyframes and apply it with animation:
@keyframes fade-in-up {from { opacity: 0; transform: translateY(12px); }to { opacity: 1; transform: translateY(0); }}.card { animation: fade-in-up 0.5s ease-out both; }@keyframes spin { to { transform: rotate(360deg); } }.spinner { animation: spin 1s linear infinite; }
Performance & reduced motion
- Animate
transformandopacity— the GPU handles them smoothly. Animatingwidth,topormarginforces layout recalculation and stutters. - Respect users who prefer less motion — a genuine accessibility need:
@media (prefers-reduced-motion: reduce) {*, *::before, *::after {animation-duration: 0.01ms !important;transition-duration: 0.01ms !important;}}
Practice
Add a hover transition to a button (background + a translateY lift). Write a fade-in-up @keyframes and apply it to cards, and a spinning loader with animation … infinite. Then add a prefers-reduced-motion block that disables it all.
Key takeaways
- 01transition animates a property when its value changes; syntax: property duration timing delay.
- 02Never transition: all — list specific properties; prefer transform/opacity.
- 03Timing functions (ease, linear, ease-out, cubic-bezier) set the motion feel.
- 04@keyframes + animation run multi-step or looping sequences (infinite, both).
- 05Animate transform/opacity for smoothness, and honour prefers-reduced-motion.
