Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to set the width of webkit-scrollbar-thumb

Tags:

css

webkit

I am trying to style up a custom scrollbar that works in Chrome and Safari. I’ve tried the following CSS:

::-webkit-scrollbar {     width: 15px; }  ::-webkit-scrollbar-track {     background-color: rgba(100, 100, 100, .5);     width: 15px; }  ::-webkit-scrollbar-thumb {     background-color: #818B99;     border-radius: 9px;     width: 9px; } 

However the width on the -webkit-scrollbar-thumb is being ignored. I would like the thumb to be thinner than the track, as above.

It appears that I can fake the width by setting a border on the -webkit-scrollbar-thumb of 3px with the same colour as the track, however this only works with an opaque background colour, and I need it to work with a transparent colour for the track as above.

Example jsfiddle: http://jsfiddle.net/L6Uzu/1/

like image 474
magritte Avatar asked May 29 '13 16:05

magritte


People also ask

What is thumb in scrollbar?

a box or “thumb” within a scrollbar that allows the user to move to a specific region by dragging the box to the appropriate location within the scrollbar. Also called a “scrollbox” or “elevator”.

What is thumb size in scrollbar?

the height of the thumb is based in the size of content, you can change the width inside the ::-webkit-scrollbar but the height will always be based on the content.

How do I change scroll bar settings?

Double-click/tap on ScrollHeight. The Edit String window will open with the default value of -255. That is the standard size of the scrollbars you see on Windows. To change the Scrollbars height to your liking, enter a value between -120 (smaller) to -1500 (larger) for the size you want, and click/tap on OK.


1 Answers

You are correct that WebKit ignores the width declaration on the scrollbar-thumb. It also ignores it on the track.

The problem with using a border is that it draws the border within the thumb, so if you just make the colour transparent, it will not work.

The solution is to change where the background is attached. By default the background extends under the border, as mentioned. You can use the background-clip property to change this, so that it only extends to the edge of the padding box instead. Then you just need to make a 3px transparent border, then Bob’s your uncle:

::-webkit-scrollbar-thumb {     background-color: #818B99;     border: 3px solid transparent;     border-radius: 9px;     background-clip: content-box; } 

See http://jsfiddle.net/L6Uzu/3/

This works correctly in Chrome, but Safari doesn’t have as advanced a border-radius implementation, so the rounded corners are not visible.

like image 155
David Storey Avatar answered Oct 08 '22 12:10

David Storey