Reading a site's spacing scale from computed styles, and the numbers that mislead
When you rebuild a site's look in Tailwind or a design system, colors and fonts are easy to read off the page. Spacing is harder. Nobody hands you "the scale"; you get hundreds of padding, margin and gap values, and you
When you rebuild a site's look in Tailwind or a design system, colors and fonts are easy to read off the page. Spacing is harder. Nobody hands you "the scale"; you get hundreds of padding, margin and gap values, and you have to guess which ones are tokens and which are one-offs.
So I wrote a console snippet that counts what the page actually renders, and ran it on three fictional demo sites I built: Northwind Roasters (a coffee shop), Ledgerline (fintech) and Afterhours (a dark developer tool). Here's the snippet, the results, and the three things that made my first version wrong.
The snippet
function spacingScale() {
// Horizontal margins are left out: `margin: 0 auto` and flex `margin-left: auto`
// compute to whatever space is left over, not to a spacing token.
const props = ['padding-top', 'padding-right', 'padding-bottom', 'padding-left',
'margin-top', 'margin-bottom', 'row-gap', 'column-gap'];
const counts = new Map();
for (const el of document.body.querySelectorAll('*')) {
const r = el.getBoundingClientRect();
if (!r.width || !r.height) continue; // hidden elements don't count
const cs = getComputedStyle(el);
const values = new Set(props.map((p) => Math.round(parseFloat(cs.getPropertyValue(p)))));
for (const v of values) if (v > 0) counts.set(v, (counts.get(v) || 0) + 1); // one vote per value per element
}
const rows = [...counts].sort((a, b) => a[0] - b[0]);
const votes = rows.reduce((n, [, c]) => n + c, 0);
const onGrid = (k) => Math.round(100 * rows.filter(([v]) => v % k === 0).reduce((n, [, c]) => n + c, 0) / votes);
console.table(Object.fromEntries(rows.map(([v, c]) => [`${v}px`, c])));
return { votes, onGrid4: `${onGrid(4)}%`, onGrid8: `${onGrid(8)}%` };
}
spacingScale();
Paste it into the DevTools console on any page. It prints a table of values with how many elements use each, and the share of those uses that sit on a 4px or 8px grid.
What it found
| Site | Values (px × elements) | On a 4px grid | On an 8px grid |
|---|---|---|---|
| Northwind | 2×1, 4×4, 6×4, 10×1, 11×4, 12×7, 16×8, 18×5, 20×5, 24×5, 28×1, 32×2, 48×1, 56×5, 72×2 | 73% | 42% |
| Ledgerline | 2×3, 4×3, 8×6, 9×1, 10×5, 12×1, 14×7, 16×2, 18×5, 20×1, 22×7, 28×2, 56×5, 72×1, 80×1 | 44% | 30% |
| Afterhours | 4×3, 8×3, 10×1, 11×3, 16×2, 18×3, 20×4, 22×2, 24×3, 28×2, 40×1, 56×4, 64×1, 72×1, 88×1 | 74% | 47% |
The results were the same at 1280px and 390px wide, because these demo pages don't change their padding at that width. A real responsive site will give you two different tables, and both are part of its scale.
All three use the same section padding, 56px, and none of them is a clean 8px system. Northwind and Afterhours are mostly 4px-based with a few odd values (6, 10, 11, 18). Ledgerline has no grid at all: its most common values are 14px and 22px.
Three things that skewed the first version
1. Auto margins look like huge tokens. My first run counted all four margins. Northwind came back with a 577px value, Ledgerline 542px, Afterhours 680px. Those are margin-left: auto in the nav and margin: 0 auto on centered containers: getComputedStyle reports the leftover space in pixels, not the word auto. At 390px wide those values disappeared, which is the giveaway. Leaving horizontal margins out removes them. You lose real horizontal margins too, so check those by hand if a layout depends on them.
2. Repeated components outvote everything. Counting every side of every element, Ledgerline's 22px came out at 26 uses, more than twice the next value. The CSS uses 22px in only three rules (two paddings and one gap); the cards with that padding just repeat on the page, and each card votes four times, once per side. Counting each value once per element brought it down to 7, level with 14px. Neither count is "right", but per-element votes are closer to how often a designer chose that value.
3. Some sites have no grid, and that's a finding. It's tempting to snap everything to 4px or 8px. Ledgerline is 44% on a 4px grid, so snapping would move more than half its spacing. If you're matching that site, keep the odd values. In Tailwind v4 that means p-[22px] where you need the exact value, or deciding on purpose to round to p-5 (20px) or p-6 (24px) and accepting the change.
Turning the table into tokens
What I do with the output:
- Drop values used once unless they're structural (section padding, page gutters).
- Keep the rest as the scale, in order. For Northwind that's 4, 6, 11, 12, 16, 18, 20, 24, 32, 56, 72.
- Check the grid share. Above roughly 70% on 4px, round the stragglers and note it. Below that, keep the exact values.
- Compare the 1280px and 390px tables to see which values are swapped on mobile.
The snippet only sees the page you're on. Run it on two or three page types (home, a content page, a form) before calling it the site's scale.
I made a Chrome extension, Stylasso, that measures spacing this way along with colors, fonts and radii, and writes it out as CSS variables or a Tailwind v4 theme; there's a guide here: turn any website into a Tailwind v4 theme. The snippet above is enough if spacing is all you need.
Written with help from an AI assistant. Every number comes from running the snippet on the three demo pages in headless Chromium.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.