Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Created March 3, 2023 00:15
Show Gist options
  • Save crazy4groovy/61132f8cd2e1e14d1a32ae23d8feae29 to your computer and use it in GitHub Desktop.
Save crazy4groovy/61132f8cd2e1e14d1a32ae23d8feae29 to your computer and use it in GitHub Desktop.
identify an overflowing element on the page (JavaScript)
/*
Real devs know that centering a div is easy, but what makes even the most senior developers cry - that random overflowing element that makes the whole page scroll horizontally.
How could you find that element? Answer below.
*/
document.querySelectorAll("*").forEach(el => {
if(el.offsetWidth > document.documentElement.offsetWidth) {
console.log(el);
}
})
/*
If an element on the page is wider than the document, it’ll get logged to the console.
Bonus:
Most browsers allow you to right click on the elements that were printed to the console and scroll them into view. Unfortunately, you still have to fix the issue, but at least you’re one step closer to the solution.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment