# Repaint Triggers

> How changing visual CSS properties forces the browser to re-draw pixels, and how to avoid expensive paint operations.

Subject: Internet | Lesson 34 of 35 | 6 min read
Interactive lesson: https://codewithanimation.in/internet/repaint-triggers
Previous: [Layout Thrashing](https://codewithanimation.in/internet/layout-thrashing.md) | Next: [Hardware Acceleration](https://codewithanimation.in/internet/hardware-acceleration.md)

## What it is

A Repaint occurs when changes are made to an element's appearance that do not affect its layout or geometry (such as its physical width, height, or position in the document flow).

During a repaint, the browser skips the Layout (Reflow) phase and goes directly to re-running the Paint phase to update the pixels on the screen for that specific element.

## Why it matters

While repaints are less computationally expensive than reflows, they are still costly.

Painting can be a highly intensive process, especially on mobile devices or when painting large areas. Frequent repaints block the main thread and can lead to dropped frames, jank, and battery drain.

## How it works

When you modify visual CSS properties that don't affect geometry-like `color`, `background-color`, `visibility`, or `box-shadow`-the browser invalidates the existing painted pixels for that element's bounding box.

The rendering engine then issues new draw calls to the graphics layer to redraw just the visual aspects of the element in its current position before compositing it to the screen.

## Common mistake: The Heavy Paint Trap

A common mistake is assuming all visual changes are cheap to animate.

For example, animating a pulsing `box-shadow` on a button forces the browser's paint engine to execute complex mathematical blur calculations on every frame. This continuous heavy repainting causes severe scroll jank on low-end devices.

## Tip: Pro tip

**The Transform and Opacity Rule.**

If you want silky-smooth, 60fps animations with absolutely zero repaints, stick exclusively to animating `transform` and `opacity`.

Modern browsers optimize these specific properties by handling them on the GPU during the Compositing phase, bypassing both Layout and Paint.

## Try it: Try it

Use the DevTools Paint Flashing simulator. Watch how the heavy box-shadow animation lights up the paint engine (green flashes), while the optimized transform animation is completely invisible to it.

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

## Check yourself

### Question 1

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

- A. width
- B. margin-top
- C. display: none
- D. color

Correct answer: D

Changing the text color only affects the visual appearance of the element, not its physical dimensions or position in the flow. Thus, it only triggers a Repaint.

- Why not A: Changing width alters the element's geometry, triggering a Reflow.
- Why not B: Changing margins shifts the element and surrounding elements, triggering a Reflow.
- Why not C: Hiding an element this way removes it from the document flow entirely, triggering a Reflow.

### Question 2

Why should you avoid animating box-shadow on a large element?

- A. It triggers a full page reflow, recalculating all element positions.
- B. It forces the browser to download new font files.
- C. It causes expensive repaints that can block the main thread and cause jank.
- D. It removes the element from the DOM temporarily.

Correct answer: C

Drawing shadows (especially with large blur radii) is mathematically complex. Animating it forces continuous, heavy repaints that lead to dropped frames.

- Why not A: box-shadow doesn't affect layout geometry.
- Why not B: Shadows are drawn by the rendering engine and are unrelated to networks.
- Why not D: box-shadow is purely visual and does not alter the DOM.

### Question 3

If your goal is to animate an element smoothly at 60fps with zero repaints, which properties should you animate?

- A. background-color and border-radius
- B. transform and opacity
- C. top and left
- D. visibility and outline

Correct answer: B

Modern browsers optimize transform and opacity animations by handling them in the Compositor thread using the GPU, entirely skipping both Layout and Paint.

- Why not A: Both of these trigger repaints on every frame.
- Why not C: Animating physical position properties triggers expensive Reflows and Repaints.
- Why not D: While they don't trigger reflows, they still trigger repaints.

## Remember this

- Repaints are cheaper than reflows, but still expensive.
- Animating box-shadow, background-color, or border-radius triggers continuous repaints.
- Animate transform and opacity for zero-paint performance.
