Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing scrollbars only when mouseover div

Tags:

Given this div:

<div style="overflow:auto;"></div> 

How can I make the scrollbars visible only when the mouse is over the div?

I don't want the scrollbars to always appear. Facebook's right-top corner is an example of this.

like image 611
Utku Dalmaz Avatar asked Apr 04 '12 16:04

Utku Dalmaz


People also ask

How do I show scroll only when overflow?

To scroll only horizontally, we need to use the overflow-x property as we mentioned above and set the width and border properties. Also, we add a <p> element inside the <div> tag and then add style to it in the CSS section.

How do I keep scrollbars visible?

To always show the scrollbar, you need to enable a setting in Windows Settings. For that, press Win+I to open Windows Settings and go to Accessibility > Visual effects. Then, Toggle the Always show scrollbars button to turn it on.

How do I get a scrollbar inside a div?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I hide the scroll bar in inactivity?

There are many ways to hide the scroll bar when the page is inactive. One such way is using the onscroll, onmousewheel, onclick and onmousemove events, which help us to achieve our goal using basic HTML and JavaScript.


1 Answers

You can make overflow hidden until the mouse is over it, then make it auto. This is what I did ... note the 16px padding assumes a scrollbar is 16px wide, and is there so the text doesn't re-wrap when the scrollbar appears.

    div.myautoscroll {         height: 40ex;         width: 40em;         overflow: hidden;         border: 1px solid #444;         margin: 3em;     }     div.myautoscroll:hover {         overflow: auto;     }     div.myautoscroll p {         padding-right: 16px;     }     div.myautoscroll:hover p {         padding-right: 0px;     } 

See it in action at this fiddle - you'll want to widen the right side "result" window to see the whole box, or reduce the width in the css.


Edit 2014-10-23

There is now more variation in how systems and browsers display scrollbars, so my 16px space may need to be adjusted for your case. The intent of that padding is to prevent the text from being re-flowed as the scrollbar appears and disappears.

Some systems, such as newer versions of Mac OS X (10.8.x at least), don't show scrollbars until you start scrolling which could throw this whole technique off. If the scrollbar doesn't show you may have no reason to hide it until hover, or you may want to leave overflow as auto or even scroll rather than toggling it.

like image 90
Stephen P Avatar answered Oct 27 '22 10:10

Stephen P