layout thrashing
Slowing a page by repeatedly changing styles and then reading sizes, forcing the browser to recalculate layout again and again.
In more detail
JavaScript positioning code that measures and moves elements on every scroll can cause it. Native CSS anchor positioning avoids it by letting the browser position elements in its normal layout pass.
Example
In the browser: The fix is to batch: read all the measurements you need first, then make all the writes. requestAnimationFrame is a natural place to group writes for the next frame.Related terms
- CSS anchor positioning — A CSS feature that positions an element relative to another named anchor element, with built-in fallbacks when it would overflow.
- bounding box — The smallest rectangle that encloses a shape or element, given by its position and size or by two opposite corners.
- forced synchronous layout — When JavaScript reads a geometry value such as offsetHeight after changing styles, making the browser run layout immediately to give an up-to-date answer.
- batching — Doing many similar pieces of work together in one operation, such as loading all needed owners with one query instead of one query each.
- layout — The rendering stage in which the browser calculates the exact position and size of every box on the page.
- requestAnimationFrame() — A function that schedules a callback to run just before the browser's next repaint, the right place to update animations.
Taught in
- Floating Primitives: Popovers, Tooltips & Positioning — Design Systems & Component Engineering
- Layout, Reflow, Paint And GPU Compositing Layers — Frontend Architecture & Web Platform
- Critical Rendering Path: Preload, Prefetch And Scripts — Frontend Architecture & Web Platform