No history yet

Hardware Accelerated Performance

The Path to Smooth Animation

To create animations that feel fluid and responsive, we need to understand how a web browser turns code into pixels. This process, often called the critical rendering pipeline, has three main stages that happen in order: Layout, Paint, and Composite.

When you animate a property like width, height, top, or margin, you force the browser to go through the entire pipeline. It must recalculate the size and position of elements (Layout), then repaint the pixels for the changed elements and any others affected (Paint). These tasks are handled by the CPU and can be slow. If they take too long, the browser misses its chance to draw a new frame, resulting in stuttering, or what developers call jank

To ensure smooth scrolling and animation, everything occupying the main thread, including calculating styles, along with reflow and paint, must take the browser less than 16.67ms to accomplish.

The Composite-Only Rule

The secret to smooth animation is to skip the expensive Layout and Paint steps entirely. We can do this by only animating two specific CSS properties: transform and opacity.

These properties are special because they can be handled directly by the Graphics Processing Unit (GPU) during the Composite stage. Think of the browser rendering your page as a stack of transparent sheets, or layers. Animating transform (like translate, scale, or rotate) just moves or resizes one of these sheets without disturbing the others. Changing opacity simply makes a sheet more or less transparent. The GPU is highly optimised for these kinds of graphical operations.

PropertyTriggers Pipeline?PerformanceUse Case
transform: translate(x, y)Composite OnlyExcellentMoving an element
opacityComposite OnlyExcellentFading in or out
width, heightLayout, Paint, CompositePoorAnimating size changes
top, leftLayout, Paint, CompositePoorAnimating position
background-colorPaint, CompositeModerateChanging colour

By sticking to transform and opacity, you are essentially telling the browser, "Don't bother recalculating everything, just let the GPU move this existing picture around." This offloads the work from the busy CPU to the specialised GPU, resulting in consistently smooth, 60fps animations.

Giving the Browser a Hint

Sometimes, an element you plan to animate isn't automatically put on its own layer. To give the browser a heads-up, you can use the will-change property. Adding will-change: transform; to an element's CSS tells the browser that you intend to animate its transform property soon. In response, the browser will proactively move this element to its own compositor layer, getting it ready for cheap, GPU-powered animation.

/* 
  Tell the browser that this element's transform 
  property is likely to change. 
*/
.box-to-animate {
  will-change: transform;
  transition: transform 0.4s ease-in-out;
}

.box-to-animate:hover {
  transform: translateX(100px);
}

However, use will-change sparingly. Each new layer consumes memory on the GPU. Creating too many layers, a problem known as 'layer explosion', can overwhelm the memory of less powerful devices like mobile phones, potentially crashing the browser. A good rule is to apply will-change right before an animation begins (e.g., on hover) and remove it once the animation is complete.

Debugging Performance

You can verify your animations are performant using Chrome DevTools. Two panels are particularly useful.

Lesson image

First, the Rendering panel. Open it via the Command Menu (Ctrl+Shift+P) and type 'Show Rendering'. Here, you can enable 'Paint flashing', which highlights any area of the screen that is being repainted in green. When you run your animation, if you see green flashes, it means a Paint operation was triggered. For a transform or opacity animation, you should see no green.

Second, the Layers panel. Also accessible from the Command Menu, this panel gives you a 3D, interactive view of all the compositor layers on your page. You can inspect why a layer was created and how much memory it uses. This is the perfect tool to check if will-change is working as expected or if you've accidentally created too many layers.

Quiz Questions 1/5

What are the three main stages of the critical rendering pipeline, in the correct order?

Quiz Questions 2/5

Which of the following CSS animations is most likely to cause 'jank'?

Mastering the rendering pipeline allows you to create animations that not only look good but feel great to interact with, forming a core skill for modern web development.