Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stop scroll bar from covering div content with auto overflow

Tags:

html

css

I have this div with overflow-y:auto; which can be very narrow and when the scroll bar appears it can cover much or all of the div. I would like the scroll bar to appear outside the div like with overflow:scroll; but I don't want to see the faded scroll bar when there is no overflow. Also I don't want to give the div a width as the width must be variable. This jsfiddle demonstrates my issue, and here is the code:

   .auto {
            display:inline-block;
            border:1px solid green;
            height:70px; 
            overflow-y:auto; 
         }

<div class = "auto">
  <div>
    a<br>
    b<br>
    c<br>
    d<br>
  </div>
</div>
like image 884
user2014429 Avatar asked May 02 '13 12:05

user2014429


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 stop DIVS from expanding?

Answer: Use the CSS display Property You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).

How will you hide extra text or content that goes outside of the area by CSS?

We can solve this problem by using CSS overflow property. overflow: hidden; hidden – The overflow is clipped, and the rest of the content will be invisible.


2 Answers

This could be a solution: http://jsfiddle.net/ZUYVe/5/

Basically, it uses a wrapper to contain the scrollbar

<div class="scroll">
  <div>a
    <br />b
    <br />c
    <br />d
    <br />
  </div>
</div>

and leaves some space on the right side for it:

padding-right: 13px;

However, since scrollbars may vary depending on OS, I'd suggest to use custom scrollbars like this jQuery plugin.

like image 190
Cedric Reichenbach Avatar answered Nov 04 '22 14:11

Cedric Reichenbach


Give 18px padding right to .auto class it will be help you.

   .auto   {
       border:1px solid green;
       height:70px; 
       overflow-y:auto; 
       display:inline-block;
       padding-right:18px;
      }
like image 37
Kishan Patel Avatar answered Nov 04 '22 14:11

Kishan Patel