Skip to content

Instantly share code, notes, and snippets.

@ahmadawais
Created December 2, 2018 09:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmadawais/8b97de6b6ce392212e4cd98fd0f98698 to your computer and use it in GitHub Desktop.
Save ahmadawais/8b97de6b6ce392212e4cd98fd0f98698 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

Find the elements that are causing a 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

Save a bookmark > right click > Edit > Change URL to the following content.

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