Detect CSS Overflow Elements
Every once in a while you encounter a CSS annoyance that takes some cleverness to discover. One such case rears its ugly head in unwanted and unexpected scrollbars. When I see unwanted scrollbars, I usually open developer tools, click the element inspector, and hover around until I find the villainous HTML element. As a visual person, I find that process effective but not efficient. Recently I was made aware of a programmatic way to find the scoundrel element with JavaScript!
To find the element summoning demon scrollbars, you can use the following JavaScript:
document.querySelectorAll('*').forEach(el => {
if (el.offsetWidth > document.documentElement.offsetWidth) {
console.log('Found the worst element ever: ', el);
}
});
After the element has been logged to the console, you can pinpoint it and play with punishments in the element inspector as you see fit.
I'm always guilty of reverting to my old ways, i.e. visual inspection, but having a programmatic solution is so much faster and convenient!
With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities. I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...
I was inspired when I first saw Addy Osmani's original ShineTime blog post. The hover sheen effect is simple but awesome. When I started my blog redesign, I really wanted to use a sheen effect with my logo. Using two HTML elements and...
Spatial navigation is the ability to navigate to focusable elements based on their position in a given space. Spatial navigation is a must when your site or app must respond to arrow keys, a perfect example being a television with directional pad remote. Firefox OS TV apps are simply...
"Copy to clipboard" functionality is something we all use dozens of times daily but the client side API around it has always been lacking; some older APIs and browser implementations required a scary "are you sure?"-style dialog before the content would be copied to clipboard -- not great for...
Great little script… To make it easy to use, I ended up creating a Bookmarklet with a slightly modified version of that script. Then I can run it easily on any page.
Firefox Developer edition (not sure about the normal one) also has an indicator on the HTML element that is causing the overflow
Still didn’t help me to find the colporate for some reason… also tried the * {border: 1px solid red} trick to try to identify whats happening (tears!) – ended up just overflow-x hidden – which is lazy i guess
But.. thanks for sharing. gr8 logic that little script
I’m not sure it will help in your particular case, using outline instead of border can be a good shout, it won’t add to the width of elements (when border-box is not used)
Maybe you want to say…
Thanks for sharing
But you must to take account of margins, etc.
This line works better for me:
Detection includes cases where no scrollbar is showed but something wrong is happening.
Thanks David for inspiring us.