Chrome CSS Animations Performance Tips
In this article
- Why Chrome Struggles with Some Animations
- How Chrome’s Compositor Works
- Best Practices for Smooth 60fps Animations
- How to Choose Which Properties to Animate
- Common Problems and How to Fix Them
- Comparing CSS Animations to JavaScript Animations
- Costs and Trade-offs of Compositing
- Safety and Accuracy in Animation
- Alternatives to CSS Animations
- Frequently Asked Questions
To get smooth CSS animations in Chrome, focus on animating only transform and opacity properties, and ensure the animated element is promoted to its own compositor layer (usually via will-change or translateZ(0)). Avoid animating layout-affecting properties like width, height, top, or margin because they force the browser to recalculate layout and repaint on every frame, which can cause jank.
Why Chrome Struggles with Some Animations
Chrome renders web pages in a pipeline: JavaScript → Style → Layout → Paint → Composite. When you animate a property, the browser must re-run parts of this pipeline each frame. If you change left or margin-left, Chrome recalculates layout (the position and size of all elements), then repaints the affected areas, then composites the layers. That is expensive. But transform and opacity are handled entirely on the compositor thread — they don’t trigger layout or paint. The compositor simply slides or fades existing layers, which is much faster.
How Chrome’s Compositor Works
The compositor takes the painted layers of a page and stitches them together on the GPU. When you animate transform, Chrome can move a layer without asking the main thread to redraw anything. For this to happen, the element must be on its own layer. Chrome automatically promotes elements with will-change: transform or will-change: opacity to their own layer. You can also use transform: translateZ(0) or backface-visibility: hidden as a fallback, but will-change is the modern, explicit way. Be careful: creating too many layers consumes GPU memory, so only add will-change to elements you are actually animating.
Best Practices for Smooth 60fps Animations
- Animate only
transformandopacity. These are compositor-friendly. For example, usetransform: translateX()instead ofleft. - Use
will-changesparingly. Add it before the animation starts, and remove it after if you want to free memory. - Keep animations short and simple. Long or complex animations can still cause jank, especially on low-end devices.
- Avoid animating
filterorbox-shadow. These trigger paint on every frame, even though they are not layout properties. If you must animate a shadow, consider using a pseudo-element withopacity. - Use the
requestAnimationFrameAPI for JavaScript-driven animations. This ensures your updates run before the browser paints, reducing dropped frames. - Test on real devices. Chrome DevTools’ Performance panel can show you frame rates and where time is spent, but emulation doesn’t always reflect real hardware.
How to Choose Which Properties to Animate
Ask yourself: “Does this property affect the element’s geometry or its visual appearance in a way that requires repainting?” If yes, it’s likely expensive. Safe properties include transform (translate, scale, rotate) and opacity. Properties like color, background-color, and border-radius cause repaint but not layout, so they are moderately expensive. Layout properties (width, height, top, left, margin, padding, font-size) are the worst. If you need to move an element, use transform: translate(). If you need to resize it, use transform: scale() — but be aware that scaling can blur text if you scale up too much.
Common Problems and How to Fix Them
- Janky or stuttering animations: Check if you’re animating a layout property. Switch to
transform. Also check for large repaint areas — if the element is huge, try reducing its size or usingcontain: layoutto isolate it. - Flickering or disappearing elements: This often happens when an element is promoted to its own layer but the GPU can’t handle it. Try adding
backface-visibility: hiddenortranslateZ(0)to force a layer, but if the problem persists, reduce the number of animated elements. - High CPU usage: Too many composited layers or complex animations can overload the CPU. Simplify the animation, reduce the number of simultaneous animations, or lower the frame rate (e.g., use
animation-durationlonger than 1s). - Animations not running on some devices: Older mobile browsers may not support
will-change. Use the fallbacktranslateZ(0)and test.
Comparing CSS Animations to JavaScript Animations
CSS animations are often the best choice for simple, declarative transitions (like hover effects) because they are handled by the browser and can be optimized automatically. JavaScript animations (using requestAnimationFrame or libraries like GSAP) give you more control over timing and easing, and can pause or reverse mid-way. However, if you write JavaScript that changes the same layout properties every frame, it will be just as slow as a CSS animation doing the same. For complex, multi-step animations, JavaScript libraries often use transform under the hood anyway, so the performance rules remain the same.
Costs and Trade-offs of Compositing
Promoting an element to its own layer uses GPU memory. On mobile devices, memory is limited, so overusing will-change can cause the browser to crash or slow down. Also, each layer adds a small compositing cost. The key is to balance: use will-change only on elements that are actively animating, and remove it when the animation ends (via JavaScript or by using a class that you remove). Another cost is that animating large elements (like full-screen backgrounds) can be heavy because the layer is large. Try to keep animated elements small or use contain: paint to limit the repaint area.
Safety and Accuracy in Animation
There are no safety risks in CSS animations, but there are accessibility concerns. For example, users with motion sensitivity may prefer reduced motion. You can use the prefers-reduced-motion media query to disable or simplify animations for those users. Also, avoid flashing animations that could trigger seizures — keep any flashing below three times per second. Always test your animations with Chrome’s built-in “Rendering” tab in DevTools to see repaint areas and frame rate.
Alternatives to CSS Animations
If CSS animations are not smooth enough, consider using the Web Animations API (WAAPI) — it runs on the main thread but is more performant than manual JavaScript because it can be optimized by the browser. Another option is to use GPU-accelerated libraries like GSAP or Framer Motion, which handle compositing and fallbacks automatically. For very complex effects, you might use Canvas or WebGL, but those are overkill for most UI animations. The simplest alternative is to reduce the animation’s complexity or remove it entirely if it’s not essential.
Frequently Asked Questions
Why is my CSS animation lagging in Chrome but not Firefox?
Chrome and Firefox have different rendering engines. Chrome’s compositor may be more sensitive to layout thrashing, or the element may not be promoted to a layer automatically. Try adding will-change: transform and see if that fixes it. Also check if you have a lot of other elements on the page that cause repaints.
Does using translateZ(0) really help?
Yes, it’s a legacy hack that forces the browser to create a compositing layer. It works, but will-change is the modern, more semantic way. However, overusing translateZ(0) can waste memory, so only apply it to elements that are animating.
Can I animate height with a smooth transition?
You can, but it will trigger layout on every frame, which is slow. Instead, use transform: scaleY() or animate max-height as a workaround, but the best approach is to use grid-template-rows: 0fr to 1fr (modern CSS) or use JavaScript to measure the height and animate transform.
How many CSS animations can I run at once?
There’s no fixed number, but each animation uses CPU/GPU resources. On a modern desktop, you can run dozens of simple transform/opacity animations, but on a low-end phone, even a few might cause jank. Use DevTools to measure frame rate and reduce the number if needed.
What is the best way to test animation performance?
Open Chrome DevTools, go to the Performance tab, record a few seconds of the animation, and look at the frame rate and the “Rendering” section. Also use the “Rendering” tab to enable “Paint flashing” to see repaint areas. For a quick check, look at the FPS meter in the Performance panel.
Should I use will-change on all elements that have a transition?
No, only on elements that you are actively animating, and ideally only for the duration of the animation. Adding will-change to every element with a transition can exhaust GPU memory and actually hurt performance.