How do you make preformatted code blocks fit on small screens?

Make a preformatted code block fit by keeping its formatting and giving it an intentional horizontal scrolling area. Code often cannot wrap without changing readability or copy accuracy. The scroller should stay inside the page width and remain usable by keyboard and touch.
pre {
max-inline-size: 100%;
overflow-x: auto;
padding: 1rem;
}
pre code {
white-space: pre;
}
Place the block in a Grid or Flex child with min-width: 0 so the parent can shrink. Use a visible boundary and enough bottom padding for a scrollbar. Avoid applying overflow-x: hidden, which removes access to the clipped part of each line.
Optional wrapping can be offered for prose-like snippets with white-space: pre-wrap and overflow-wrap: anywhere, but test whether indentation and commands remain understandable. A copy button should have an accessible name, visible focus and a text confirmation after activation.
Test arrow keys, Shift plus mouse wheel where supported, touch panning, zoom and high contrast. Announce the region or provide a short instruction if horizontal scrolling is not visually obvious. Keep font size readable instead of shrinking code until it fits without scrolling. Recheck the pattern after typography, spacing or browser-support changes. Shared token updates can revive overflow, so the test should use the limiting content state instead of an ideal screenshot. Keep the layout rule beside the component and document why the threshold or constraint exists. This reduces accidental overrides and gives future editors a concrete condition to retest after content changes. Add this case to responsive regression tests with long content, browser zoom and at least one narrow embedded container. Record the expected wrapping and focus behavior so later component changes can be checked against a known result.