Chapter 06 of 11Part 2 · CSS

Transform, opacity and compositor only animation

Why transform and opacity animate smoothly when other properties stutter, and how animation affects responsiveness and loading.

10 minBy Zuzo StudioUpdated

10 min

In this chapter you will

  • explain what layout, paint and composite each do to draw a frame
  • rewrite an animation so it changes only transform and opacity
  • audit your hero so nothing stutters and the heading is visible on first paint

Before you startThis builds on CSS keyframe animation: @keyframes and the animation property.

Check your understanding

3 questions

Q1A card should grow slightly on hover. Which animation is cheapest for the browser?

Show the answer

Transition transform from scale(1) to scale(1.04) Scale is part of transform, which only needs compositing: the browser enlarges the already painted card without running layout or paint.

Q2A page runs a heavy script while two boxes animate, one with left and one with transform. What do you expect to see?

Show the answer

The left box stutters and the transform box stays smooth The left animation waits for the busy main thread on every frame. The transform animation only needs compositing, which browsers can do on a separate thread.

Q3Where does will-change: transform belong?

Show the answer

On one element just before a heavy animation that you have measured as a problem, removed afterwards MDN describes will-change as a last resort for existing performance problems, so use it narrowly and only while it is needed.

Key takeaways

  1. Each frame can need style, layout, paint and composite work, and the browser has about 10 milliseconds of it per frame at 60 frames a second.
  2. Animating transform and opacity skips layout and paint, so the browser only composites layers it has already drawn.
  3. Composited CSS animations can keep running smoothly while the main thread is busy; layout animations cannot.
  4. Use will-change as a last resort for a measured problem, on one element, and remove it afterwards.
  5. Motion can move the hero heading but must never start it invisible, because that delays Largest Contentful Paint.
Up next · Chapter 07 · 9 min A GSAP tutorial: tweens, eases and stagger What GSAP is, when it beats CSS, how to load it, and the four tween methods, eases and stagger you will use most.

02FAQ

Questions on this chapter

Something else on your mind? Email the studio and a designer, not a salesperson, replies within a day.

Why should I animate transform instead of top and left?

Changing top or left moves the box in the layout, so the browser recalculates layout and repaints on every frame. Transform moves an already painted layer at the compositing stage, which is far cheaper and can keep running smoothly while the main thread is busy.

Does will-change improve performance?

Sometimes, on one element just before a heavy animation, because it lets the browser create a layer in advance. Used widely it does the opposite, since every layer costs memory, and MDN recommends it only as a last resort for a problem you have measured.