Created
March 3, 2023 00:15
-
-
Save crazy4groovy/61132f8cd2e1e14d1a32ae23d8feae29 to your computer and use it in GitHub Desktop.
identify an overflowing element on the page (JavaScript)
This file contains hidden or 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
/* | |
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