Custom Scrollbars in WebKit

By  on  

Before each of the browser vendors we like was providing unique CSS controls, Internet Explorer was setting the tone.  One such example is IE's early implementation of CSS filters. Internet Explorer was also the first browser that allowed developers to, for better or worse, customize the display of scrollbars.  We had a laugh about scrollbar customization back then, but fast forward to today and WebKit allows us to customize the scrollbar with CSS too.  Let's have a look!

There are a variety of scrollbar properties to choose from, all which must be -webkit prefixed:

  • scrollbar
  • scrollbar-button
  • scrollbar-track
  • scrollbar-track-piece
  • scrollbar-thumb
  • scrollbar-corner
  • resizer

Even better is that there are a number of pseudo classes which can be used.  From the WebKit blog post:

:horizontal - The horizontal pseudo-class applies to any scrollbar pieces that have a horizontal orientation.

:vertical - The vertical pseudo-class applies to any scrollbar pieces that have a vertical orientation.

:decrement - The decrement pseudo-class applies to buttons and track pieces. It indicates whether or not the button or track piece will decrement the view's position when used (e.g., up on a vertical scrollbar, left on a horizontal scrollbar).

:increment - The increment pseudo-class applies to buttons and track pieces. It indicates whether or not a button or track piece will increment the view's position when used (e.g., down on a vertical scrollbar, right on a horizontal scrollbar).

:start - The start pseudo-class applies to buttons and track pieces. It indicates whether the object is placed before the thumb.

:end - The end pseudo-class applies to buttons and track pieces. It indicates whether the object is placed after the thumb.

:double-button - The double-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is part of a pair of buttons that are together at the same end of a scrollbar. For track pieces it indicates whether the track piece abuts a pair of buttons.

:single-button - The single-button pseudo-class applies to buttons and track pieces. It is used to detect whether a button is by itself at the end of a scrollbar. For track pieces it indicates whether the track piece abuts a singleton button.

:no-button - Applies to track pieces and indicates whether or not the track piece runs to the edge of the scrollbar, i.e., there is no button at that end of the track.

:corner-present - Applies to all scrollbar pieces and indicates whether or not a scrollbar corner is present.

:window-inactive - Applies to all scrollbar pieces and indicates whether or not the window containing the scrollbar is currently active. (In recent nightlies, this pseudo-class now applies to ::selection as well. We plan to extend it to work with any content and to propose it as a new standard pseudo-class.)

In addition the :enabled, :disabled, :hover and :active pseudo-classes also work with scrollbar pieces.

A simple example of a custom scrollbar implementation would be:

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

::-webkit-scrollbar-track {
	background: none;
}

::-webkit-scrollbar-thumb {
	background: -webkit-linear-gradient(left, #547c90, #002640);
	border: 1px solid #333;
	box-shadow: inset 1px 0 0 rgba(255, 255, 255, 0.4);
}

Using CSS gradients with the scrollbar provides an excellent opportunity to class up the scrollbar -- just take a look at how CodePen uses them within their scrollbars. Note that some properties are limited; for example, opacity cannot be applied to scrollbar properties.

Customizing scrollbars is a bit of a conundrum.  On one hand, it can enrich and help brand a given site.  On the other hand, some may see it as a "cheap trick" or Busch League  effect.  What do you think?

Recent Features

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

Incredible Demos

  • By
    jQuery UI DatePicker:  Disable Specified Days

    One project I'm currently working on requires jQuery. The project also features a datepicker for requesting a visit to their location. jQuery UI's DatePicker plugin was the natural choice and it does a really nice job. One challenge I encountered was the...

  • By
    Duplicate the jQuery Homepage Tooltips Using Dojo

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: Here's how to accomplish this same effect using Dojo. The XHTML The above HTML was taken directly from the jQuery homepage -- no changes. The CSS The above CSS has been slightly modified to match the CSS rules already...

Discussion

  1. hehehe, I have the same feeling about customizing scrollbars.. or changing the mouse cursor image,etc : those are kind of tricks that most of the time only will make the user annoyed.

    But however, good to be aware of it in case a customer demands it

  2. James Tracy

    I had noticed that GMail, for example, has been using customized scrollbars for some time now.

  3. I always think it cheapens a brand rather than enhancing it, unless done very subtly. More likely to piss off/confuse users too, but helpful to be aware of the possibilities!

  4. jagz

    how to deal with ie browsers so that we can make custom scrollbar styling

  5. How to put a scrollbar is only on the particular widget and not to scroll the browser?

    thank you

  6. Just wanted to share that ::-webkit-scrollbar does not work in Safari (8) when you also use webkit-transform: translate3d(0, 0, 0); This caused me a lot of headache so hopefully this helps someone.

  7. What a fun plugin. I just installed it. Is there any way to control the opacity – of the color of the scrollbar when you aren’t hovered over it? Thanks for this post! Custom Scrollbar WordPress is a jQuery custom scrollbar for your wordpress website. This plugin will enable awesome custom scrollbar.

  8. var mean = require("meanjs")

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!