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>
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.
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).
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.
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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With