Skip to content

Instantly share code, notes, and snippets.

@partiid
Forked from cuth/debug-scroll.md
Created June 4, 2024 12:53
Show Gist options
  • Save partiid/27c9f4bf6a66feeb22e95d3ed70fcddd to your computer and use it in GitHub Desktop.
Save partiid/27c9f4bf6a66feeb22e95d3ed70fcddd to your computer and use it in GitHub Desktop.
Find the elements that are causing a horizontal scroll. Based on http://css-tricks.com/findingfixing-unintended-body-overflow/

Debug Horizontal Scroll

(function (d) {
    var w = d.documentElement.offsetWidth,
        t = d.createTreeWalker(d.body, NodeFilter.SHOW_ELEMENT),
        b;
    while (t.nextNode()) {
        b = t.currentNode.getBoundingClientRect();
        if (b.right > w || b.left < 0) {
            t.currentNode.style.setProperty('outline', '1px dotted red', 'important');
            console.log(t.currentNode);
        }
    };
}(document));

Bookmarklet

javascript:(function(d){var w=d.documentElement.offsetWidth,t=d.createTreeWalker(d.body,NodeFilter.SHOW_ELEMENT),b;while(t.nextNode()){b=t.currentNode.getBoundingClientRect();if(b.right>w||b.left<0){t.currentNode.style.setProperty('outline','1px dotted red','important');console.log(t.currentNode);}};}(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment