Layout debugger overlay
The canvas draws an article, and over it the boxes that layout gave each element: margin in orange, padding in green, the baseline of each line of text in pink. Tap a part of the article to see that element alone, with its size. Drag "Width" to make the article lay out again. "Turn the picture" draws the article at an angle, and the overlay turns with it.
Show the code measure at 0, 0 with no ink, then draw the element and its boxes
- The markup. One drawable child. Nothing is added to it for the overlay.
- A measuring draw. The element is drawn at 0, 0 with
globalAlpha = 0so that client rectangles are layout positions. - The boxes and the baselines.
- One frame. Measure, draw the article with its transform, draw the overlay with the same transform.
- Drawing the boxes.
- Picking an element. A tap reaches the element itself, because hit testing follows the draw.
How it works
- Each frame has three steps. The article is drawn at 0, 0 with no transform and
globalAlpha = 0. Nothing appears, and for that momentgetBoundingClientRect()of every descendant is its layout position plus the canvas position. The page reads all of them. Then the article is drawn where it belongs, which also puts hit testing there. - Margin, border and padding widths come from
getComputedStyle(). The margin is drawn as the ring between the border box and the border box grown by the margins. - A baseline is found without adding anything to the document. A
Rangeover a text node returns one rectangle per line. The rectangle is the inline box, as tall as the font's ascent plus descent. The ascent comes frommeasureText().fontBoundingBoxAscentfor the same font. - The overlay is drawn in the article's own coordinates under the same canvas transform as the article, so a rotation or scale needs no extra arithmetic.
- A tap is a
clickon the element under the finger. No coordinates are converted: the browser hit tests the drawn article. - The canvas redraws on the
paintevent, so typing in the last paragraph or dragging "Width" measures everything again.
Live checks
Each check builds a small canvas of its own in a corner of the window. The picture above isn't touched.
| Result | Experiment | Detail |
|---|
Findings
From Chromium 157, headless, on 2026-11-21. The rows above say what your browser does.
- Works: a measuring draw with no ink. A draw with
globalAlpha = 0places the element, so layout rectangles can be read at 0, 0 and the element drawn elsewhere in the same frame. Hit testing ends at the last draw. - Limit: no call returns layout rectangles of a turned element. After a draw turned by 0.3 rad,
getBoundingClientRect()andRange.getClientRects()return bounding boxes in the canvas.offsetLeftandoffsetTopgive layout positions of elements as whole numbers, and nothing gives them for a line of text.getBoxQuads()isn't in Chromium. - Works: baselines from the font's ascent. They match a zero-size inline-block probe to within 0.5 px for the three fonts checked. A line box that mixes sizes shares one baseline, which the pink line shows under "6" and its neighbours.
- Seen in the overlay: collapsed margins overlap. Two paragraphs with a 20 px margin each are 20 px apart, and the two orange rings lie on top of each other. Computed style reports the margin an element asked for, not the space it got.
- Seen in the overlay: a flex gap and
border-spacingaren't margins. The space between the two buttons and between table cells has no colour. - Not covered: baselines of form controls and images, CSS transforms on descendants, grid line names and flex free space.
- Not verified here: a phone. The overlay should be checked at a device pixel ratio of 2.625, where layout positions are fractions of a pixel, and with a system font size that isn't the default.