A narrow technical article starts moving sideways, so you add overflow-x: auto to its code block. In a flex row with a sidebar and a flex-item article, it might not help. The article can grow to the length of the code line, leaving nothing to overflow inside the code box itself.
At 320 CSS px, the page had an 891px scroll width with code-only overflow. Adding min-inline-size: 0 to the article brought the page back to 320px and let the code scroll independently. Hiding the overflow kept the page just as narrow but made the end of the line unreachable with the keyboard in the follow-up test.
01 · Why does the page move when the code is set to scroll?
The flex row has a 24px rail, 12px padding on each side, and a 12px gap. At 320px, that leaves 320 − 24 − 12 − 24 = 260px for the article. The article contains a short paragraph and an unbroken 100-character ASCII line set in 14px monospace.
A uses defaults. B adds overflow-x: auto to the code block. C keeps B and gives the article flex item min-inline-size: 0. In B, the code box ended up as wide as its contents and the browser moved the page, not the code. Setting flex: 1 does not in itself remove the item's minimum. See Flexbox's automatic minimum size.
02 · Measure the page and the code as separate boxes
We compared clientWidth and scrollWidth in isolated 320px and 360px frames. Then we requested scrollLeft = 120 on the code and the document separately and read the resulting positions. Values are CSS px.
| Width | Case | Page client → scroll | Code client → scroll | Page scrollLeft | Code scrollLeft |
|---|---|---|---|---|---|
| 360 | A/B | 360 → 891 | 843 → 843 | 120 | 0 |
| 360 | C | 360 → 360 | 300 → 843 | 0 | 120 |
| 320 | A/B | 320 → 891 | 843 → 843 | 120 | 0 |
| 320 | C | 320 → 320 | 260 → 843 | 0 | 120 |
Figure 1. At 320px, code-only auto leaves an 891px page scroll width. Letting the article shrink gives a 320px page and a 260px visible code box. Bars show page scrollWidth and code clientWidth.
In C, the code contents are still 843px wide. What changed was the size of the box holding the line, not the line itself. Prose remains readable without horizontal scrolling, and the code can move independently.
03 · Counterexample: a fitting page can still clip the code
The keyboard follow-up used a separate 320px fixture with TAIL_MARKER appended to the 100 characters. D changed just the code block's overflow-x, from auto to hidden. Both code boxes received focus through Tab. We then pressed Right Arrow 24 times without assigning a scroll position in script.
| Code setting | Tab focus | Code offset before → after | Page offset | Entire end marker |
|---|---|---|---|---|
| C · auto | pre | 0 → 676px | 0px | Visible |
| D · hidden | pre | 0 → 0px | 0px | Not visible |
Figure 2. Both variants fit the page and accept focus. Right Arrow reveals the entire end marker with auto but leaves it clipped with hidden.
Script-driven movement and keyboard access are different checks. A box that can be moved in script is not necessarily readable using arrow keys. CSS Overflow's value definitions distinguish clipping from user scrolling.
04 · Apply the fix in this order
- Check
document.scrollingElementat a narrow width. If the page overflows, inspect the actual child of the nearest horizontal flex row and find the minimum size preventing it from shrinking. In a nested layout, you may have to check more than one flex item. - Let the article shrink and put scrolling on the code box that must preserve its lines. If the article already fits in normal flow without a flex minimum getting in the way, don't add extra ancestor rules. For a log where line structure doesn't matter, try wrapping first.
- Check that the page fits, then use the keyboard to reach the end and Tab out. Keep the focus indicator visible and test touch, keyboard, and zoom in the browsers you support. If a publishing editor strips the CSS, don't hide page overflow; separate the long material into a copyable example instead.
Reproduction and measurement
Put this HTML in a standards-mode document (<!doctype html>) and apply the CSS. Follow the comments to compare A/B/C. For the layout pass, request and reset horizontal offsets on the code and the document separately. For the keyboard follow-up, place a button before the code and append <span id="tail">TAIL_MARKER</span> after the digits. Focus the first button, Tab once into the code, then press Right Arrow 24 times. Read the offsets after input without assigning them from script.
<div class="row"> <div class="rail" aria-hidden="true"></div> <article class="article"> <p>Short prose should stay readable.</p> <pre tabindex="0"><code></code></pre> <button type="button">After code</button> </article> </div> <script> document.querySelector("code").textContent = "0123456789".repeat(10); </script> *, *::before, *::after { box-sizing: border-box; } body { margin: 0; font: 16px/1.5 sans-serif; } .row { display: flex; width: 100%; padding: 12px; gap: 12px; } .rail { flex: 0 0 24px; background: #e8edf3; } .article { flex: 1 1 0%; } p, pre { margin: 0 0 8px; } pre { font: 14px/1.5 monospace; background: #e8edf3; } /* B: add the following declaration; A leaves it out. */ pre { overflow-x: auto; } /* C: keep B and add this rule to the flex item. */ .article { min-inline-size: 0; } pre:focus-visible { outline: 2px solid #185abc; outline-offset: 2px; } const page = document.scrollingElement; const code = document.querySelector("pre"); const read = () => ({ page: [page.clientWidth, page.scrollWidth, page.scrollLeft], code: [code.clientWidth, code.scrollWidth, code.scrollLeft], focused: document.activeElement === code }); console.table(read()); Limits. Both isolated-frame experiments ran on November 22 in cloud Chrome; the exact build was not recorded. Text and fonts affect the measurements. We did not test mobile hardware, Safari, Firefox, a screen reader, or a live publishing theme. W3C's Reflow explanation uses a width equivalent to 320 CSS px, but this fixture does not establish accessibility conformance.
Takeaway: A fitted page and readable code are separate goals. Fix the article's sizing first, keep the scroll inside the code box, and check that the end is reachable with the keyboard.
AI generated this article and its illustrations. The evidence and reproduction were checked.


