Forked from joshwcomeau/find-elem-with-breakout-width.js
Created
July 27, 2022 14:20
-
-
Save villesau/c156094354b392ec536119277f3fcf9a to your computer and use it in GitHub Desktop.
Paste this in your browser console to search for HTML elements which extend past the window's width and create a horizontal scrollbar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let widestElement = document.body; | |
let widestElementDepth = 0; | |
function checkElemWidth(elem, depth) { | |
const currentWidest = widestElement.getBoundingClientRect(); | |
const boundingRect = elem.getBoundingClientRect(); | |
const currentRight = currentWidest.right; | |
const right = boundingRect.right; | |
if (right >= currentRight && depth > widestElementDepth) { | |
widestElement = elem; | |
widestElementDepth = depth | |
} | |
// Recursively check all the children | |
// of the element to find the culprit. | |
[...elem.children].forEach(elem => checkElemWidth(elem, depth + 1)); | |
} | |
checkElemWidth(document.body, widestElementDepth); | |
widestElement.scrollIntoView(); | |
console.info( | |
`widest element, ${widestElement.getBoundingClientRect().right}px from left is: ${widestElement.className}` | |
); | |
console.log(widestElement); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let widestElement = document.body; | |
let widestElementDepth = 0; | |
function checkElemWidth(elem, depth) { | |
if (elem.clientWidth >= widestElement.clientWidth && depth > widestElementDepth) { | |
widestElement = elem; | |
widestElementDepth = depth | |
} | |
// Recursively check all the children | |
// of the element to find the culprit. | |
[...elem.children].forEach(elem => checkElemWidth(elem, depth + 1)); | |
} | |
checkElemWidth(document.body, widestElementDepth); | |
widestElement.scrollIntoView(); | |
console.info( | |
`widest element, ${widestElement.clientWidth}px wide is: ${widestElement.className}` | |
); | |
console.log(widestElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment