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.
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.
I have had the same problem and tried a bunch of the above solutions without result. After lots of research I came to this solution. Just paste these lines into your css file.
What was happening to me was that Mac OSX lion and up (I am running Yosemite) auto hide scrollbars to seem more sleek. The code above overwrites the default and brings back the scrollbar ... combined with the css for changing the overflow to scroll on hover this will achieved the desired result for users on the newer mac OSXs. Here is a fiddle (not my own but the one where I found this answer). http://jsfiddle.net/simurai/UsvLN/
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.
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 evenscroll
rather than toggling it.I came up with this solution. Basically the negative margin cuts off the vertical scrollbar.
LESS mixin
NOTE: @padding should be at least the scrollbar width (e.g. 25px)
Basically add this to your LESS/CSS and add the class to the element that needs it's scrollbar cut off.
Try selecting the div with
:hover
selectorThe answer with changing overflow have a bunch of issues, like inconsistent width of the inner block, triggering of reflow, the need to have extra code for deal with paddings and without disabling keyboard (and, possibly, other) interactions when not hovered.
There is an easier way to have the same effect that would not trigger reflow ever: using
visibility
property and nested blocks:Here is a pen with a working example: http://codepen.io/kizu/pen/OyzGXY
Another feature of this method is that
visibility
is animatable, so we can add a transition to it (see the second example in the pen above). Adding a transition would be better for UX: the scrollbar won't appear immediately when hovered just while moving along to another element, and it would be harder to miss the scrollbar when targeting it with mouse cursor, as it won't hide immediately as well.Snippet: