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
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    Build a Calendar Using PHP, XHTML, and CSS

    One of the website features my customers love to provider their web users is an online dynamic calendar. An online calendar can be used for events, upcoming product specials, memos, and anything else you can think of. I've taken some time to completely...

  • By
    Create a 3D Panorama Image with A-Frame

    In the five years I've been at Mozilla I've seen some awesome projects.  Some of them very popular, some of them very niche, but none of them has inspired me the way the MozVR team's work with WebVR and A-Frame project have. A-Frame is a community project...

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!