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
    Being a Dev Dad

    I get asked loads of questions every day but I'm always surprised that they're rarely questions about code or even tech -- many of the questions I get are more about non-dev stuff like what my office is like, what software I use, and oftentimes...

  • By
    Page Visibility API

    One event that's always been lacking within the document is a signal for when the user is looking at a given tab, or another tab. When does the user switch off our site to look at something else? When do they come back?

Incredible Demos

  • By
    Highlight Table Rows, Columns, and Cells Using MooTools 1.2.3

    Row highlighting and individual cell highlighting in tables is pretty simple in every browser that supports :hover on all elements (basically everything except IE6). Column highlighting is a bit more difficult. Luckily MooTools 1.2.3 makes the process easy. The XHTML A normal table. The cells...

  • By
    dat.gui:  Exceptional JavaScript Interface Controller

    We all love trusted JavaScript frameworks like MooTools, jQuery, and Dojo, but there's a big push toward using focused micro-frameworks for smaller purposes. Of course, there are positives and negatives to using them.  Positives include smaller JS footprint (especially good for mobile) and less cruft, negatives...

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!