What forces layout/reflow. The comprehensive list.
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, and is common performance bottleneck.
elem.offsetLeft
, elem.offsetTop
, elem.offsetWidth
, elem.offsetHeight
, elem.offsetParent
elem.clientLeft
, elem.clientTop
, elem.clientWidth
, elem.clientHeight
elem.getClientRects()
, elem.getBoundingClientRect()
elem.scrollBy()
, elem.scrollTo()
elem.scrollIntoView()
, elem.scrollIntoViewIfNeeded()
elem.scrollWidth
, elem.scrollHeight
elem.scrollLeft
, elem.scrollTop
also, setting themelem.focus()
can trigger a double forced layout (source)elem.computedRole
, elem.computedName
elem.innerText
(source)window.getComputedStyle()
will typically force style recalc (source)
window.getComputedStyle()
will force layout, as well, if any of the following is true:
min-width
, min-height
, max-width
, max-height
, width
, height
aspect-ratio
, min-aspect-ratio
, max-aspect-ratio
device-pixel-ratio
, resolution
, orientation
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
webkit-filter
, backdrop-filter
motion-path
, motion-offset
, motion-rotation
x
, y
, rx
, ry
window.scrollX
, window.scrollY
window.innerHeight
, window.innerWidth
window.getMatchedCSSRules()
only forces styleinputElem.focus()
inputElem.select()
, textareaElem.select()
(source)mouseEvt.layerX
, mouseEvt.layerY
, mouseEvt.offsetX
, mouseEvt.offsetY
(source)doc.scrollingElement
only forces stylerange.getClientRects()
, range.getBoundingClientRect()
More on forced layout
section below covers everything in more detail, but the short version is:
for
loops that force layout & change the DOM are the worst, avoid them.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.
updateLayoutIgnorePendingStylesheets
- GitHub search - WebKit/WebKit FrameNeedsReflow
- mozilla-central searchupdateLayoutIgnorePendingStylesheets
- Chromium Code SearchupdateLayoutTree
- Chromium Code SearchCSS Triggers is a related resource and all about what operations are required to happen in the browser lifecycle as a result of setting/changing a given CSS value. It's a great resource. The above list, however, are all about what forces the purple/green/darkgreen circles synchronously from JavaScript.