Back to Portfolio
ArticleTechnologyMar 29, 2026

How Browsers Measure Text

Event
The Innovation Lab
Overview

An interactive explorable explanation of browser text measurement — from Canvas measureText() and Intl.Segmenter to greedy line-breaking, variable-width reflow, and shrinkwrap binary search. Build intuition for the prepare/layout two-phase architecture that powers modern text-heavy applications.

Technologies
Text LayoutCanvas APIIntl.SegmenterInteractiveBrowser APIsPerformance
How Browsers Measure Text preview
Step 1

The Character

When you call ctx.measureText('A'), what do you actually get back? Most developers only use .width. But the TextMetrics object returns much more: bounding boxes, ascent, descent, and font-level boundaries that tell you exactly where the ink lands.

72px
Try:

Notice how width and the actual bounding box rarely agree. A lowercase “g” descends below the baseline; an emoji may extend far beyond the font's reported width. When you need pixel-perfect layout, bounding boxes are what matter.

Step 2

The Word

Step 3

The Segmenter

Step 4

The Line

Now we have segments with measured widths. Line-breaking is just arithmetic: accumulate widths left-to-right until the next segment would overflow, then break. No DOM. No canvas. Just numbers.

Max Width320px
0
Lines
0px
Predicted Height
0.000ms
Layout Time
The layout function

Greedy line-breaking: pure arithmetic over pre-measured widths.

Step 5

The Resize

The prepare step is expensive — segmenting and measuring text takes real time. But it only runs once. The layout step is pure arithmetic over cached widths. How fast is “pure arithmetic”?

Container Width400px
Scale
1 measurement per update
Arithmetic Layout
The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. How vexingly quick daft zebras jump. The five boxing wizards jump quickly at dawn. Bright vixens jump; dozy fowl quack. Jackdaws love my big sphinx of quartz. The jay, pig, fox, zebra and my wolves quack. A wizard's job is to vex chumps quickly in fog. Watch Jeopardy, Alex Trebek's fun TV quiz game. By Jove, my quick study of lexicography won a prize.
0
Lines
0px
Height
<0.001ms
Time
DOM Measurement
The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs. How vexingly quick daft zebras jump. The five boxing wizards jump quickly at dawn. Bright vixens jump; dozy fowl quack. Jackdaws love my big sphinx of quartz. The jay, pig, fox, zebra and my wolves quack. A wizard's job is to vex chumps quickly in fog. Watch Jeopardy, Alex Trebek's fun TV quiz game. By Jove, my quick study of lexicography won a prize.
0
Lines
0px
Height
<0.001ms
Time
Predicted: 0pxActual: 0pxΔ 0px
Timing History

At scale 1, both approaches are near-instantaneous. Drag the scale to 1000 and the gap becomes dramatic: arithmetic layout stays flat while DOM measurement climbs. The prepare-once, layout-many architecture makes resize essentially free.

Step 6

The Obstacle

CSS shape-outside lets text flow around static shapes. But what if the shape moves? Each overlapping line queries the shape’s actual edges at that y-position — not a bounding box. Text fills both sides of the shape at 60fps.

0
Lines
0
Split Lines
0px
Height
Shape-aware line layout

Each line queries the hexagon's actual edges at that y-position. Near the tips, more text fits. In the middle, less.

Step 7

The Shrinkwrap

CSS fit-content sizes a container to its longest line — but that's rarely the tightest possible fit. What's the narrowest width that keeps text in exactly N lines? Because our layout function is pure arithmetic, we can binary search for the answer.

Text
Target Lines
Binary search for optimal width

Because layout is pure arithmetic, we can binary search over container widths in microseconds.

The Full Picture

Putting It All Together

Prepare~19ms once
Layout~0.002ms per frame
Applications

Where This Matters

Virtualized Chat

Slack, Discord, Teams — thousands of messages that need height prediction for smooth scrolling without rendering every message to the DOM.

Document Editors

Notion, Google Docs — paginate and reflow on resize without triggering layout thrashing. Pagination requires knowing where page breaks fall before rendering.

Masonry Layouts

Pinterest, image galleries — predict card heights for positioning without rendering offscreen. Eliminates the layout shift when scrolling fast.

Collaborative Tools

Real-time reflow as collaborators edit. Text measurement that runs in a Web Worker via OffscreenCanvas — no main thread blocking.

Inspiration

This explorable was inspired by Cheng Lou’s pretext library, which uses the same core ideas — Intl.Segmenter, Canvas measureText, and greedy line-breaking — to do text layout without touching the DOM. It also handles edge cases we glossed over: Japanese kinsoku rules, emoji width correction, RTL bidirectional text, and sub-pixel rounding differences across browsers.

We build interfaces where the engineering matches the problem. When your application needs text measurement that the browser can’t provide natively, we know how to build it.

Start a Project

Ready to transform your engineering?

Whether you need technical leadership, enterprise development, or team optimization—let's discuss how we can help.