Animation Performance: Keeping Motion Smooth
Animation can add polish, but only when it runs smoothly. Janky motion makes a site feel broken, and heavy effects can slow down phones. Smooth animation comes from choosing the right properties and doing less work per frame.
How the browser draws a frame
To show a page, a browser can go through several steps: calculating styles, working out layout (sizes and positions), painting pixels, and compositing layers together. For smooth motion, it needs to do all this in around 16 milliseconds per frame at 60 frames per second. The more steps an animation triggers, the more likely it misses that budget.
Animate cheap properties
- Cheap:
transform(move, scale, rotate) andopacity. Browsers can often handle these on the compositor, without redoing layout or paint - Expensive:
width,height,top,left,margin, and other properties that change layout. They force the browser to recompute positions of other elements - Sometimes costly:
box-shadow,filter, and largebackgroundchanges, which require repainting
So, to move something, use transform: translate() instead of changing left. To fade, use opacity.
Use will-change sparingly
The will-change property hints that an element will animate, so the browser can prepare a layer. But each extra layer uses memory. Apply it only to elements that are about to animate, and remove it afterward. Do not put it on everything.
Limit what is animating
- Animate few elements at once
- Avoid animating very large areas or blurred, translucent layers
- Pause animations that are off screen
- Avoid long chains of nested effects
Prefer CSS over script when possible
Declarative CSS animations and transitions are generally easier for the browser to optimize. If you use JavaScript, drive changes with requestAnimationFrame, avoid reading layout and writing styles repeatedly in a loop, and throttle pointer-driven effects.
Scroll and 3D effects
Scroll-linked effects, parallax, and 3D transforms can look impressive and can be expensive. Keep them simple, test on mid-range phones, and provide a calm fallback. Newer CSS features for scroll-driven animation are not supported equally in all browsers, so use feature checks. See animation and motion in web design, 3D elements in web design, and micro-interactions.
Respect user preferences
Some visitors ask their devices for reduced motion because motion can cause discomfort or distraction. Honor the setting. See reduced motion and animation accessibility and accessible 3D, parallax, and motion effects.
How to test
- Use the Performance panel in developer tools to record a scroll or interaction, and look for dropped frames and long tasks
- Throttle the CPU to imitate a slower phone
- Test on a real mid-range device
- Watch for layout shifts caused by animations. See how to fix Cumulative Layout Shift
Common mistakes
- Animating layout properties
- Running many animations at once
- Leaving animations running off screen
- Testing only on a powerful computer
Some visuals go beyond CSS animation, using graphics hardware directly. See using WebGL and heavy visuals without hurting speed.