Treehouse

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?

ydkjs-4.png

Recent Features

  • Responsive Images: The Ultimate Guide

    Chances are that any Web designers using our Ghostlab browser testing app, which allows seamless testing across all devices simultaneously, will have worked with responsive design in some shape or form. And as today’s websites and devices become ever more varied, a plethora of responsive images...

  • 5 More HTML5 APIs You Didnât Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • Digg-Style Dynamic Share Widget Using the Dojo Toolkit

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

  • Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

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

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