Thursday, 8 April, 2021 UTC


Summary

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!
The post Detect CSS Overflow Elements appeared first on David Walsh Blog.