Manipulate Scrollbar Colors Using CSS and JavaScript Article

Share this article

The thing about the default color of scrollbars is that it’s dull and ugly — usually this color is gray. Wouldn’t it be nice to change this color to better fit the overall theme of your site? Luckily, Cascading Style Sheets and JavaScript can be used to do just that!
Using CSS
In CSS, simply add the below definitions to the top of your page to customize the browser’s scrollbar colors. The great thing about CSS is that browsers that don’t understand it will just skip it. Scrollbar painting is supported by IE5.5 and up. <style> <!-- BODY{ scrollbar-face-color:#8080FF; scrollbar-arrow-color:#FFFFFF; scrollbar-track-color:#DDDDFF; scrollbar-shadow-color:''; scrollbar-highlight-color:''; scrollbar-3dlight-color:''; scrollbar-darkshadow-Color:''; } –> </style> Bet you never realized the scrollbar consisted of that many components! The first three definitions are the most important, as they correspond to the most visible aspects of the scrollbar. Feel free to play around with the other definitions to see what they affect.
Using JavaScript
You can also use JavaScript to dynamically change the scrollbar color. This is useful when you wish to do something fancy, like alternating the scrollbar from one color to another. The JavaScript translation of the scrollbar CSS definitions are: document.body.style.scrollbarFaceColor="colorname" document.body.style.scrollbarArrowColor="colorname" document.body.style.scrollbarTrackColor="colorname" document.body.style.scrollbarShadowColor="colorname" document.body.style.scrollbarHighlightColor="colorname" document.body.style.scrollbar3dlightColor="colorname" document.body.style.scrollbarDarkshadowColor="colorname" Here’s an example of a “blinking” scrollbar, which changes color every second: <script>
var mode=0 function blinkscroll(){ if (mode==0) document.body.style.scrollbarFaceColor=”blue” else document.body.style.scrollbarFaceColor=”green” mode=(mode==0)? 1 : 0 } setInterval(“blinkscroll()”,1000) </script> A more elaborate example of scrollbar manipulation using JavaScript, called onMouseover Scrollbar Effect, is written by Svetlin Staev. This changes the scrollbar colors when you move your mouse over and away from it. I’m seeing more and more sites customize the scrollbar color to blend in with the rest of their sites. I hope you find these tips useful in helping you do the same!

Frequently Asked Questions about CSS and JavaScript Colors

How can I change the color of the scrollbar using CSS?

Changing the color of the scrollbar using CSS is quite simple. You can use the ::-webkit-scrollbar pseudo-element to select the scrollbar, and then apply your desired styles. Here’s an example:

::-webkit-scrollbar {
width: 10px;
}

::-webkit-scrollbar-track {
background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
background: #888;
}

::-webkit-scrollbar-thumb:hover {
background: #555;
}
In this example, the scrollbar’s width is set to 10px, the track (the part that the handle slides along) is set to a light gray color, and the handle (or “thumb”) is set to a darker gray. When you hover over the handle, it changes to an even darker gray.

Can I use JavaScript to change the color of elements on a webpage?

Yes, you can use JavaScript to change the color of elements on a webpage. You can do this by accessing the style property of an element and then changing its color property. Here’s an example:

document.getElementById("myElement").style.color = "red";
In this example, the text color of the element with the id “myElement” is changed to red.

How can I use CSS to create a gradient color effect?

CSS provides a function called linear-gradient() that you can use to create a gradient color effect. Here’s an example:

background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
In this example, the background of the element will be a gradient that transitions from red to violet, moving from left to right.

Can I use JavaScript to change the background color of an element?

Yes, you can use JavaScript to change the background color of an element. You can do this by accessing the style property of an element and then changing its backgroundColor property. Here’s an example:

document.getElementById("myElement").style.backgroundColor = "blue";
In this example, the background color of the element with the id “myElement” is changed to blue.

How can I use CSS to set the opacity of an element?

CSS provides a property called opacity that you can use to set the opacity of an element. Here’s an example:

.myElement {
opacity: 0.5;
}
In this example, the element with the class “myElement” will have its opacity set to 0.5, making it semi-transparent.

Can I use JavaScript to change the opacity of an element?

Yes, you can use JavaScript to change the opacity of an element. You can do this by accessing the style property of an element and then changing its opacity property. Here’s an example:

document.getElementById("myElement").style.opacity = "0.5";
In this example, the opacity of the element with the id “myElement” is changed to 0.5, making it semi-transparent.

How can I use CSS to set the border color of an element?

CSS provides a property called border-color that you can use to set the border color of an element. Here’s an example:

.myElement {
border-color: red;
}
In this example, the element with the class “myElement” will have its border color set to red.

Can I use JavaScript to change the border color of an element?

Yes, you can use JavaScript to change the border color of an element. You can do this by accessing the style property of an element and then changing its borderColor property. Here’s an example:

document.getElementById("myElement").style.borderColor = "red";
In this example, the border color of the element with the id “myElement” is changed to red.

How can I use CSS to set the text color of an element?

CSS provides a property called color that you can use to set the text color of an element. Here’s an example:

.myElement {
color: blue;
}
In this example, the element with the class “myElement” will have its text color set to blue.

Can I use JavaScript to change the text color of an element?

Yes, you can use JavaScript to change the text color of an element. You can do this by accessing the style property of an element and then changing its color property. Here’s an example:

document.getElementById("myElement").style.color = "blue";
In this example, the text color of the element with the id “myElement” is changed to blue.

Mike ThompsonMike Thompson
View Author

Mike is an aspiring Web designer. He's currently studying full time in the Unversity of British Columbia, Canada.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form