# What forces layout / reflow All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or [layout thrashing](http://www.kellegous.com/j/2013/01/26/layout-performance/), and is common performance bottleneck. ### Element ##### Box metrics * `elem.offsetLeft`, `elem.offsetTop`, `elem.offsetWidth`, `elem.offsetHeight`, `elem.offsetParent` * `elem.clientLeft`, `elem.clientTop`, `elem.clientWidth`, `elem.clientHeight` * `elem.getClientRects()`, `elem.getBoundingClientRect()` ##### Scroll stuff * `elem.scrollBy()`, `elem.scrollTo()` * `elem.scrollIntoView()`, `elem.scrollIntoViewIfNeeded()` * `elem.scrollWidth`, `elem.scrollHeight` * `elem.scrollLeft`, `elem.scrollTop` also, setting them ##### Focus * `elem.focus()` can trigger a *double* forced layout ([source](https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/dom/Element.cpp?q=updateLayoutIgnorePendingStylesheets+-f:out+-f:test&sq=package:chromium&dr=C)&l=2923) ##### Also… * `elem.computedRole`, `elem.computedName` * `elem.innerText` ([source](https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/dom/Element.cpp?q=updateLayoutIgnorePendingStylesheets+-f:out+-f:test&sq=package:chromium&dr=C)&l=3440)) ### getComputedStyle `window.getComputedStyle()` will typically force style recalc `window.getComputedStyle()` will force layout, as well, if any of the following is true: 1. The element is in a shadow tree 1. There are media queries (viewport-related ones). Specifically, one of the following: ([source](https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/css/MediaQueryExp.cpp?type=cs&q=f:MediaQueryExp.cpp+MediaQueryExp::IsViewportDependent&l=192)) * `min-width`, `min-height`, `max-width`, `max-height`, `width`, `height` * `aspect-ratio`, `min-aspect-ratio`, `max-aspect-ratio` * `device-pixel-ratio`, `resolution`, `orientation` , `min-device-pixel-ratio`, `max-device-pixel-ratio` 1. The property requested is one of the following: ([source](https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/css/CSSComputedStyleDeclaration.cpp?dr=C&q=f:CSSComputedStyleDeclaration.cpp+isLayoutDependent&sq=package:chromium)) * `height`, `width` * `top`, `right`, `bottom`, `left` * `margin` [`-top`, `-right`, `-bottom`, `-left`, or *shorthand*] only if the margin is fixed. * `padding` [`-top`, `-right`, `-bottom`, `-left`, or *shorthand*] only if the padding is fixed. * `transform`, `transform-origin`, `perspective-origin` * `translate`, `rotate`, `scale` * `grid`, `grid-template`, `grid-template-columns`, `grid-template-rows` * `perspective-origin` * These items were previously in the list but appear to not be any longer (as of Feb 2018): `motion-path`, `motion-offset`, `motion-rotation`, `x`, `y`, `rx`, `ry` ### window * `window.scrollX`, `window.scrollY` * `window.innerHeight`, `window.innerWidth` * `window.getMatchedCSSRules()` only forces style ### Forms * `inputElem.focus()` * `inputElem.select()`, `textareaElem.select()` ### Mouse events * `mouseEvt.layerX`, `mouseEvt.layerY`, `mouseEvt.offsetX`, `mouseEvt.offsetY` ([source](https://cs.chromium.org/chromium/src/third_party/WebKit/Source/core/events/MouseEvent.cpp?type=cs&q=f:Mouse+f:cpp+::computeRelativePosition&sq=package:chromium&l=517)) ### document * `doc.scrollingElement` only forces style ### Range * `range.getClientRects()`, `range.getBoundingClientRect()` ### SVG * Quite a lot; haven't made an exhaustive list, the following are from [Tony Gentilcore's 2011 Layout Triggering List](https://web.archive.org/web/20180916041757/http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html) now deleted blog post: #### SVGLocatable * `computeCTM()`, `getBBox()` #### SVGTextContent * `getCharNumAtPosition()`, `getComputedTextLength()`, `getEndPositionOfChar()`, `getExtentOfChar()`, `getNumberOfChars()`, `getRotationOfChar()`, `getStartPositionOfChar()`, `getSubStringLength()`, `selectSubString()` #### SVGUse * `instanceRoot` ### contenteditable * Lots & lots of stuff, …including copying an image to clipboard ([source](https://cs.chromium.org/search/?q=UpdateStyleAndLayoutIgnorePendingStylesheets+file:%5Esrc/third_party/WebKit/Source/core/editing/+package:%5Echromium$&type=cs)) ## *Appendix * Reflow only has a cost if the document has changed and invalidated the style or layout. Typically, this is because the DOM was changed (classes modified, nodes added/removed, even adding a psuedo-class like :focus). * If layout is forced, style must be recalculated first. So forced layout triggers both operations. Their costs are very dependent on the content/situation, but typically both operations are similar in cost. * What should you do about all this? Well, the `More on forced layout` section below covers everything in more detail, but the short version is: 1. `for` loops that force layout & change the DOM are the worst, avoid them. 1. Use DevTools Timeline to see where this happens. You may be surprised to see how often your app code and library code hits this. 1. Batch your writes & reads to the DOM (via [FastDOM](https://github.com/wilsonpage/fastdom) or a virtual DOM implementation). Read your metrics at the begininng of the frame (very very start of `rAF`, scroll handler, etc), when the numbers are still identical to the last time layout was done.
Timeline trace of The Guardian. Outbrain is forcing layout repeatedly, probably in a loop.