Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style webkit scrollbar on certain state

I'd like my WebKit scrollbars to have a different color when its container is hovered over. I want the entire scrollbar to light up.

I was thinking something like this would do the trick (but it doesn't):

.scroller:hover::-webkit-scrollbar {
  background: green;
}

I've styled the scrollbars the same way: on .scroller, not globally. (That works: .scroller::-webkit-scrollbar) I want the overflowed divs special, not the document.

Another (related) problem: light up the thumb when hovering over the scrollbar. This doesn't work:

.scroller::-webkit-scrollbar:hover ::-webkit-scrollbar-thumb
like image 590
Rudie Avatar asked Dec 26 '11 01:12

Rudie


People also ask

Can you style scrollbar CSS?

In September 2018, W3C CSS Scrollbars defined specifications for customizing the appearance of scrollbars with CSS. As of 2020, 96% of internet users are running browsers that support CSS scrollbar styling. However, you will need to write two sets of CSS rules to cover Blink and WebKit and also Firefox browsers.

How do you target a scrollbar in CSS?

CSS Scrollbar Selectors You can use the following pseudo-elements to customize various parts of the scrollbar for WebKit browsers: ::-webkit-scrollbar — the entire scrollbar. ::-webkit-scrollbar-button — the buttons on the scrollbar (arrows pointing upwards and downwards that scroll one line at a time).

How do I customize my horizontal scroll bar?

Scrollbar Selectors For webkit browsers, you can use the following pseudo elements to customize the browser's scrollbar: ::-webkit-scrollbar the scrollbar. ::-webkit-scrollbar-button the buttons on the scrollbar (arrows pointing upwards and downwards). ::-webkit-scrollbar-thumb the draggable scrolling handle.


2 Answers

This is possible using pure CSS, at least with Chrome version 29.

http://jsfiddle.net/eR9SP/

To style the scrollbar when its container (in this case .scroller) is hovered over, use:

.scroller:hover::-webkit-scrollbar { /* styles for scrollbar */ }
.scroller:hover::-webkit-scrollbar-thumb { /* styles for scrollbar thumb */ }
.scroller:hover::-webkit-scrollbar-track { /* styles for scrollbar track */ }

Additionally, you can style the scrollbar thumb itself when it's hovered over or active (being clicked) using the following:

.scroller::-webkit-scrollbar-thumb:horizontal:hover,
.scroller::-webkit-scrollbar-thumb:vertical:hover { /* hover thumb styles */ }
.scroller::-webkit-scrollbar-thumb:horizontal:active,
.scroller::-webkit-scrollbar-thumb:vertical:active { /* active thumb styles */ }
like image 146
Michael Avatar answered Oct 01 '22 11:10

Michael


Changing the background color works just fine for me.

http://jsfiddle.net/QcqBM/1

div#container:hover::-webkit-scrollbar {
    background: lightyellow;
}

Are you sure there isn't something else wrong with your CSS call?

like image 24
mrtsherman Avatar answered Oct 01 '22 10:10

mrtsherman