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 […]

The post Detect CSS Overflow Elements appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

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.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2021-04-08T10:59:14+00:00) Detect CSS Overflow Elements. Retrieved from https://www.scien.cx/2021/04/08/detect-css-overflow-elements/

MLA
" » Detect CSS Overflow Elements." David Walsh | Sciencx - Thursday April 8, 2021, https://www.scien.cx/2021/04/08/detect-css-overflow-elements/
HARVARD
David Walsh | Sciencx Thursday April 8, 2021 » Detect CSS Overflow Elements., viewed ,<https://www.scien.cx/2021/04/08/detect-css-overflow-elements/>
VANCOUVER
David Walsh | Sciencx - » Detect CSS Overflow Elements. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/08/detect-css-overflow-elements/
CHICAGO
" » Detect CSS Overflow Elements." David Walsh | Sciencx - Accessed . https://www.scien.cx/2021/04/08/detect-css-overflow-elements/
IEEE
" » Detect CSS Overflow Elements." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2021/04/08/detect-css-overflow-elements/. [Accessed: ]
rf:citation
» Detect CSS Overflow Elements | David Walsh | Sciencx | https://www.scien.cx/2021/04/08/detect-css-overflow-elements/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.