Skip to content

Instantly share code, notes, and snippets.

@jeiea
Forked from cuth/debug-scroll.md
Last active October 28, 2021 23:05
Show Gist options
  • Save jeiea/3c5302dbf84f31a51a972829fd7af44c to your computer and use it in GitHub Desktop.
Save jeiea/3c5302dbf84f31a51a972829fd7af44c 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

(() => {
  let { offsetWidth, offsetHeight } = document.documentElement;
  let walker = document.createTreeWalker(document.body, NodeFilter.SHOW_ELEMENT);
  while (walker.nextNode()) {
    let bound = walker.currentNode.getBoundingClientRect();
    let isWidthOverflow = bound.right > offsetWidth || bound.left < 0;
    let isHeightOverflow = bound.bottom > offsetHeight || bound.top < 0;
    let { style } = walker.currentNode;
    if (isWidthOverflow && isHeightOverflow) {
      style.setProperty('outline', '1px dotted red', 'important');
    } else if (isWidthOverflow) {
      style.setProperty('outline', '1px dotted maroon', 'important');
    } else if (isHeightOverflow) {
      style.setProperty('outline', '1px dotted darkviolet', 'important');
    } else {
      continue;
    }
    console.log(walker.currentNode);
  }
})();

Bookmarklet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment