Styling scrollbars with CSS

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks Sudhir, I'm glad you like my articles 🤩
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday we looked at removing the scrollbars for certain areas. In regards to that, let's look at how we can style scrollbars with CSS.
Styled scrollbars are actually pretty rare. I've only seen them on a couple of sites so far.
Some examples of websites using custom scrollbars:
It's somewhat quite weird that we don't see more custom scrollbars, as they can really help to enhance your design.
The scrollbar was a thing that's always been very hard to style, like select elements. However, we got some recent additions to CSS that give us scrollbar-color and scrollbar-width.
Let's see how we can give the best support for a styled scrollbar.
When styling the scrollbar, it's important to use both the WebKit styles and the newer scrollbar styles.
:root {
--scrollbarWidth: 12px;
--scrollbarBg: rgb(99, 102, 241);
--scrollbarThumb: rgb(244, 63, 94);
}
.container {
scrollbar-width: var(--scrollbarWidth);
scrollbar-color: var(--scrollbarThumb) var(--scrollbarBg);
}
.container::-webkit-scrollbar {
width: var(--scrollbarWidth);
}
.container::-webkit-scrollbar-track {
background: var(--scrollbarBg);
}
.container::-webkit-scrollbar-thumb {
background-color: var(--scrollbarThumb);
border-radius: 6px;
border: 3px solid var(--scrollbarBg);
}
Alright, let's go through these elements and see what happens for each.
We start by defining some CSS variables so we can re-use the same styles.
:root {
--scrollbarWidth: 12px;
--scrollbarBg: rgb(99, 102, 241);
--scrollbarThumb: rgb(244, 63, 94);
}
I wrote a more detailed article on CSS variables as well.
The next element is the newer scrollbar options we get.
.container {
scrollbar-width: var(--scrollbarWidth);
scrollbar-color: var(--scrollbarThumb) var(--scrollbarBg);
}
It's new, and so far, I found only Chrome supporting it, but you need to turn on some settings for it.
The values you see for the scrollbar-color or as follows:
Note: It supports other colors, like:
scrollbar-color: dark|light, but so far, no browser supports this.
This is where our fallback prefixes come in place:
.container::-webkit-scrollbar {
width: var(--scrollbarWidth);
}
.container::-webkit-scrollbar-track {
background: var(--scrollbarBg);
}
.container::-webkit-scrollbar-thumb {
background-color: var(--scrollbarThumb);
border-radius: 6px;
border: 3px solid var(--scrollbarBg);
}
These will make sure the scrollbar renders in all the other modern browsers. We can use the main scrollbar prefix to define the width of the scrollbar.
Next up, we can style the track, which acts as the background. Then lastly, we have the option to style the thumb. This is the actual sliding bit. In my example, I use a border-radius and border to offset it from the sides a bit.
And that's it, you now have a styled scrollbar!
Check out the demo I made on Codepen.
Unfortionally the scrollbar-color and scrollbar-width are not supported well.

The WebKit prefix however, is supported more widely. You can see a combination of both can support quite a lot of browsers.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter