Skip to content

Instantly share code, notes, and snippets.

@liamnewmarch
Last active February 24, 2024 13:13
Show Gist options
  • Save liamnewmarch/593a43c16b30c978a40ad2be2c4a3649 to your computer and use it in GitHub Desktop.
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.
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