Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical Scrollbar leads to horizontal scrollbar

My CSS looks like this:

div.SOMECLASS {
  position: absolute;
  max-height: 300px
  height: auto;
  width: auto;
  overflow: auto;
  ...
}

The div height and width scale automatically. The height has a fixed maximum though: as soon as this value is reached vertical scrollbars appear. This works all pretty swell.

Now the issue:
When the vertical scrollbar appears, it uses up around 10px of horizontal space, as the scrollbar will be placed inside the div.
However, the width is not autoscaled to allow for these additional 10-something pixels used up by the vertical scrollbars. As the horizontal width before the adding the vertical scrollbars was just exactly right for the content (as expected from the width:auto setting), the div now also displays horizontal scrollbars - to allow for the missing 10 pixels. This is silly.

  • How can I avoid having these horizontal scrollbars and just autoscale the width of the div to make the vertical scrollbars fit?

If possible I am looking for a solution which does not rely on just completely disabling horizontal scrolling, as this will probably be needed at some point (i.e. for certain inputs).

like image 365
fgysin Avatar asked Nov 26 '12 17:11

fgysin


People also ask

Why am I getting a horizontal scrollbar on my website?

That makes sense — if you specify a fixed width of 3000px in the CSS, someone at 1920x1080 resolution will only see the left-hand 1920 pixels of the content on the website. On the right side, there is “horizontal overflow,” so you'll see horizontal scrollbars.

How do I find out what is causing horizontal scroll?

To find out which elements cause a horizontal scrollbar, you can use the devtools to delete elements one by one until the scrollbar disappears. When that happens, press ctrl/cmd Z to undo the delete, and drill down into the element to figure out which of the child elements cause the horizontal scrollbar.

How do I get rid of the horizontal scroll bar?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.


3 Answers

Just figured out a pretty passable solution (at least for my version of this problem).

I assume the issue with width: auto is that it behaves similarly to width: 100vw; the problem is that when the vertical scrollbar appears, the viewport width remains the same (despite the ~10px scrollbar), but the viewable area (as I'm calling it) is reduced by 10px.

Apparently defining width by percentage defines it in terms of this "viewable area", so changing your code to:

div.SOMECLASS {
  position: absolute;
  max-height: 300px
  height: auto;
  width: 100%;
  overflow: auto;
  ...
}

should allow the content to rescale properly!

p.s. You can also instead add overflow-x: hidden, which will stop the horizontal scrollbar from appearing, instead simply cutting ~10px off of the right side of your div when the vertical scrollbar appears.

like image 127
Spencer Avatar answered Sep 28 '22 18:09

Spencer


I found a solution which is working but far from perfect:

I added a padding-right : 15px to my div, to automatically grow the entire div. Now if the vertical scrollbars appear, they fit within the padding so the horizontal width is still ok.

Regretfully the padding of course also shows up when no vertical scrolling is needed, making my div just a tiny bit wider than it would have to be... :/ Well, in my eyes this is still preferable to unneeded horizontal scrollbars.

like image 18
fgysin Avatar answered Sep 28 '22 18:09

fgysin


Often setting 100vw is the problem. Just remove it and your width will be 100%, which will be what you want anyways.

like image 2
bersling Avatar answered Sep 28 '22 20:09

bersling