Article

Home » Client-side Coding » JavaScript & Ajax Tutorials » Manipulate Scrollbar Colors Using CSS and JavaScript

About the Author

Mike Thompson

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

View all articles by Mike Thompson...

Manipulate Scrollbar Colors Using CSS and JavaScript

By Mike Thompson

January 16th, 2002

Reader Rating: 7.5

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!

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Comment on This Article

Have something to say?

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: