How to measure spacing on any website
A practical guide to pixel-perfect design QA: what to measure, how to read the box model, the gaps DevTools will not show you, and a checklist for comparing a build with its design.
By Swapnil Gupta · · 7 min read
The design says 24 pixels between the card and the button. The build looks close. Close is where most visual bugs live: a gap that is 20 here and 28 there, a heading that sits a little low, a list whose last item has a margin the others do not. Nobody files a bug for any one of them, and together they are why a page feels slightly off.
This guide is about catching them: what to measure, how to read what the browser tells you, and the gaps the usual tools leave out.
Why spacing drifts between design and build#
A design tool places boxes at fixed positions. A browser works spacing out from rules, and several of those rules add or remove space no one wrote down:
- Margins collapse. Two vertical margins of 24 and 16 between siblings make a gap of 24, not 40. Adding up the CSS gives the wrong answer; only measuring the rendered page gives the right one.
- Line height adds space you cannot see. A 16px line of text in a 24px line box has empty space above and below its letters, and more above the small letters than a designer would draw.
- Components bring their own spacing. A button with a default margin, or a paragraph's margin inside a card, adds to the gap the layout already sets.
- Fonts change size. Until the web font loads, the fallback font has different metrics, and text-driven heights move.
- Widths change everything. A layout that is right at 1440 pixels can be wrong at 1280, where a column wraps.
Read the box model first#
Every element is four boxes inside each other. The content holds text or children; padding is space inside the element's background; the border wraps both; and the margin is space outside, which the element's background never covers.
One property changes how widths add up. With box-sizing: border-box, which most sites set for every element, a width of 354 includes the padding and the border, so the content is narrower than the width you set. With the default, content-box, the padding and border are added on top. When a measured width and the CSS disagree, this is the first thing to check.
Measuring with DevTools#
Every browser's developer tools show one element's box model:
- Right-click the element and choose Inspect.
- Hover its line in the Elements panel. The page shows its content, padding and margin in colour, and a label with its size.
- Open the Computed tab. The box model diagram there lists the margin, border and padding on each side, in pixels.
That answers how big an element is. It does not answer the question design QA asks most: how far apart are these two things? DevTools has no tool for it. You can work it out in the Console from each element's position: select the first element in the Elements panel, then run
a = $0.getBoundingClientRect()
// select the second element, then:
b = $0.getBoundingClientRect()
b.top - a.bottom // the vertical gap, in CSS pixelsIt works, and it is slow: four steps per gap, and a page has hundreds of gaps. It also measures the boxes, not what you see, which matters for text (below).
Measuring between elements#
What you want is a ruler: select one element, point at another, and read the distance. That is what a measuring extension adds to the browser. In Nitpique, click an element to select it, then hover any other: lines show the gap between them, the inset when one sits inside the other, or how much they overlap, with the value on each line.
Three habits make measuring faster:
- Measure what renders, not what the CSS says. Collapsed margins, inherited padding and line height all show up in the rendered boxes, so you never need to add them up.
- Check every pair in a list, not only the first. The last item is where a stray margin hides.
- Use guides for alignment. Drag a guide from the ruler to the left edge of the content, and anything that does not touch it is out of line.
Measuring to text, not its box#
Designers place text by its letters. Browsers place it by its line box, which is as tall as the line height. Measure from a heading's box and the number includes space above the letters that nobody can see, so a gap that matches the design file can still look too big.
To the box (DevTools)
Measured: 16
To the letters (what you see)
Measured: 38
To compare with a design, measure to the glyphs. In Nitpique, press T for text edges: distances to text are then taken from the letters themselves (their cap height, x-height and baseline), not from the box.
text-box property trims a text box to its letters in browsers that support it, so a 24px gap in CSS becomes a 24px gap you can see. Check its support on MDN before you rely on it.A spacing QA checklist#
Run through it at each width the design has a frame for:
- Set the browser to the design frame's width, at 100% zoom. A responsive canvas shows several widths at once.
- Lay the design over the page with a design overlay to see where it differs, then measure each difference.
- Measure the space between sections first: it is the largest and the most visible.
- Measure every gap in a list or grid, the last item included.
- Check the padding inside buttons, inputs and cards on all four sides.
- Check left edges line up with the grid or with each other, using a guide.
- Measure text to its letters, not to its line box.
- Check touch targets are at least 24 × 24 CSS pixels (WCAG 2.2, 2.5.8).
- Hover and focus each control: a state should never move the layout.
- Reload with a slow connection: the page should not jump when the fonts and images arrive.