الانتقال إلى المحتوى
FrontendWeb Performance· 6 دقائق للقراءة

Not All CSS Changes Cost the Same: Style, Layout, Paint and Composite

Geometry changes re-run layout and paint, while transform and opacity can skip both.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

The filter panel on a product listing opens by animating its width from 0 to 280px, and the product grid beside it re-wraps to make room. On the designer's laptop it looks fine; on a budget phone it stutters. We rebuilt it with 1,500 product rows and traced the 300 ms transition in headless Chromium 156. Unthrottled, each frame spent about 4 ms in layout. With the CPU slowed 4x through the DevTools protocol, each layout took 24 to 50 ms across three runs, longer than a whole frame, and only 5 to 9 layout passes fit in the 300 ms instead of 18 or 19. The same panel sliding in with transform ran no layout at all.

The common misconception

The belief: every CSS change costs about the same, because after any change the browser simply repaints the page.

It's an easy model to hold. On a fast machine both versions of the panel look smooth, and each is one line of CSS. Developers say "repaint" for any visual update, and the Elements panel shows a style change the same way whatever property it touches. The difference only shows up when a slow device has to do the expensive version 60 times a second.

What actually happens

Five stages, run only as far as needed

web.dev describes the work behind each frame as a with five stages:

  1. JavaScript, or a CSS animation or transition, makes a visual change.
  2. Style matches CSS rules to elements and computes each element's final styles.
  3. Layout calculates geometry: where every box goes and how big it is.
  4. Paint fills in pixels: text, colors, images, borders and shadows.
  5. Composite draws the painted layers to the screen in the right order.

A change always enters at the top, but it only travels as far down as it has to. That's where the costs split.

Three paths through the pipeline

web.dev names three paths. Changing geometry, such as width, height, left or top, runs every stage, because the browser has to redo . Changing only how a box looks, such as background-image, color or box-shadow, skips layout but repaints the affected area. Changing a property that needs neither layout nor paint lets the browser jump straight to compositing, which web.dev calls the cheapest path, and today only transform and opacity qualify.

We checked each path by changing one property from requestAnimationFrame for 60 frames. Changing left produced a Layout and a Paint event on every frame. background-color and box-shadow painted on every frame with no layout. opacity did neither after the first frame.

Layout costs whatever it touches

Layout has no fixed price. web.dev notes that one element's layout can affect others, so the browser may have to check the rest of the page. MDN calls the first pass layout and every later recalculation a reflow. Moving a fixed drawer with left cost a fraction of a millisecond of layout per frame in our run; widening the panel resized the grid of 1,500 wrapping rows beside it, and layout took about 4 ms per frame.

That matters because of the frame budget. At 60 Hz the browser has 16.66 ms per frame, and web.dev advises finishing your own work within about 10 ms, since the browser needs the rest. Miss it and frames are dropped: that's , and it all happens on the , where input handling waits too.

Compositor-only needs a layer of its own

"Skips paint" comes with a condition. web.dev says the element should be on its own , so the browser can move or fade pixels it already painted. CSS transitions and animations of transform got that in our trace: after the first frame, the main thread ran no style, layout or paint while the panel moved. But when JavaScript set transform on every frame on an element without its own layer, Chromium still painted on every frame. Adding will-change: transform promoted it, and the paints disappeared.

Here is the filter panel, before and after:

CSS filters.css
.page {  display: flex;}.filters {  width: 0; /* geometry: layout on every frame */  overflow: hidden;  transition: width 300ms ease-out;}.filters.open {  width: 280px;}.grid {  flex: 1; /* re-wraps while the panel grows */}

The fix changes the design a little: the panel now slides over the grid instead of pushing it. That's usually the right trade for a panel that opens and closes. If the grid really must make room, let it reflow once, after the panel has opened, not on every frame of the motion.

What you changeStages that runTypical propertiesOur trace, per frame
GeometryStyle, Layout, Paint, Compositewidth, height, left, topLayout and Paint
Appearance onlyStyle, Paint, Compositebackground-color, box-shadowPaint, no Layout
Compositor-only, set from JSStyle, Compositetransform, opacityStyle only, with its own layer
CSS transition of transformComposite, after the first frametransformNothing on the main thread

How to apply it in your project

  1. Find animated geometrySearch your CSS for transition and @keyframes that change width, height, top, left or margins, and your JavaScript for code that sets them on every frame.
  2. Re-express the motionA slide becomes translate, a grow becomes scale, a fade-in becomes opacity. Lay the element out at its final size once, and animate only the compositor-friendly properties.
  3. Measure on a slow CPUTurn on Paint flashing, then record with CPU throttling. A smooth animation on a developer laptop proves little; the trace shows whether layout and paint repeat every frame.
  4. Promote on purposeAdd will-change: transform only to elements that actually animate, and for one-off motion set it from JavaScript just before and remove it afterward, as web.dev suggests.

Key takeaways

  • Every frame can run up to five stages: JavaScript, Style, Layout, Paint and Composite, and a change runs only those it needs.
  • Geometry changes re-run layout and paint; appearance-only changes skip layout; transform and opacity can skip both.
  • Layout cost grows with how much of the page a change affects, and the whole frame has about 10 ms for your work.
  • Compositor-only changes need the element on its own layer; CSS transitions of transform give the browser the best chance.
  • Use will-change sparingly: layers cost memory and create stacking contexts.

📚 المصادر

  1. Rendering performance — web.dev
  2. Stick to compositor-only properties and manage layer count — web.dev
  3. How to create high-performance CSS animations — web.dev
  4. Populating the page: how browsers work — MDN
  5. Discover issues with rendering performance — Chrome DevTools

التعليقات (0)