{"id":9130,"date":"2011-05-02T06:01:34","date_gmt":"2011-05-02T13:01:34","guid":{"rendered":"http:\/\/css-tricks.com\/?p=9130"},"modified":"2019-07-15T06:29:22","modified_gmt":"2019-07-15T13:29:22","slug":"custom-scrollbars-in-webkit","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/custom-scrollbars-in-webkit\/","title":{"rendered":"Custom Scrollbars in WebKit"},"content":{"rendered":"

Way back in the day, you could customize scrollbars in IE (e.g. v5.5) with non-standard CSS properties like scrollbar-base-color<\/code> which you would use on the element that scrolls (like the <body><\/code>) and do totally rad things<\/a>. IE dropped that.<\/p>\n

These days, customizing scrollbars is back, but it’s WebKit this time. It’s a bit better now, because the properties are vendor-prefixed (e.g. ::-webkit-scrollbar<\/code>) and use the “Shadow DOM<\/a>“. This has been around for a couple of years. David Hyatt blogged it<\/a> in early 2009.<\/p>\n

The Goods<\/h3>\n

The Different Pieces<\/h4>\n

These are the pseudo-elements themselves. The actual parts of the scrollbars.<\/p>\n

::-webkit-scrollbar              { \/* 1 *\/ }\r\n::-webkit-scrollbar-button       { \/* 2 *\/ }\r\n::-webkit-scrollbar-track        { \/* 3 *\/ }\r\n::-webkit-scrollbar-track-piece  { \/* 4 *\/ }\r\n::-webkit-scrollbar-thumb        { \/* 5 *\/ }\r\n::-webkit-scrollbar-corner       { \/* 6 *\/ }\r\n::-webkit-resizer                { \/* 7 *\/ }<\/code><\/pre>\n
\"\"<\/figure>\n

The Different States<\/h4>\n

These are the pseudo-class selectors. They allow for more specific selection of the parts, like when the scrollbar is in different states.<\/p>\n

:horizontal\r\n:vertical\r\n:decrement\r\n:increment\r\n:start\r\n:end \r\n:double-button\r\n:single-button\r\n:no-button\r\n:corner-present\r\n:window-inactive<\/code><\/pre>\n

I’m going to steal this whole section from David’s blog post<\/a> on the WebKit blog because it explains each part well:<\/p>\n

:horizontal<\/strong> \u2013 The horizontal pseudo-class applies to any scrollbar pieces that have a horizontal orientation.<\/p>\n

:vertical<\/strong> \u2013 The vertical pseudo-class applies to any scrollbar pieces that have a vertical orientation.<\/p>\n

:decrement<\/strong> \u2013 The decrement pseudo-class applies to buttons and track pieces. It indicates whether or not the button or track piece will decrement the view\u2019s position when used (e.g., up on a vertical scrollbar, left on a horizontal scrollbar).<\/p>\n

:increment<\/strong> \u2013 The increment pseudo-class applies to buttons and track pieces. It indicates whether or not a button or track piece will increment the view\u2019s position when used (e.g., down on a vertical scrollbar, right on a horizontal scrollbar).<\/p>\n

:start<\/strong> \u2013 The start pseudo-class applies to buttons and track pieces. It indicates whether the object is placed before the thumb.<\/p>\n

:end<\/strong> \u2013 The end pseudo-class applies to buttons and track pieces. It indicates whether the object is placed after the thumb.<\/p>\n

:double-button<\/strong> \u2013 The double-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is part of a pair of buttons that are together at the same end of a scrollbar. For track pieces it indicates whether the track piece abuts a pair of buttons.<\/p>\n

:single-button<\/strong> \u2013 The single-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is by itself at the end of a scrollbar. For track pieces it indicates whether the track piece abuts a singleton button.<\/p>\n

:no-button<\/strong> \u2013 Applies to track pieces and indicates whether or not the track piece runs to the edge of the scrollbar, i.e., there is no button at that end of the track.<\/p>\n

:corner-present<\/strong> \u2013 Applies to all scrollbar pieces and indicates whether or not a scrollbar corner is present.<\/p>\n

:window-inactive <\/strong>\u2013 Applies to all scrollbar pieces and indicates whether or not the window containing the scrollbar is currently active. (In recent nightlies, this pseudo-class now applies to ::selection as well. We plan to extend it to work with any content and to propose it as a new standard pseudo-class.)<\/p><\/blockquote>\n

All together now<\/h4>\n

These pseudo elements and pseudo class selectors work together. Here are some random examples:<\/p>\n

::-webkit-scrollbar-track-piece:start {\r\n   \/* Select the top half (or left half) or scrollbar track individually *\/\r\n}\r\n\r\n::-webkit-scrollbar-thumb:window-inactive {\r\n   \/* Select the thumb when the browser window isn't in focus *\/\r\n}\r\n\r\n::-webkit-scrollbar-button:horizontal:decrement:hover {\r\n   \/* Select the down or left scroll button when it's being hovered by the mouse *\/\r\n}<\/code><\/pre>\n

Very Simple Example<\/h3>\n

To make a really simple custom scrollbar we could do this:<\/p>\n

::-webkit-scrollbar {\r\n    width: 12px;\r\n}\r\n \r\n::-webkit-scrollbar-track {\r\n    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); \r\n    border-radius: 10px;\r\n}\r\n \r\n::-webkit-scrollbar-thumb {\r\n    border-radius: 10px;\r\n    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); \r\n}<\/code><\/pre>\n

In which we’d get this on a simple div with vertically overflowing text:<\/p>\n

\"\"<\/figure>\n

In The Wild<\/h3>\n

Check out the very subtle and nice scrollbars on Tim Van Damme’s blog Maxvoltar<\/a> (Update September 2012<\/strong>: Tim’s site no longer uses this design):<\/p>\n

\"\"<\/p>\n

The particularly nice bit here is that the scrollbar is on the body element, yet the scrollbar isn’t stuck to the top, bottom, or right edge of the browser window as scroll bars normally are. I made a test page with copy-and-pasteable code to achieve that a similar effect:<\/p>\n

View Demo<\/a><\/p>\n

On Forrst<\/a>, they use custom scollbars on code snippets which are also pretty nice. They are less visually intense and so don’t fight as much with the code highlighting.<\/p>\n

\"\"<\/figure>\n

Related<\/h3>\n