ยท css animations performance
The Art of CSS Animations: Beyond the Basics
A deep dive into creating performant, delightful CSS animations that don't murder your frame rate.
Why CSS animations?
JavaScript animation libraries are powerful, but CSS animations have a secret superpower: they can be composited on the GPU, running at 60fps without touching the main thread.
The golden rules
- Only animate
transformandopacityโ these are the only properties that can be GPU-composited - Use
will-changesparingly โ itโs a hint, not a magic wand - Respect
prefers-reduced-motionโ always provide a reduced-motion alternative
A practical example
@keyframes float {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.floating-element {
animation: float 3s ease-in-out infinite;
}
@media (prefers-reduced-motion: reduce) {
.floating-element {
animation: none;
}
}
Performance tips
- Avoid animating
width,height,top,leftโ usetransforminstead - Use
animation-fill-mode: bothto prevent layout shift at start/end - Test on low-end devices โ what feels smooth on your M-series Mac might chug on an older phone
The key is restraint. A few well-placed animations create delight; too many create chaos.