Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put scroll bar outside div with auto overflow

Tags:

html

css

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> 
like image 216
user2014429 Avatar asked Apr 30 '13 18:04

user2014429


People also ask

How do I make my div scrollable overflow?

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;.

How do I show the scroll bar only on overflow?

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 ).

How can I add a horizontal scrollbar to my div automatically?

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.

How do I add scrolls to overflow?

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.


1 Answers

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/).

like image 70
Lily Avatar answered Sep 23 '22 12:09

Lily