← Back to HTML & CSS, page 5

html-css-0085 · Hard senior

A page has an unwanted horizontal scrollbar on phones. How do you find the cause and fix it properly?

Find the element that is wider than the viewport with DevTools or a console check, then fix the cause: fixed pixel widths, 100vw, long unbroken text, wide media or flex items with min-width: auto. Do not hide the symptom with overflow-x: hidden on the body.

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:

  1. Open DevTools in a narrow viewport. Scroll right and see which part sticks out.
  2. Run a short check in the console that lists elements whose right edge is past the viewport width: compare el.getBoundingClientRect().right with document.documentElement.clientWidth.
  3. 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. Use max-width: 100% or a fluid width.
  • width: 100vw with a vertical scrollbar present. 100vw includes the scrollbar width, so it is wider than the visible area on desktop browsers that show classic scrollbars. Use width: 100% instead.
  • A long word, URL or code line. Use overflow-wrap: anywhere on text, or overflow-x: auto on a pre.
  • 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. Add min-width: 0.
  • Negative margins, or padding added to a width: 100% box without box-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.

html
<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.