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 vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    How I Stopped WordPress Comment Spam

    I love almost every part of being a tech blogger:  learning, preaching, bantering, researching.  The one part about blogging that I absolutely loathe:  dealing with SPAM comments.  For the past two years, my blog has registered 8,000+ SPAM comments per day.  PER DAY.  Bloating my database...

Incredible Demos

  • By
    MooTools Text Flipping

    There are lots and lots of useless but fun JavaScript techniques out there. This is another one of them. One popular April Fools joke I quickly got tired of was websites transforming their text upside down. I found a jQuery Plugin by Paul...

  • By
    Dynamically Load Stylesheets Using MooTools 1.2

    Theming has become a big part of the Web 2.0 revolution. Luckily, so too has a higher regard for semantics and CSS standards. If you build your pages using good XHTML code, changing a CSS file can make your website look completely different.

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!