Skip to content

Instantly share code, notes, and snippets.

@jareha
Created February 8, 2022 19:51
Show Gist options
  • Save jareha/4fcdc1d6271301ccb87075d3b7d470ae to your computer and use it in GitHub Desktop.
Save jareha/4fcdc1d6271301ccb87075d3b7d470ae to your computer and use it in GitHub Desktop.
Bookmarklet to highlight DOM elements that cause horizontal scrollbars
/* jshint esversion: 8 */
(function (currentDocument) {
const treeWalker = currentDocument.createTreeWalker(currentDocument.body, NodeFilter.SHOW_ELEMENT);
const width = currentDocument.documentElement.offsetWidth;
while (treeWalker.nextNode()) {
const domRectangle = treeWalker.currentNode.getBoundingClientRect();
if (domRectangle.right > width || domRectangle.left < 0) {
const currentNode = treeWalker.currentNode;
currentNode.style.setProperty("outline", "2px dotted red", "important");
console.log(currentNode);
}
}
}(document));
// javascript:(function(currentDocument){var width=currentDocument.documentElement.offsetWidth;var treeWalker=currentDocument.createTreeWalker(currentDocument.body,NodeFilter.SHOW_ELEMENT);while(treeWalker.nextNode()){const domRectangle=treeWalker.currentNode.getBoundingClientRect();if(domRectangle.right>width||domRectangle.left<0){const currentNode=treeWalker.currentNode;currentNode.style.setProperty("outline","2px dotted red","important");console.log(currentNode)}}}(document));
// Minified at https://duckduckgo.com/?q=javascript+minifier
/*
License: CC BY-SA 4.0, https://creativecommons.org/licenses/by-sa/4.0/
Creator: Tamilselvan K, https://stackoverflow.com/users/3089813/tamilselvan-k/
Source: https://stackoverflow.com/a/52195436/
Modifications: reformatted for use as a bookmarklet, readability
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment