
Not All CSS Changes Cost the Same: Style, Layout, Paint and Composite
- كاتب موثّق
محتويات المقال
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
What actually happens
Five stages, run only as far as needed
JavaScript, or a CSS animation or transition, makes a visual change. Style matches CSS rules to elements and computes each element's final styles. Layout calculates geometry: where every box goes and how big it is. Paint fills in pixels: text, colors, images, borders and shadows. Composite draws the painted layers to the screen in the right order.
Three paths through the pipeline
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.
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
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.
Compositor-only needs a layer of its own
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.
.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 */}.filters { position: fixed; inset: 0 auto 0 0; width: 280px; /* laid out once */ transform: translateX(-100%); transition: transform 300ms ease-out;}.filters.open { transform: none; /* composite only */}width, height, left, top | |||
background-color, box-shadow | |||
transform, opacity | |||
transform | transform |
How to apply it in your project
Find animated geometry Search your CSS for transitionand@keyframesthat changewidth,height,top,leftor margins, and your JavaScript for code that sets them on every frame.Re-express the motion A slide becomes translate, a grow becomesscale, a fade-in becomesopacity. Lay the element out at its final size once, and animate only the compositor-friendly properties.Measure on a slow CPU Turn 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. Promote on purpose Add will-change: transformonly 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; transformandopacitycan 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 transformgive the browser the best chance.Use will-changesparingly: layers cost memory and create stacking contexts.
التعليقات (0)