Skip to content

fix: stop the code boxes growing a vertical scrollbar - #204

Merged
magnus-madsen merged 1 commit into
masterfrom
fix-code-box-scrollbars
Aug 8, 2026
Merged

magnus-madsen merged 1 commit into
masterfrom
fix-code-box-scrollbars

Conversation

@magnus-madsen

Copy link
Copy Markdown
Member

The wider snippets showed a vertical scrollbar they had no use for. Two causes, both from overflow nobody quite wrote.

What was happening

Astro ships the <pre> with an inline overflow-x: auto of its own:

<div class="code-snippet-code">                     <!-- overflow-x: auto  (global.css) -->
  <pre style="...; overflow-x: auto;" tabindex="0">  <!-- overflow-x: auto  (Astro, inline) -->

So the code boxes were two nested scroll containers, the outer one redundant. And neither states overflow-y, so per the CSS overflow spec the initial visible is promoted to auto when the other axis is not visible. The horizontal scrollbar is then laid out inside the box and steals just enough height to trip the vertical one.

That is why it was only some boxes — it tracks the ones wide enough to scroll horizontally. At 12px Monaco inside a col-md-6, roughly:

Viewport Fits Snippets that overflow
~1440px ~79 ch none
~1280px ~67 ch httpExample
~1024px ~54 ch 13 of 22
~800px ~38 ch nearly all

The fix

  • Drop the outer scroller; let the <pre> be the only one.
  • State overflow-y: hidden on it, since leaving it implicit is what created the second axis.
  • Move the bottom padding from the div onto the <pre>, so the horizontal scrollbar has somewhere to sit that isn't the last line of code. overflow-y: hidden on its own would have traded the stray scrollbar for a clipped last line.

Bottom spacing is unchanged when no scrollbar is present. Keeping the scroller on the <pre> rather than the div is also the nicer of the two: the side padding stays a fixed gutter, instead of the left one scrolling away and the code running flush to the edge when you scroll right.

Side padding is also trimmed from 1em 1.75em to 0.9em 1.6em, as discussed separately.

Reviewing it

This only reproduces where scrollbars take layout space — macOS overlay scrollbars occupy none, so you need System Settings → Appearance → Show scroll bars: Always, or Windows/Linux. Good places to look are httpExample at ~1280px, or most of the home page around ~1024px: the horizontal bar should sit in the padding with the last code line fully visible, and no vertical bar.

npm run build passes; all 10 pages generate.

🤖 Generated with Claude Code

The wider snippets showed a vertical scrollbar they had no use for. Two
causes, both from overflow nobody quite wrote:

Astro ships the <pre> with an inline overflow-x:auto of its own, so the
overflow-x:auto on .code-snippet-code made the code boxes two nested
scroll containers. And neither one states overflow-y, so per the CSS
overflow spec the initial `visible` is promoted to `auto` alongside a
non-visible x. The horizontal scrollbar is then laid out inside the box
and steals just enough height to trip the vertical one.

So: drop the outer scroller and let the <pre> be the only one, state
overflow-y:hidden on it, and move the bottom padding from the div onto
the <pre> so the horizontal scrollbar has somewhere to sit that is not
the last line of code. Bottom spacing is unchanged when no scrollbar is
present.

Keeping the scroller on the <pre> rather than the div is also the nicer
of the two: the side padding stays a fixed gutter instead of the left
one scrolling away and the code running flush to the edge.

Side padding is also trimmed from 1em 1.75em to 0.9em 1.6em.

Note this only reproduces where scrollbars take layout space - macOS
overlay scrollbars occupy none, so it needs "Show scroll bars: Always",
or Windows/Linux.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@magnus-madsen
magnus-madsen merged commit 7c75c4b into master Aug 8, 2026
2 checks passed
@magnus-madsen
magnus-madsen deleted the fix-code-box-scrollbars branch August 8, 2026 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant