How to Find the Element Causing Horizontal Scroll
A tester filed a bug last March that said "page wobbles sideways on my phone". No screenshot, no device model.

A tester filed a bug last March that said "page wobbles sideways on my phone". No screenshot, no device model. It turned out to be four pixels of horizontal scroll on a marketing page, caused by a decorative circle positioned at right: -2rem inside a section that had no overflow rule. Nobody on the team could reproduce it on a desktop browser, because at 1440 pixels wide the circle sat over empty gutter space and never pushed the document.
Horizontal overflow is one of the few layout bugs that is genuinely hard to see and genuinely easy to locate, which is a frustrating combination. The visual signal is tiny: a scrollbar that appears for a second, or a page that rubber bands sideways under a thumb. Meanwhile the browser already knows exactly which element is sticking out and will tell you in about one minute if you ask it the right way.
Confirm the document is really the thing scrolling
Before hunting for an element, establish which box has the overflow. A page can scroll sideways because the document is wider than the viewport, or because some inner container with overflow: auto is wider than its own content box. Those are different bugs with different fixes, and people waste a lot of time fixing the wrong one.
In the console, compare two numbers on the document element: document.documentElement.scrollWidth and document.documentElement.clientWidth. If the first is larger, the document itself is overflowing and the culprit is a descendant that extends past the viewport edge. If they match but you can still drag the page sideways, the overflow belongs to a nested scroller, and you find that one by walking up from the element under your finger with el.closest plus a check on computed overflow.
Worth doing this in the viewport size where the bug appears. Responsive mode in DevTools is good enough for the measurement, with one caveat covered below about scrollbar width, which is exactly the sort of difference that makes a bug show on one machine and not another.
The console snippet that names the element
This is the whole technique. Walk every element, measure its box against the viewport, and report anything that crosses the edge.
const w = document.documentElement.clientWidth; document.querySelectorAll('*').forEach(el => { const r = el.getBoundingClientRect(); if (r.right > w + 1 || r.left * -1 > 1) console.log(Math.round(r.right), el); });
Three details make the difference between this working and producing a hundred lines of noise. The tolerance of one pixel suppresses subpixel rounding, which otherwise flags half the page on a scaled display. Logging the element reference rather than a string gives you a clickable node in the console that highlights in the Elements panel on hover. And checking the left edge as well catches negative margins, which are a common cause and invisible if you only look at the right side.
You will usually get a cascade of results: the offending element plus every ancestor that contains it. The one you want is the deepest node in the list, so read from the bottom. If the deepest result is the body or a top level wrapper, you are looking at a width or padding bug rather than a stray child.
For a quick visual pass, the old trick of applying outline: 1px solid red to everything still has its place, because outlines do not affect layout the way borders do. It finds elements that spill a visible amount. It will not find anything parked off screen, which is why I reach for the measurement first and the outline second.
The seven causes behind nearly every case
Once you have the element, the fix depends on why it is wide. In my notes from roughly four years of these bugs, almost all of them fall into one of these.
| Cause | How it shows up | Fix |
|---|---|---|
| A unit of 100vw on a full bleed section | Overflow equal to the scrollbar width, around 15 pixels, desktop only | Use 100 percent, or the newer 100dvw family, since vw ignores a classic scrollbar |
| Grid or flex child refusing to shrink | One column wider than its track, usually holding a long word or a table | Set the track to minmax(0, 1fr), or give the flex item min-width of 0 |
| Long unbreakable string | A raw URL, a hash, a code sample or a German compound noun | Apply overflow-wrap: anywhere on the text container, not on the body |
| Decorative element with a negative offset | Overflow of a few pixels that only appears under a given breakpoint | Add overflow: clip to the section that owns the decoration |
| Padding on a box set to width 100 percent | Overflow exactly equal to the left plus right padding | Set box-sizing: border-box, or drop the explicit width |
| Transform or box-shadow pushing past the edge | Scroll appears on hover or during an animation only | Clip the parent, since transforms do not change layout but do extend the scrollable area |
| A wide image or iframe with no max-width | Overflow matching the asset's intrinsic width | Set max-width: 100 percent and height: auto on media |
The viewport unit row deserves a note, because it explains the most common "works on my machine" version of this bug. A length of 100vw resolves to the viewport width including the space taken by a classic scrollbar, while 100% of the body resolves to the space actually available. On macOS and on phones, where scrollbars overlay the content, the two agree and nothing goes wrong. On Windows with a classic scrollbar, the 100vw section is about 15 pixels too wide, which creates a horizontal scrollbar, which does not change the 100vw value, so the problem is stable rather than oscillating. Two colleagues can look at the same page and only one sees it.
Why I stopped reaching for overflow-x on the body
The advice you will find most often is to put overflow-x: hidden on html or body and move on. I have done it under deadline and I will probably do it again, but it is worth knowing what you are buying.
Setting an overflow value other than visible on an ancestor turns that element into a scroll container, and position: sticky inside a scroll container sticks to that container rather than to the viewport. This is the single most common reason a sticky header stops working weeks after someone else fixed an unrelated layout bug, which we pulled apart in why position sticky stops working. The overflow fix and the sticky bug are frequently the same commit.
There is a second cost. The content is still there, just unreachable, so keyboard users can focus an element that cannot be scrolled into view, and anyone on a right to left locale may find the overflow lands on the other side where hiding it does something different. The content also still counts for the page's scrollable area in some engines, which can leave you with a page that scrolls in one browser and not another.
If you do want to clip rather than fix, prefer overflow: clip on the specific section that owns the stray element. It clips without creating a scroll container, so sticky and scroll anchoring keep working, and it has been safe in all current engines for a while now. Scope it to the smallest box you can, and leave a note in the stylesheet saying what it is clipping, because the next person will otherwise delete it.
One honest limitation on all of this: an overflow that only appears after an interaction, such as a menu that animates in from the right, will not show in a static scan. For those I record a performance trace and watch the document width over time, or just run the console snippet with the menu open. There is no clever shortcut, and anyone who tells you the layout tools in grid and flexbox prevent this class of bug has not shipped a page with a decorative blob on it. More of this sits in the front end section.
FAQ
Why does my page scroll sideways only on Windows?
Almost always a length in vw units on a full width section. Viewport units include the classic scrollbar, so the section ends up roughly 15 pixels wider than the space available. Switch to a percentage of the parent.
How do I find the element causing overflow without DevTools open on the device?
Run the measuring snippet inside a bookmarklet, or add it behind a query string flag in a staging build so you can trigger it on a real phone. Remote debugging works too, but the bookmarklet is faster when you have the device in your hand.
Is overflow-x: hidden on the body ever acceptable?
As a deliberate, documented clip, yes. As a fix for an unknown cause, it buries the bug and can break sticky positioning elsewhere on the page. Prefer overflow: clip on the narrowest container that contains the problem.
Why does the console log so many elements?
Every ancestor of the overflowing node also crosses the viewport edge, so they all match. Read the list from the bottom and work with the deepest element.
More in Front-End
Front-End
How Front-End Retry Code Quietly Overloads Your API
A payments endpoint we run went dark for about 90 seconds on a Tuesday afternoon.
Front-End
Why z-index Stops Working: Stacking Contexts Explained
The dropdown menu opens, and half of it disappears behind the hero image below. You give the menu z-index: 10 . Nothing.
Front-End
Event Delegation in JavaScript and When to Use It
You render a list of 300 rows, attach a click listener to each delete button, and everything works until new rows arrive from...