← Tous les articles

Horizontal scroll on mobile: finding the element that overflows

The page slides sideways on phones? One element that is too wide is enough. How to find it in a minute and fix it properly.

Horizontal scroll on mobile: finding the element that overflows

Key points

  • A single element wider than the screen creates horizontal scrolling.
  • A short console script points to it immediately.
  • overflow-x: hidden on body hides the symptom; fix the element instead.

On phones the page “floats”: you can drag it sideways and an empty strip appears on the right. It is a common defect, and we check for it before every release.

Find the culprit in a minute

Open developer tools in mobile mode, then paste this into the console:

const w = document.documentElement.clientWidth;
document.querySelectorAll('*').forEach(el => {
  const r = el.getBoundingClientRect();
  if (r.right > w + 1 || r.left < -1) console.log(el, Math.round(r.right - w));
});

The console lists each overflowing element and by how many pixels. Start with the highest one in the page: its children often overflow because of it.

The usual causes

  • A fixed width: width: 600px on a table, an embedded video or a form.
  • width: 100vw: on desktop it includes the scrollbar and overflows by a few pixels.
  • A long word or URL with no spaces: add overflow-wrap: anywhere; to text areas.
  • Animations that start off-screen (translateX(100px)).
  • Negative margins on a grid without matching container padding.

Fix rather than hide

body { overflow-x: hidden; } removes the scroll but can break position: sticky and leaves content cut off. As a safety net that keeps sticky working, use:

html, body { overflow-x: clip; }

For tables, wrap them in an overflow-x: auto container: the table scrolls on its own and the page stays still.

FAQ

Why only on some phones?

Screen widths range from 320 to over 430 pixels: a 360px element fits on one and overflows on another.

Can this check be automated?

Yes, a Playwright script can open each page at mobile width and flag any overflow. We run one on every deploy.

Facing this on your site or app? We diagnose and fix it.

Partager LinkedIn Facebook X WhatsApp
</>

webtestudio

Agence web : sites WordPress sur mesure, thèmes premium et logiciels SaaS.

Parler de votre projet →