Fix guides · Performance

How to fix forced reflow (layout thrashing)

Scripts that change the page and then immediately measure it, forcing the browser to redo its layout again and again. How to find the code and batch reads and writes.

Time to fix
30–90 minutes
Skill needed
Developer
Who notices
Visitors on phones: scrolling and taps feel sluggish

Why it matters

When a script changes something on the page (a class, a style, some content) and then reads a size or position, such as offsetWidth or getBoundingClientRect(), the browser must stop and recalculate the layout right then.

Doing that in a loop, change, measure, change, measure, is called layout thrashing. Each round is a full layout, so a few lines of code can block the page for a long time.

“This forces the browser to immediately do a layout, which interrupts script execution and results in poor performance.”

Chrome: Forced reflow insight

Before you start

  • Your report lists the function and file that caused each reflow. If it's a third-party script (a slider, chat or analytics), update or replace it; you can't change its code.
  • Chrome flags this when a single forced reflow takes longer than 30 ms.
  • The fix is always the same idea: do all the measuring first, then all the changes.

Fix it on your platform

Shopify

  1. If the file belongs to an app, contact its developer or remove the app.
  2. If it's your theme's JavaScript, batch DOM reads before writes (see the example under “Any other site”).

WordPress

  1. If the file belongs to a plugin (the address usually includes /wp-content/plugins/<name>/), update the plugin or replace it.
  2. If it's your theme's own script, batch DOM reads before writes.

Webflow

  1. Webflow's own scripts are managed by Webflow. If the file is in your custom code or an embed, fix or remove that script.
  2. Complex scroll-driven interactions can also cause repeated layouts; simplify them on key pages.

Wix Managed by the platform

  1. Wix's own scripts are managed by Wix. If the file is from an app or your custom code (Velo or Custom Code settings), fix or remove that code.

Squarespace Managed by the platform

  1. Squarespace's own scripts are managed by Squarespace. If the file comes from code injection or an extension, fix or remove that code.

Next.js

  1. Look for effects that measure elements after setting state, such as reading getBoundingClientRect() in a loop or on every scroll event.
  2. Measure once in requestAnimationFrame, store the values, then update. Prefer CSS (container queries, sticky positioning, transforms) over measuring in JavaScript.

Any other site

  1. Read all the sizes you need first, then make all the changes.
  2. Avoid reading layout properties inside scroll handlers; use IntersectionObserver or ResizeObserver instead.
Batch reads, then writes
// Slow: each loop measures after a change
items.forEach((el) => { el.style.height = el.offsetWidth / 2 + "px"; });

// Fast: measure everything, then change everything
const widths = items.map((el) => el.offsetWidth);
items.forEach((el, i) => { el.style.height = widths[i] / 2 + "px"; });

Check that it's fixed

  • Re-run your StudioLNC audit: the forced reflow insight should no longer list your code.
  • In Chrome DevTools, record a Performance trace and look for purple “Layout” blocks marked as forced.

Find every issue like this on your site

Free audit of performance, accessibility, SEO and security, with fixes for your platform.