Skip to content
iD-SIGN
Contact us

How to deal with unwanted horizontal scrollbars

If you are surprised that your page is wider than the viewport and have no idea why, the following tip may come in handy

1 min readpublished 10/2020

When I code web pages, it sometimes happens with more complex responsive templates that some element overflows the width of the document and causes horizontal scrollbars. I don’t always notice it quickly, because the overflow may only occur at certain widths, and even fixing it can turn into detective work.

Deleting individual nodes until the overflow disappears doesn’t always get you there quickly. The element inspector didn’t always help me either. I also used Firefox and its 3D view mode, which could render the boundaries of all layers in 3D, including the boundaries of the base document. However, this mode has since been removed from Firefox, so I had no choice but to look for another solution.

On CSS-Tricks I found a great article https://css-tricks.com/findingfixing-unintended-body-overflow/ with a handy snippet that, once pasted into the console, finds all the culprits and prints them in a list.

javascript
var docWidth = document.documentElement.offsetWidth;

[].forEach.call(
  document.querySelectorAll('*'),
  function(el) {
    if (el.offsetWidth > docWidth) {
      console.log(el);
    }
  }
);

If you run into a similar problem from time to time, give this a try too. If it helps you, enjoy :-)

Tags Frontend, JavaScript

Share LinkedIn Facebook

Need more than an article?

A few sentences about what you are dealing with are enough.