Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML reserve space for scrollbar

Tags:

I have a div that may overflow as content is added or removed.

However the UI designer does not want a visible, but inactive scrollbar (as with overflow: scroll), and they don't want the content layout to change when one is added and remove (as with overflow: auto).

Is there a means to get this behavior, and considering the different scrollbars on different platforms and browsers.

enter image description here https://jsfiddle.net/qy9a2r00/1/

like image 499
Fire Lancer Avatar asked Jul 08 '16 11:07

Fire Lancer


People also ask

How do I stop my scrollbar from using space?

The easy fix is to use width: 100% instead. Percentages don't include the width of the scrollbar, so will automatically fit. If you can't do that, or you're setting the width on another element, add overflow-x: hidden or overflow: hidden to the surrounding element to prevent the scrollbar.

How do I reduce the size of the scrollbar in HTML?

scrollbar-width accepts the following values: auto is the default value and will render the standard scrollbars for the user agent. thin will tell the user agent to use thinner scrollbars, when applicable. none will hide the scrollbar completely, without affecting the element's scrollability.

What is scroll margin?

The scroll-margin shorthand property sets all of the scroll margins of an element at once, assigning values much like the margin property does for margins of an element.

Does 100vw include scrollbar?

Many assume that width: 100vw is the same as width: 100% . This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.


2 Answers

No browser support this property yet (2021), but scrollbar-gutter is a suggested solution for this.

like image 151
olejorgenb Avatar answered Nov 08 '22 23:11

olejorgenb


The only way to do this is to make the width of the items in the container fixed. And you'll have to be conservative with the width of the scrollbar.

   .container { 
       width: 200px;
       height: 200px;
       overflow: auto;
       overflow-x: hidden;
       border: 1px solid black;
   }
   .item {
       width: 200px;
...

https://jsfiddle.net/fr1jnmn6/1/

like image 45
Sean Avatar answered Nov 09 '22 01:11

Sean