When Reflow and Repaint Occur in Browser
What are Reflow and Repaint?
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 (also known as "layout")
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 (also known as "paint")
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.
When Do Reflow and Repaint Occur?
When Reflow Occurs:
Reflow occurs in following cases:
-
Element size change:
- Changing width, height, margin, padding and other properties.
- Example: changing block width or increasing content height.
-
Document structure change:
- Adding, removing or changing elements in DOM.
- Example: inserting new element or removing existing element.
-
Element position change:
- Changing position, top, left, right, bottom, z-index properties.
- Example: changing position of element with
absoluteorrelativepositioning.
-
Font or font size change:
- Changing font styles, e.g.,
font-size,font-family,line-height. - Example: changing text size in block.
- Changing font styles, e.g.,
-
Element visibility or display change:
- Changing display (e.g., from
nonetoblockor vice versa). - Example: hiding or showing elements.
- Changing display (e.g., from
-
Animations and transformations:
- Although animations such as transform don't always cause Reflow, if animation changes layout, it can cause Reflow.
When Repaint Occurs:
Repaint occurs in following cases:
-
Color or style change:
- Changing color, background-color, border, box-shadow and other styles that don't affect size or position.
- Example: changing element background color or text.
-
Opacity change:
- Changing opacity doesn't affect layout, but causes Repaint to update display.
- Example: decreasing element opacity.
-
Background addition and change:
- Changing background-image, background-position, background-size and other background-related properties.
- Example: changing background image or its position.
-
Fonts and text styles:
- Changing font-family, font-size, line-height, letter-spacing, if it doesn't affect positioning.
- Example: changing letter spacing (letter-spacing).
-
Border style change:
- Changing element border color or width, if it doesn't affect its sizes.
- Example: changing block border color or thickness.
How to Minimize Reflow and Repaint?
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:
- Avoid frequent DOM structure changes, as this always causes Reflow.
- Try to group DOM changes in one cycle, rather than calling them one by one.
-
Use
requestAnimationFrame:- Use
requestAnimationFrameto synchronize visual changes with screen repaint to avoid unnecessary repaints.
- Use
-
Use CSS transformations instead of size changes:
- If possible, use
transformandopacityfor animation instead of changing sizes or position to avoid Reflow. - Example: using
transform: translate()instead of changing top and left.
- If possible, use
-
Use document fragments:
- When adding multiple elements to DOM, use Document Fragment to prevent multiple Reflow calls.
-
Hide elements before changing their sizes:
- If you want to change element size or position, hide them first, change parameters, then show back. This prevents unnecessary Reflow during process.
-
Using
will-change:- Use
will-changeproperty to indicate to browser in advance which elements will change. This helps browser optimize rendering.
- Use
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.