Keep Code Blocks Scrollable Without Widening the Page
A preformatted command preserves one long line and lets its overflow escape into the document instead of remaining inside the code region.
overflow-x: visible- Viewport / page
- --
- Code block
- --
- Page overflow
- --
overflow-x: auto- Viewport / page
- --
- Code block
- --
- Page overflow
- --
Three-browser evidence verified. Live values come from your current browser. The formal matrix records the controlled browser runs and fixture revision used for the published conclusion.
Observed failure
The preserved line overflows the article boundary
The `pre` element preserves the complete command on one line. Without a local overflow policy, the command extends beyond the code box and contributes to the page scrollable area.
Minimal code diff
Keep horizontal scrolling inside the code block
Constrain the code block to its article and use `overflow-x: auto` so the exact line remains reachable without widening the document.
Before
.article pre {
overflow-x: visible;
}After
.article pre {
max-inline-size: 100%;
overflow-x: auto;
}Root cause
The default white-space behavior of pre preserves the authored line. That is useful when line breaks and spacing carry technical meaning, but a long line can overflow the element. If the element does not establish its own scrollable overflow area, the document becomes the effective horizontal scroll container.
The repair keeps white-space: pre. It changes where overflow is handled, not the command itself. The code region is keyboard focusable and retains the entire line.
Verify the local scrolling contract
Measure both boundaries after the repair: the document should have no extra scroll width, while the code element should report scrollWidth greater than clientWidth for the deliberately long command. Then reach the region using only the keyboard, move the local horizontal scroll position, and confirm that its focus indicator remains visible. A mouse-only scrollbar is not enough for a technical example whose complete line is part of the content.
Also test copy and selection from the start through the end of the command. Truncation, fading overlays, or overflow: hidden can make the page appear responsive while removing characters. The fixture passes only when the exact authored line remains available and unrelated article text continues to reflow normally.
When this fix is not enough
Short illustrative snippets can sometimes wrap safely, but wrapping commands, diffs, tables, or aligned output may change how they are read. A local scrollbar is a deliberate Reflow exception for genuinely two-dimensional content, not a general excuse for making article text scroll sideways.
Regression condition
At narrow viewports, the failing fixture must produce page-level overflow. The fixed page must have zero page-level overflow while the code block itself reports positive local overflow, proving that the end of the command remains reachable rather than clipped.
Primary references
Sources checked for this case
- CSS Overflow Module Level 3 - W3C, checked 2026-08-04
- Understanding Success Criterion 1.4.10: Reflow - W3C Web Accessibility Initiative, checked 2026-08-04
Formal evidence
Browser and viewport matrix
Cells show failing / fixed page overflow in CSS pixels.
| Browser | 320 | 375 | 768 | 1024 | 1440 |
|---|---|---|---|---|---|
| Chromium | +432 / +0 | +377 / +0 | +0 / +0 | +0 / +0 | +0 / +0 |
| Firefox | +432 / +0 | +377 / +0 | +0 / +0 | +0 / +0 | +0 / +0 |
| WebKit | +496 / +0 | +441 / +0 | +48 / +0 | +0 / +0 | +0 / +0 |
18/18 recorded assertions passed
chromium-failing-overflow-320432pxchromium-fixed-overflow-3200pxchromium-fixed-local-scroll-320482pxchromium-failing-overflow-375377pxchromium-fixed-overflow-3750pxchromium-fixed-local-scroll-375427pxfirefox-failing-overflow-320432pxfirefox-fixed-overflow-3200pxfirefox-fixed-local-scroll-320482pxfirefox-failing-overflow-375377pxfirefox-fixed-overflow-3750pxfirefox-fixed-local-scroll-375427pxwebkit-failing-overflow-320496pxwebkit-fixed-overflow-3200pxwebkit-fixed-local-scroll-320546pxwebkit-failing-overflow-375441pxwebkit-fixed-overflow-3750pxwebkit-fixed-local-scroll-375491px
Run 2026-08-04T11-36-57-234Z. chromium 149.0.7827.201, firefox 153.0, webkit 26.5. Open raw manifest