# The Layout Phase (Reflow)

> How the browser calculates the exact size and position of every node in the Render Tree.

Subject: Internet | Lesson 28 of 35 | 5 min read
Interactive lesson: https://codewithanimation.in/internet/layout-phase
Previous: [Render Tree Compilation](https://codewithanimation.in/internet/render-tree-compilation.md) | Next: [The Paint Phase](https://codewithanimation.in/internet/paint-phase.md)

## What it is

The Layout Phase (also called Reflow) is the step where the browser calculates the precise geometry of the page.

The Render Tree tells the browser *what* to draw, but Layout determines exactly *where* to draw it and *how big* it should be.

## Why it matters

Layout is computationally expensive. If you change a layout-triggering property (like `width`, `height`, `margin`, or `left`) in an animation, the browser has to recalculate the geometry for the entire node tree 60 times a second.

This is a massive source of jank and poor performance in web apps. Understanding Layout allows you to animate properties that skip this phase entirely (like `transform` and `opacity`).

## How it works

The browser starts at the root of the Render Tree and traverses downwards.

It determines the size of the viewport and uses the Box Model to calculate the exact width, height, padding, margins, and border of every element in pixels.

Because elements can affect each other (e.g., a child expanding pushes its sibling down), changing the size of a single element high up in the DOM can cause a 'Layout Thrash'-forcing the browser to recalculate the geometry of hundreds of children and siblings.

## Common mistake: Layout Thrashing

A common performance killer in JavaScript is 'Layout Thrashing'. This happens when you write to the DOM, then read a layout property, then write again, over and over in a loop.

Normally, the browser batches DOM updates and performs one Layout calculation at the end of the frame. But if you ask for an element's `offsetHeight` immediately after changing a style, the browser is forced to calculate the Layout *synchronously* right then and there to give you the correct answer.

Doing this repeatedly in a loop destroys frame rates.

## Tip: Pro tip

**Never animate width/height or top/left.** Instead of animating `left: 100px`, animate `transform: translateX(100px)`. Instead of animating width/height for a scaling effect, use `transform: scale()`. Transform and Opacity skip the Layout and Paint phases entirely and run on the GPU.

## Try it: Try it

Observe how animating `width` causes the browser to constantly recalculate the surrounding layout, while animating `transform` leaves the layout alone.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/layout-phase

## Check yourself

### Question 1

Why is animating the `margin-left` property generally bad for performance?

- A. It triggers a recalculation of the Layout phase for every frame of the animation.
- B. It forces the browser to re-download the CSSOM.
- C. It cannot be hardware-accelerated by the CPU.
- D. It causes memory leaks in JavaScript.

Correct answer: A

Changing margin affects the layout (geometry) of the element and potentially its siblings, forcing the browser to run the expensive Layout phase 60 times a second.

- Why not B: CSSOM is already downloaded. Animation only triggers rendering pipeline updates.
- Why not C: It's not hardware-accelerated by the *GPU*, not CPU.
- Why not D: CSS animations don't cause JS memory leaks.

### Question 2

What is 'Layout Thrashing'?

- A. When a CSS animation moves too quickly to be seen.
- B. When JavaScript rapidly alternates between writing to the DOM and reading layout properties, forcing synchronous recalculations.
- C. When the DOM tree is too deeply nested for the browser to parse.
- D. When flexbox and grid are used on the same element.

Correct answer: B

Normally browsers batch layout updates. By reading a property like 'offsetHeight' right after setting a style, you force the browser to pause your JS, calculate the layout, return the value, and then resume. Doing this in a loop 'thrashes' performance.

- Why not A: That's just a fast animation.
- Why not C: Deep DOM trees slow down parsing and CSSOM matching, but thrashing is a specific JS/Render cycle issue.
- Why not D: You can't use both on the same element (display: flex vs grid), but doing so doesn't cause 'thrashing'.

### Question 3

Which CSS properties are safe to animate because they skip the Layout phase?

- A. width and height
- B. top, left, bottom, right
- C. transform and opacity
- D. color and background-color

Correct answer: C

Transform and opacity do not affect the geometry of the page. The browser can handle them purely in the Compositing phase, often offloading the work to the GPU for smooth 60fps animations.

- Why not A: These directly define geometry and trigger Layout.
- Why not B: These determine positioning and trigger Layout.
- Why not D: These trigger the Paint phase (which is still slower than Compositing).

## Remember this

- Layout determines size and position (geometry) in pixels.
- Layout is highly expensive and often cascades to siblings and children.
- Layout Thrashing occurs when JS forces synchronous layout calculations by reading geometry right after writing styles.
