Scrollbar customisation in CSS and JS
One day some designers provide you with beautiful mock-ups ๐ and โฆ custom macOS-like scrollbars which look like:
But maybe on the left, smaller or bigger, with different indents dependent on whether itโs mobile ๐ฑ, desktop ๐ฅ๏ธ or TV screen ๐บ Asking more? But the reality isโฆ a customisation of scrollbars remains same since the end of 90s. If my dad was programming, he would make fun of me all day while I found the solution to make everything universal and stable. Itโs 2020 and itโs still a PITA.
Native support
โ Webkit and Blink
1::-webkit-scrollbar {2 /** */3}4::-webkit-scrollbar-button {5 /** */6}7::-webkit-scrollbar-track {8 /** */9}10::-webkit-scrollbar-track-piece {11 /** */12}13::-webkit-scrollbar-thumb {14 /** */15}16::-webkit-scrollbar-corner {17 /** */18}19::-webkit-resizer {20 /** */21}
It means at least itโs working on Chrome, Safari, Opera, Edgium (Edge based on WebKit for iOS and Blink for Android, Windows and MacOS) and and less known such as Vivaldi and others.
โ Gecko
Since Firefox 64 was released, scrollbar customisation is now available.
I remind you, that original bug was reported 18 years ago, only less than 1 year ago implementation of CSS Scrollbars Module Level 1 was considered ๐.
Yet itโs not working for me when you try this chunk of CSS:
1.container {2 scrollbar-color: rebeccapurple green;3 scrollbar-width: 5px;4}
Check it in your FF, mine is not working with this.
Example: https://jsfiddle.net/beraliv/4yd7bg2r/14/
However, if you try this, it works perfect:
1.container {2 scrollbar-width: thin;3}
Mac. On the left โ original size. On the right โ thin. Now you see the difference:
Same for Windows:
More information you can find on MDN: scrollbar-width and scrollbar-color.
โ Trident-related
If you are not familiar with it, probably forget about it. Itโs IE11. Such a pain everywhere. Maybe not supporting it at all?
However, even it has CSS properties which might help:
Only colours ๐ด๐๐ ughhhโฆ
If you still need to customise colours, you can check the generator:
And also autohiding scrollbar property!!! Thanks ๐โก
Another one is EdgeHTML which is a fork of Trident and used in old Edge. It had only enhancement (which is currently unavailable) in its backlog with medium priority to add support for scrollbar styling. But since Edgium release you can use Webkit / Blink pseudo elements ๐.
CSS Hacks and tricks
Slowly but surely we achieve what we want.
Scrollbar on the left
transform
: https://jsfiddle.net/4yd7bg2r/5/ with great support
direction
: https://jsfiddle.net/4yd7bg2r/6/ with great support
Hiding scrollbar
overflow
, margin
and padding
- horizontal: https://jsfiddle.net/4yd7bg2r/8/
- vertical: https://jsfiddle.net/4yd7bg2r/7/
Turn scrollbar on with JS
Accepting the fact FF, Edge and IE do not support scrollbar customisation, JS library might be not bad solution for them.
Library-independent
Unfortunately some of libraries are plugins for jQuery: jScrollPane, nanoScroller. And this is not what can be a fit.
Bundle size
A size of scrollbar JS bundle should be worth it. However itโs not what itโs expected.
lib | minified (KB) | minified + gzipped (KB) |
---|---|---|
๐ณ๏ธ size of black hole | โ | โ |
nanoscroller@0.8.7 | 94.2 | 32.4 |
simplebar@5.3.0 | 63.5 | 19 |
iscroll@5.2.0 | 32.1 | 8.3 |
perfect-scrollbar@1.5.0 | 18.2 | 5.3 |
๐ค light enough | ||
slim-scroll@1.3.18 | 4.2 | 1.8 |
๐ฅ too good to be true | ||
simple-scrollbar@0.4.0 | 2.3 | 0.9 |
Bundlephobia helps evaluating sizes of bundles.
Of course, good options are lightweight vanilla libraries.
Mobile support
Some of choices like simple-scrollbar provide scrollbars unfriendly for mobile devices. It is required to add a lot of functionality and therefore not production-ready.
Platform-oriented
slim-scroll is created by a stack overflow user and is aimed to improve design on Windows. Thatโs good as macOS scrollbar looks good everywhere. However, native customisation is only available on WebKit and Blink.
Your implementation
Probably you want to customise you own scrollbar. First have a look at scrollbar mechanics.
Links
- โญโญ๏ธ Google Developers: CSS Deep-Dive: matrix3d() For a Frame-Perfect Custom Scrollbar
- โญโญ๏ธ๏ธ CSDGN: Scrollbar Mechanics
- โญ๏ธ CSS Tricks: Custom Scrollbars in WebKit
- ๐ MDN: CSS scrollbars
- ๐ W3School: How To Create Custom Scrollbars
- ๐ Stack Overflow: Hide scroll bar, but while still being able to scroll
- ๐ Stack Overflow: Custom CSS Scrollbar for Firefox
- ๐ Stack Overflow: CSS customized scroll bar in div
Conclusion
My last choice: to try perfect-scrollbar or simplebar.
Have a productive week ๐ช
cssjavascript