CSS Animations: From Basics to Production-Ready Effects
CSS animations can transform a static page into an engaging experience. Here's how to use them effectively — with performance in mind.
Transitions vs Animations
Transitions animate between two states (triggered by hover, focus, class change):
.button {
background: #6366f1;
transition: background 0.2s ease, transform 0.2s ease;
}
.button:hover {
background: #4f46e5;
transform: scale(1.05);
}Animations run independently using @keyframes:
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.element {
animation: fadeIn 0.3s ease-out;
}Rule of thumb: Use transitions for interactive state changes, animations for entrance effects and looping motion.
The animation Shorthand
animation: name duration timing-function delay iteration-count direction fill-mode;Example:
animation: fadeIn 0.5s ease-out 0s 1 normal forwards;- forwards: Element keeps the final keyframe state after animation ends
- infinite: Loop forever
- alternate: Reverse direction on each cycle
Useful @keyframes Patterns
Fade in from below (page load):
@keyframes fadeUp {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}Pulse (attention indicator):
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}Spin (loading spinner):
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner { animation: spin 1s linear infinite; }Shake (error feedback):
@keyframes shake {
0%, 100% { transform: translateX(0); }
25% { transform: translateX(-5px); }
75% { transform: translateX(5px); }
}Performance: What to Animate
Only these properties are GPU-accelerated (cheap to animate):
transform(translate, scale, rotate)opacity
Avoid animating:
width,height,top,left— triggers layout recalculationbox-shadow,border-radius— triggers repaintcolor,background— triggers repaint (but acceptable for simple transitions)
/* Bad: animates width (triggers layout) */
@keyframes grow { to { width: 200px; } }
/* Good: animates transform (GPU-accelerated) */
@keyframes grow { to { transform: scaleX(2); } }Respecting User Preferences
Always honor prefers-reduced-motion:
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after {
animation-duration: 0.01ms !important;
transition-duration: 0.01ms !important;
}
}This ensures users who are sensitive to motion aren't affected by your animations.
Staggered Animations
Create sequential entrance effects with animation-delay:
.item:nth-child(1) { animation-delay: 0ms; }
.item:nth-child(2) { animation-delay: 100ms; }
.item:nth-child(3) { animation-delay: 200ms; }Or use CSS custom properties for cleaner code:
.item { animation: fadeUp 0.4s ease-out calc(var(--i) * 100ms) forwards; opacity: 0; }Combine animations with visual effects for polished UIs. Create animated gradient backgrounds with our Gradient Generator, or add hover transitions to text shadow and filter effects.