Full answer
A horizontal scrollbar means something is wider than the viewport. Hiding it with overflow-x: hidden on body is a patch that hides the symptom, can break position: sticky, and can cut off content that users should see. Find the element first.
Steps to find it:
- Open DevTools in a narrow viewport. Scroll right and see which part sticks out.
- Run a short check in the console that lists elements whose right edge is past the viewport width: compare
el.getBoundingClientRect().rightwithdocument.documentElement.clientWidth. - Or add a temporary
* { outline: 1px solid red; }and look for the box that reaches beyond the edge.
Frequent causes and fixes:
- A fixed width in pixels such as
width: 600px. Usemax-width: 100%or a fluid width. width: 100vwwith a vertical scrollbar present.100vwincludes the scrollbar width, so it is wider than the visible area on desktop browsers that show classic scrollbars. Usewidth: 100%instead.- A long word, URL or code line. Use
overflow-wrap: anywhereon text, oroverflow-x: autoon apre. - A wide table or image. Give images
max-width: 100%and put tables in a scrollable wrapper. - A flex item that refuses to shrink because of
min-width: auto. Addmin-width: 0. - Negative margins, or
paddingadded to awidth: 100%box withoutbox-sizing: border-box.
The first green block is 500px wide inside a 300px box, so the box has a horizontal scroll width. The second is limited to 100% of the box. The first long address is wider than its box, and the second wraps inside it. Compare scrollWidth and clientWidth to see the difference.
After the fix, test the real page at 320px width and with browser zoom, and look for new clipping.
<style>
html, body { margin: 0; }
* { box-sizing: border-box; }
.fixed { width: 500px; background: #cfc; }
.fixed.fixed-ok { width: 100%; max-width: 500px; }
.url { background: #cde; }
.url.url-ok { overflow-wrap: anywhere; }
</style>
<div style="width:300px;overflow:auto;height:60px" id="box">
<div class="fixed" id="a">Fixed 500px</div>
<div class="fixed fixed-ok" id="b">Fluid</div>
<div class="url" id="c">https://example.com/a/very/long/address/without/any/break/points/in/it</div>
<div class="url url-ok" id="d">https://example.com/a/very/long/address/without/any/break/points/in/it</div>
</div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Locate the overflowing element before changing anything.
- Fixed widths, 100vw and long strings are common causes.
- Flex items may need min-width: 0.
- overflow-x: hidden on body only hides the symptom.
- Retest at 320px and with zoom.
Common mistakes
- Adding overflow-x: hidden to the body without finding the element that is too wide.
- Using 100vw for full-width blocks, which includes the scrollbar width.
- Fixing the width of one element but not checking again at other screen sizes and zoom levels.
Follow-ups
- Why can 100vw cause a horizontal scrollbar?
- How would you list the elements that are wider than the viewport in the console?
Tips
- Describe a method, not a guess: find the element first with the console, then fix the cause.
- Name 100vw and min-width: auto as two causes that surprise people.