scripted steps · no trace
01. How the browser works / Lesson 4 of 4 · intermediate · 10 min · 11 steps

Reflow vs repaint

Why reading offsetHeight in a loop froze a grid of cards.

1-min Short
DOMfrom the HTMLhtmlheadlinkscriptbodyh1pRENDER TREEwhat will be drawnbodyh1pCSSOMfrom style.cssbodyh1HTMLtokens, in orderPARSERdoneSCREENpaintedSTYLErunsLAYOUTrunsPAINTrunsCOMPOSITEruns0LAYOUTSSCRIPTwriteread

An agent made a grid of 300 order cards all the same height. On a mid-range phone, the page freezes for a moment every time it resizes.

IN THIS LESSONStep 1 of 11 · Symptom0:00 / 2:10

Transcript

follows the lesson · 0 of 11 steps

Reflow vs repaint: the whole lesson in words

A grid of 300 cards freezes for a moment every time the window resizes, on a mid-range phone.

The one-page cheatsheet for this lesson →

Sources

orders.html · agent-writtenhtml
<!doctype html>
<html><head><meta charset="utf-8"><title>Orders</title>
<style>
.grid { display: flex; flex-wrap: wrap; gap: 12px; padding: 12px; }
.card { width: 180px; padding: 12px; border: 1px solid #ccc; }
</style></head><body>
<div class="grid"><!-- 300 order cards --></div>
<script>
// Make every card as tall as the tallest one.
const cards = [...document.querySelectorAll('.card')];
function equalise() {
let max = 0;
for (const card of cards) {
card.style.height = 'auto';
max = Math.max(max, card.offsetHeight);
}
for (const card of cards) card.style.height = max + 'px';
}
equalise();
window.addEventListener('resize', equalise);
</script>
</body></html>
CHEATSHEET

Opens after you lock in your predictions. It states the answers.