WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe

Newletters


Custom Scrollbars with CSS

By

Social Bookmark

Web Production Manager
Targus
US-CA-Santa Monica

Justtechjobs.com Post A Job | Post A Resume


Web designers are known to be fussy about everything. They want to have control over the look of everything that goes on the website. From the height and width of the window, to the anti-aliasing of fonts, to the way buttons and scrollbars are rendered. Before CSS was introduced, you had to use images for the simplest of things like giving a border to a box, etc. Web designers dreamed of a day when they could completely customize how input fields and buttons were rendered. Customizing scrollbars was beyond their imagination.

Years passed and we moved into 2010. Following CSS 1 and 2, CSS 3 is well on its way and promises some amazing features that will appeal to web designers. Among other features, it introduces scrollable divs. While this allowed designers to design fixed height websites with the content scrolling inside a div; they now want to be able to style the scrollbar to match the look of the site. Doing this can be tricky, but the results are remarkable.

There are several implementations of custom scrollbars, but the following implementation is my favorite for the following reasons:



An Example



Here is an example of the scrollbar at work.



Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

How To Use It

You'll be surprised to see how easy it is to use this scrollbar. All you need to do is include a few files and then wrap the scrollable content into a div.

1.  First, you need to download and include the following javascript and CSS files: prototype.js, slider.js, scroller.js and scroll.css.

2.  Then, you add the following lines of code wherever you want the scrollbar to appear. As I mentioned earlier, you need to wrap the content into a div in the following manner:

The important part is to give the div the makeScroll class. You should specify the height and width of the div; otherwise, it will take the maximum height and width available. The inner div in the code above is optional. I use it to give some space between the scroller and the text.

Customize It

The scroller has three parts to it and the style of each of them can be customized. The parts are:

Depending on the design, sometimes you don't want the scroll buttons to display. The user scrolls by dragging the scroll handle. In such cases, you add display: none !important to both classes.

You may also enjoy:

Conclusion

This implementation of the scroller is the only one that I have come across that supports the mouse scroll wheel. There were a few others that claimed to support it, but either they are much too complex to use or they don't work on all platforms. The only shortcoming with this implementation is that it doesn't support horizontal scrolling and it doesn't automatically scroll when you are selecting the text. But in most cases, you need vertical scrolling, and it does a great job of that.


Recent Articles

WebReference.com site name
Creating an ASP.NET Registration and Confirmation System
Combine Ajax and JSON to Transmit Complex Presentation Data
Ten Tools Web Developers Can't Live Without
internet.com site name
A Simpler Solution for VoIP Phone Calls?
Review: Dropcam Echo
AMD Unleashes Six-Core Desktop CPU


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers