I have a small div with overflow:auto;
but when the the scroll bar appears it covers up a great deal of the div. This can be avoided by using overflow:scroll;
, but then you get the unsightly faded scroll bar when there is no overflow. Is there a way of putting the scroll bar outside of the div without using overflow:scroll;
? Thanks.
here is a demonstration jsfiddle
.alphabet{ display:inline-block; overflow-y:auto; overflow-x:hidden; border:1px solid; height:50; } <div class = "alphabet">abcdefgh<br> ijklmnop<br> qrstuvwx </div>
Making a div vertically scrollable is easy by using CSS overflow property. There are different values in overflow property. For example: overflow:auto; and the axis hiding procedure like overflow-x:hidden; and overflow-y:auto;.
Use overflow: auto . Scrollbars will only appear when needed. (Sidenote, you can also specify for only the x, or y scrollbar: overflow-x: auto and overflow-y: auto ).
For horizontal scrollable bar use the x and y-axis. Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line.
overflow: scrollSetting the value to scroll , the overflow is clipped and a scrollbar is added to scroll inside the box. Note that this will add a scrollbar both horizontally and vertically (even if you do not need it): You can use the overflow property when you want to have better control of the layout.
If it's an option to use a container element around .alphabet
, you can set the vertical scroll on that. I've added the <hr>
to fake an always-visible bottom border that won't also go under the scrollbar.
HTML:
<div class="container"> <div class="alphabet"> abcdefgh<br /> abcdefgh<br /> abcdefgh<br /> abcdefgh<br /> </div> </div> <hr>
CSS:
.container{ overflow-y:auto; overflow-x:hidden; height:50px; width:100px; } .alphabet{ width:100%; overflow:visible; box-sizing:border-box; border:1px solid; border-bottom:0; } hr{ margin:0; height:0; width:85px; border:0; border-bottom:1px solid; }
With inner border: http://jsfiddle.net/Q32gG/1/
If you don't actually care about the scrollbar showing inside the border, you can drop the <hr>
and add a full border to .container
instead (http://jsfiddle.net/V3MbV/3/).
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