Tutorial

How To Customize the Browser's Scrollbar with CSS

Updated on September 15, 2020
author

Akinjide Bankole

How To Customize the Browser's Scrollbar with CSS

Introduction

Custom scrollbars are getting popular, and you might have come across websites that have unique scrollbars, making the sites feel and look different. There are basically a few ways to implement a custom scrollbar.

In this tutorial we will be using CSS3, which is the most straightforward way. However, there are jQuery plugins that can help with customizing scrollbar, but I do not like to add more JavaScript to my website. If you are a designer, photographer or you just want your website to have a cool scrollbar, go ahead with the jQuery plugins.

Understanding Scrollbar Terminologies

Custom scrollbars are exposed behind the -webkit vendor prefix for use in browsers using the Webkit (and Blink) rendering engine.

Screenshot depicting the scrollbar-thumb and scrollbar-track parts to a scrollbar.

-webkit-scrollbar consists of seven different pseudo-elements, and together comprise a full scrollbar UI element:

  1. ::-webkit-scrollbar the background of the bar itself.
  2. ::-webkit-scrollbar-button the directional buttons on the scrollbar.
  3. ::-webkit-scrollbar-track the empty space “below” the progress bar.
  4. ::-webkit-scrollbar-track-piece the top-most layer of the the progress bar not covered by the thumb.
  5. ::-webkit-scrollbar-thumb the draggable scrolling element resizes depending on the size of the scrollable element.
  6. ::-webkit-scrollbar-corner the bottom corner of the scrollable element, where two scrollbar meet.
  7. ::-webkit-resizer the draggable resizing handle that appears above the scrollbar-corner at the bottom corner of some elements.

Now that you are familiar with the terminologies, let’s start!

Setting Up the Project

First, create index.html and style.css files, and open the current directory with your code editor.

index.html
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8" />
		<title>Customize the Browser's Scrollbar with CSS</title>
		<link type="text/css" rel="stylesheet" href="styles.css">
	</head>
	<body>
		<div class="scrollbar" id="style-1">
			<div class="force-overflow"></div>
		</div>
	</body>
</html>

You’ll need to include the style.css file in the HTML file. You can type out the above HTML code or just copy and paste into your HTML file.

Firstly, we set the .scrollbar (class) width, height, background-color, then set overflow-y: scroll to get the vertical scrollbar. We set min-height: 450px to the .force-overflow div so that the scrollbar appears (because we used the overflow-y property to scroll in .scrollbar class).

styles.css
.scrollbar {
	background-color: #F5F5F5;
	float: left;
	height: 300px;
	margin-bottom: 25px;
	margin-left: 22px;
	margin-top: 40px;
	width: 65px;
	overflow-y: scroll;
}

.force-overflow {
	min-height: 450px;
}

Screenshot depicting the current default appearance of the scrollbar.

Now, we use the scrollbar pseudo-element for creating our custom scrollbar. It will replace its default width with a new width of 6px and then add background-color: #F5F5F5.

styles.css
#style-1::-webkit-scrollbar {
	width: 6px;
	background-color: #F5F5F5;
}

Since we know that scrollbar contains track, button and thumb, we are now going to give a stylish look to thumb. We use pseudo-element (i.e., ::-webkit-scrollbar-thumb) with -webkit prefix and set scrollbar-thumb background- color.

styles.css
#style-1::-webkit-scrollbar-thumb {
	background-color: #000000;
}

After that, the scrollbar looks like this:

Screenshot of the modified scrollbar with a black scrollbar-thumb.

We use box-shadow on scrollbar-track to make it stylish and add contrast between the scrollbar and scrollbar-track.

styles.css
#style-1::-webkit-scrollbar-track {
	-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
	background-color: #F5F5F5;
}

Screenshot of the modified scrollbar with a black scrollbar-thumb and light-colored scrollbar-track.

Conclusion

In this article, we covered:

  • Customized scrollbars aren’t uncommon anymore. You will find them in major websites and blogs, especially personal portfolio websites.
  • There are tons of jQuery plugins that can help with customizing scrollbar.
  • Custom scrollbars are exposed behind the -webkit vendor prefix.
  • Basic structure of a scrollbar.
  • Terminologies associated with scrollbars.

The CSS way of customizing scrollbars is simple, but looks a bit rough. However, operating systems like Windows, OS X and Linux have their own style for the scrollbar. This in return could lead to undesirable results and inconsistencies for your design. Remember, you should keep it simple, stupid (KISS), not too fancy.

I have created more scrollbars using the above procedure. I made use of codePen for the demo, and you can find the full code for this tutorial on CodePen.

Screenshot of a demo with many customized scrollbars in different colors and sizes.

That’s all. I hope you like it!

Make sure to leave any thoughts, questions or concerns in the comments below. I would love to see them.

Acknowledgements

@twholman Codepen

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Akinjide Bankole

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more