Last active
February 24, 2024 13:13
-
-
Save liamnewmarch/593a43c16b30c978a40ad2be2c4a3649 to your computer and use it in GitHub Desktop.
Quick script to find elements that are causing horizontal scroll because the overflow the viewport.
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
function test(which, value) { | |
if (value > 0) throw new Error(`${which}: ${value}px`) | |
} | |
for (const element of document.querySelectorAll('*')) { | |
const { left, right } = element.getBoundingClientRect() | |
try { | |
test('right', right - window.innerWidth) | |
test('left', 0 - left) | |
} catch ({ message }) { | |
element.style.outline = '2px solid hotpink' | |
console.log(`Element outside page bounds (${message})`, element) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment