Two layouts that were wrong by 880 and 609 pixels

A grid or flex child defaults to min-width:auto, so it refuses to shrink below its content — a 900px container can end up with a 1509px column and 270px of page overflow. The other classic is content that was never put in a container at all: an article rendered straight into <main> sat at x=0 with an 880px void to its right. Both are found by reading four numbers instead of looking at the page: bounding box left and width, computed max-width, and grid-template-columns.

Two bugs, one afternoon, both invisible in a screenshot and obvious in a number.

Case 1: content that was never in a container

A page rendered its article directly into <main> with no wrapper. It looked “left-aligned” and slightly airy. Measured at a 1600px viewport:

const a = document.querySelector('article').getBoundingClientRect();
// { left: 0, width: 720, right: 720 }
window.innerWidth;   // 1600
1600 - 720;          // 880px of nothing on the right

The rule that should have centred it lived on .wrap — a class the template never applied. max-width: 720px was working perfectly; there was simply no container to centre it inside.

After adding the wrapper:

wrap    left 193  width 1200   (1200 max, centred in 1600)
article left 225  width 720

Same CSS. The only change was putting the element inside the element that the stylesheet expected.

That is the general shape of layout bugs: the rule is fine, the structure is wrong. Which is why reading getComputedStyle on the element usually shows a perfectly reasonable max-width while the page looks broken.

Case 2: the 1fr that refuses to shrink

A two-column grid, 1fr 200px, inside a 900px container. One cell contains a long unbreakable URL. Measured:

getComputedStyle(wrap).gridTemplateColumns;   // "1509.08px 200px"
document.documentElement.scrollWidth - document.documentElement.clientWidth;  // 270

The track that was supposed to be 1fr resolved to 1509px inside a 900px container, and the page overflowed by 270px.

The cause is one default: grid and flex items have min-width: auto, which means “never narrower than my content”. 1fr is a maximum, not a promise. Add the guard:

.grid > * { min-width: 0; }

Measured again on the same page:

without guard: cell width 1509   page overflow 270
with guard:    cell width  676   page overflow   0   (content scrolls inside: 1500)

min-width: 0 lets the track shrink; overflow-wrap: anywhere (or overflow-x: auto) decides what the too-long token does once the box is the right size.

The four numbers

Instead of squinting, read these in order:

# What to read What it tells you
1 rect.left, rect.width, innerWidth - rect.right Is there a void, and on which side
2 getComputedStyle(el).maxWidth Which rule actually applies (and whether something else is winning)
3 getComputedStyle(parent).gridTemplateColumns The resolved track sizes — where 1fr became 1509px
4 documentElement.scrollWidth - clientWidth Whether the page is wider than the viewport, in pixels

Number 3 is the one people skip. grid-template-columns from the stylesheet says 1fr 200px; the computed value says 1509.08px 200px, and that is the number that explains the overflow.

How you know it worked

  • scrollWidth === clientWidth at every width you care about. One line, works headless, catches every horizontal overflow including the ones you did not think to look for. Test at least 390, 768 and 1440.
  • The tracks add up. sum(columns) + gap should equal the container’s content width. When it does not, one track grew past its share.
  • The void is bounded. For a centred layout, rect.left and innerWidth - rect.right should be (nearly) equal. 193 and 207 is a centred page; 0 and 880 is a missing container.
  • Measure before and after, with numbers in the commit message. “Fixed layout” is unreviewable; “article left 0 → 225, right gap 880 → 655” is a diff someone can check.

How to undo it

  • CSS revert: keep the measurement script, not just the old file. Re-running four numbers tells you whether the revert restored the old geometry — a screenshot cannot.
  • min-width: 0 on a broad selector: it can make previously-stable columns collapse. Scope it to the grid’s direct children, then re-measure all three viewports.
  • overflow: hidden as a quick fix: it hides the symptom and clips focus rings and dropdowns. If you use it to buy time, leave a note and a follow-up, because the guard you actually wanted was min-width: 0.

Questions people actually ask

Why does 1fr overflow instead of shrinking?

Because the default for a grid or flex item is min-width: auto, and auto means "never smaller than my content". 1fr is a maximum, not a promise: the track satisfies the content first and the container pays with horizontal overflow.

Is min-width: 0 the fix, or is overflow: hidden?

They do different things. min-width: 0 lets the track shrink and keeps the content selectable; overflow: hidden clips it and hides the problem. Use min-width: 0 for layout, and add overflow-wrap: anywhere if the content is an unbreakable token like a URL.

How do you find a layout bug without eyeballing it?

Read the numbers: getBoundingClientRect() gives left and width, computed max-width tells you which rule is fighting, and grid-template-columns shows the resolved track sizes. A 1509px track inside a 900px container is impossible to miss once it is a number.

What single assertion catches most of these?

document.documentElement.scrollWidth === clientWidth. Any horizontal overflow is a layout bug, at every viewport width you care about — and it is one line in a headless check.

Sources


Last checked: 2026-11-18 · Both cases were measured in a real browser on 2026-11-18: the 880px void on a page of this site (before the fix), and the 1509px track on a two-column test page. All numbers on this page come from getBoundingClientRect() and getComputedStyle().