Understanding Repaint vs Reflow

Optimizing browser rendering for smoother web experiences.

Introduction

When you interact with a website, your browser is constantly working behind the scenes to render the page. This involves several steps, two of the most critical being Reflow (also known as Layout) and Repaint (also known as Paint). Understanding these processes is key to building high-performance web applications.

In simple terms:

Reflow (Layout)

A Reflow occurs when the browser needs to recalculate the layout of a part of the document (or the whole document). This happens when changes are made to the DOM (Document Object Model) or CSS that affect the geometry of elements. Because elements often affect each other's positions, a single reflow can trigger a cascade of recalculations across the entire page, making it a very expensive operation.

Common Reflow Triggers:

Interactive Reflow Demo

Observe how changes to layout properties cause all elements to shift and trigger a "Reflow". The entire container will flash red.

Box 1
Box 2
Box 3

Reflow Count: 0

Repaint (Paint)

A Repaint occurs when changes are made to an element's visual properties that do not affect its layout. The browser simply redraws the pixels for that element on the screen. Repaints are less expensive than reflows because they don't require recalculating the geometry of other elements.

Common Repaint Triggers:

Interactive Repaint Demo

Observe how changes to visual properties cause only the affected element to redraw and trigger a "Repaint". Only the changed box will flash green.

Box A
Box B
Box C

Repaint Count: 0

The Relationship: Reflow Always Causes Repaint

It's important to understand that a Reflow will always trigger a Repaint. If the browser has to recalculate the layout of elements, it then needs to redraw those elements in their new positions. However, a repaint does not necessarily cause a reflow. This is why minimizing reflows is generally a higher priority for performance optimization.

Optimizing for Performance

Reducing unnecessary reflows and repaints is crucial for smooth animations and a responsive user interface. Here are some strategies:

Browser Rendering Pipeline

This flowchart illustrates the typical steps a browser takes to render a web page.

graph TD A[HTML] --> B(DOM Tree) C[CSS] --> D(CSSOM Tree) B & D --> E(Render Tree) E --> F(Layout / Reflow) F --> G(Paint / Repaint) G --> H(Compositing) H --> I(Display on Screen) style A fill:#e0f2fe,stroke:#7dd3fc,stroke-width:2px,color:#000 style C fill:#e0f2fe,stroke:#7dd3fc,stroke-width:2px,color:#000 style B fill:#f0f9ff,stroke:#cbd5e1,color:#475569 style D fill:#f0f9ff,stroke:#cbd5e1,color:#475569 style E fill:#f0f9ff,stroke:#cbd5e1,color:#475569 style F fill:#7dd3fc,stroke:#0ea5e9,stroke-width:2px,color:#000 style G fill:#a78bfa,stroke:#8b5cf6,stroke-width:2px,color:#000 style H fill:#f0f9ff,stroke:#cbd5e1,color:#475569 style I fill:#22c55e,stroke:#16a34a,stroke-width:2px,color:#000