# The Paint Phase

> How the browser fills in the pixels for every node, converting geometry into colors and images.

Subject: Internet | Lesson 29 of 35 | 4 min read
Interactive lesson: https://codewithanimation.in/internet/paint-phase
Previous: [The Layout Phase (Reflow)](https://codewithanimation.in/internet/layout-phase.md) | Next: [The Compositing Phase](https://codewithanimation.in/internet/compositing-phase.md)

## What it is

The Paint Phase is where the browser finally decides what pixels to draw to the screen.

During Layout, the browser calculated the size and position of boxes. During Paint, it fills those boxes with colors, gradients, text, shadows, and images.

## Why it matters

Painting is the second most expensive operation after Layout. If you change a property like `color`, `background`, or `box-shadow` via JavaScript or CSS hover states, the browser has to re-paint the affected area.

While a Repaint is cheaper than a Layout Reflow (because it doesn't move other elements around), painting massive, complex layers-especially with large images or heavy drop-shadows-can still cause performance drops on low-end devices.

## How it works

The browser takes the geometric boxes from the Layout phase and creates a 'Paint Record'.

This record is a list of sequential drawing instructions. It draws the background color first, then the background image, then the borders, then the text, and finally the outlines.

The browser usually divides the page into 'tiles' and paints them sequentially, which is why on very slow connections or heavy pages you might see checkerboard patterns as scrolling forces new tiles to paint.

## Common mistake: The Box-Shadow Performance Killer

A very common beginner mistake is applying large, blurred `box-shadow` properties on elements that animate on hover, or using `filter: drop-shadow` on complex transparent PNGs.

Shadows require the browser to draw the element, blur it algorithmically, and draw it again underneath. If you animate the `box-shadow` property itself (e.g., `transition: box-shadow 0.3s`), the browser must recalculate that blur algorithm for every single frame of the animation.

This frequently causes noticeable lag on mobile devices.

## Tip: Pro tip

**How to animate shadows efficiently**: Instead of animating the `box-shadow` property, give the element a pseudo-element (`::after`) that contains the final hover shadow. Keep its `opacity` at 0. On hover, transition the pseudo-element's `opacity` to 1.

Because `opacity` skips the Paint phase entirely and runs on the Compositor (GPU), this technique achieves a 60fps shadow animation with zero painting overhead!

## Try it: Try it

Watch the 'Paint Record' execute sequentially. The browser doesn't draw everything at once; it builds up the pixels layer by layer.

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

## Check yourself

### Question 1

Which of the following CSS property changes will trigger a Repaint, but NOT a Layout Reflow?

- A. width
- B. background-color
- C. margin-top
- D. transform: translateX(10px)

Correct answer: B

Changing colors (background, text color) doesn't change the size or position of elements. Therefore, the browser skips Layout and goes straight to re-painting the pixels.

- Why not A: Width changes geometry, so it triggers Layout.
- Why not C: Margin pushes other elements, triggering Layout.
- Why not D: Transform skips both Layout and Paint! It goes straight to Compositing.

### Question 2

Why can animating a `box-shadow` cause lag?

- A. Because it triggers a global Layout Reflow.
- B. Because the browser must calculate a complex blur algorithm for every frame of the animation during the Paint phase.
- C. Because box-shadows download extra resources from the network.
- D. Because the DOM tree becomes too deep.

Correct answer: B

Blur algorithms are mathematically intensive. Forcing the CPU to repaint a blur 60 times a second can overwhelm mobile processors.

- Why not A: Shadows don't affect standard layout geometry.
- Why not C: Box shadows are calculated locally.
- Why not D: Box shadows don't add nodes to the DOM.

### Question 3

In what order does a browser's Paint Record generally draw an element?

- A. Text first, then borders, then background.
- B. Background first, then borders, then text.
- C. All simultaneously.
- D. Alphabetically by CSS property name.

Correct answer: B

Like a real painter, the browser works from back to front. It lays down the background color, draws the borders on top, and finally renders the text on the uppermost layer.

- Why not A: Text on the bottom would be covered up by the background.
- Why not C: Drawing is a sequential process.
- Why not D: Drawing relies on z-order stacking contexts, not the alphabet.

## Remember this

- Paint fills in the visual details (colors, images, text) inside the geometry calculated by Layout.
- Repainting is cheaper than Layout, but still expensive compared to Compositing.
- Complex shadows and large images increase paint time.
