Created
December 27, 2024 03:41
-
-
Save panphora/738b0ed142e5b57483b80d8ebfc9989f to your computer and use it in GitHub Desktop.
Identify Elements Causing Horizontal Overflow
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
(() => { | |
// Remove any existing highlights | |
document.querySelectorAll('.overflow-highlight').forEach(el => { | |
el.style.outline = ''; | |
el.classList.remove('overflow-highlight'); | |
}); | |
// Get viewport width | |
const viewportWidth = document.documentElement.clientWidth; | |
// Check all elements | |
document.querySelectorAll('*').forEach(element => { | |
const rect = element.getBoundingClientRect(); | |
// Check if element extends beyond viewport width | |
if (rect.right > viewportWidth || rect.left < 0) { | |
element.style.outline = '2px solid red'; | |
element.classList.add('overflow-highlight'); | |
console.log('Overflow element found:', { | |
element: element.tagName, | |
id: element.id, | |
class: element.className, | |
width: rect.width, | |
right: rect.right, | |
viewportWidth, | |
overflowAmount: Math.max(rect.right - viewportWidth, -rect.left) | |
}); | |
} | |
}); | |
console.log('Scan complete. Check elements highlighted in red.'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment