Loading...
Loading...
By continuing to use the platform, you accept the terms of the Privacy Policy and the use of cookies.
Reflow and Repaint are two processes that occur in browser when DOM or CSS changes. These processes affect performance, so it's important to understand when and how they occur to minimize them.
Reflow occurs when element sizes or positions on page change. This process forces browser to recalculate not only changed element's position, but also positions of all other elements that may be affected by changes (e.g., due to container size change).
When Reflow occurs, browser recalculates coordinates and sizes of all elements, which can be expensive, especially on complex pages.
Repaint occurs when element styles that don't affect its position on page change (e.g., background color, font, border, etc.). Unlike Reflow, Repaint doesn't require recalculating other elements' positions, only updates visual representation of specific element.
Reflow occurs in following cases:
Element size change:
Document structure change:
Element position change:
absolute or relative positioning.Font or font size change:
font-size, font-family, line-height.Element visibility or display change:
none to block or vice versa).Animations and transformations:
Repaint occurs in following cases:
Color or style change:
Opacity change:
Background addition and change:
Fonts and text styles:
Border style change:
Reflow and Repaint can slow down page rendering, especially if they occur frequently or for large elements. Here are some tips to minimize them:
Minimize DOM changes:
Use requestAnimationFrame:
requestAnimationFrame to synchronize visual changes with screen repaint to avoid unnecessary repaints.Use CSS transformations instead of size changes:
transform and opacity for animation instead of changing sizes or position to avoid Reflow.transform: translate() instead of changing top and left.Use document fragments:
Hide elements before changing their sizes:
Using will-change:
will-change property to indicate to browser in advance which elements will change. This helps browser optimize rendering.Recommendation:
To improve web page performance, avoid frequent DOM changes, minimize element size changes and use CSS animations to reduce CPU load and speed up rendering.