Web development

Find the element causing horizontal overflow

Investigate oversized content before hiding horizontal overflow on the whole page. Common causes include fixed widths, long code lines, large images, and grid children with an implicit minimum width.

Worked example

HTML example · save as example.html and open in a browser

<style>
.layout{display:grid;grid-template-columns:12rem minmax(0,1fr);gap:1rem}
.content{min-width:0}
pre{overflow-x:auto;max-width:100%}
@media(max-width:600px){.layout{grid-template-columns:1fr}}
</style>
<div class="layout"><aside>Navigation</aside><main class="content"><pre>A long example belongs in its own scrolling region.</pre></main></div>

How it works

The minmax zero lower bound lets the flexible column shrink below its content’s intrinsic width. Min-width zero does the same for the content item. The code block receives its own horizontal scrollbar instead of stretching the entire page.

Limits and mistakes

Overflow hidden can conceal controls or text rather than repairing the cause. A scrolling region must still be usable by keyboard and touch. Do not force prose into a horizontal scroller when ordinary wrapping would be clearer.

Check the result

Narrow the viewport and inspect the page’s scroll width. Confirm only the intentionally wide region scrolls and that content does not disappear off the edge.

Sources

Reference documentation for the behavior used in this guide: