1

In my application, where ever the scrollbar comes it should be changed to the custom scrollbar.Is there any way in css to style the scrollbar.If not,Please provide me any suggestions regarding the customizing the scrollbar.

I had seen the jquery plugins for the custom scrollbar,but I need to change the each place for the custom scrollbar .Instead,is there any way such that changing in one place should affect the all places where scrollbar will come in my application.

3 Answers 3

7

I've developed a custom scrollbar library named SimpleScrollbar.

It does not depend on any other library/framework, and it's less than 1KB after gzip and minification.

It uses the native scroll, so there are no hacks, and the performance is awesome.


You only need to include the library in your page, and use the ss-container attribute in any div that you want to make scrollable. Live example:

<link href="http://buzinas.github.io/simple-scrollbar/simple-scrollbar.css" rel="stylesheet"/>
<div style="height: 180px; width: 200px; display: inline-block;" ss-container>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing 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.</p>
</div>
<section style="height: 180px; width: 300px; display: inline-block;" ss-container>
  <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
      <p>But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?</p>
</section>
<!--[if IE 9]><script src="https://cdn.jsdelivr.net/classlist/2014.01.31/classList.min.js"></script><![endif]-->
<script src="http://buzinas.github.io/simple-scrollbar/simple-scrollbar.min.js"></script>


It works on all the modern browsers (Firefox, Chrome, Opera, Safari, Edge), and on IE10 and IE11. You can also use it in IE9 by including a classList polyfill.

All Android, iOS and Windows Phone browsers are supported either.


If you want to automatically find out all the elements that already have a scrollbar, you can do something like that:

// After all the content in your page has loaded
document.addEventListener('DOMContentLoaded', function() {
  // Get all elements in your page
  var elements = document.querySelectorAll('*'), el;
  for (var i = 0; i < elements.length, el = elements[i]; i++) {
    // If the scrollheight is higher than the clientheight, it's because it has a scrollbar
    if (el.scrollHeight > el.clientHeight) {
      SimpleScrollbar.initEl(el); // turn the element into a SimpleScrollbar one
    }
  }
});
3
  • 1
    Would SimpleScrollbar also support to not show a scrollbar at all (but retain the functionality when using the mousewheel), and instead display two buttons (to go up/down one element and/or page), like Sly can? I would prefer to use yours, as I really like that it does not depend on jQuery.
    – domsson
    Commented Jun 25, 2017 at 13:49
  • You can do that by overwriting its CSS, e.g: .ss-container:hover .ss-scroll { opacity: 0; }.
    – Buzinas
    Commented Jun 26, 2017 at 14:56
  • You can also hide the native scroll bars by setting CSS scrollbar-width:none; on the scrollable container. With that solution at least for scrollbar-less containers you don't need a whole extra library. But I still think SimpleScrollbar is a nice thing and I'll try it out myself for my online OS simulation.
    – Lampe2020
    Commented Oct 13, 2023 at 9:15
2

You can style the scrollbar in WebKit and older IEs.

To affect all scroll bars, you should be able to use a selector such as body, textarea, .any-other-element-with-overflow-scroll.

Please consider the possible usability issues when using custom scroll bars.

6
  • How will be the browser support.If I use the webkit styling
    – Bhaskar
    Commented Sep 9, 2011 at 4:42
  • @Bhaskar Devices that use the WebKit rendering engine. That includes Google Chrome and Safari.
    – alex
    Commented Sep 9, 2011 at 4:44
  • Is there any way for styling in Firefox
    – Bhaskar
    Commented Sep 9, 2011 at 4:45
  • @Bhaskar Nope, but people have wanted it since 2001. You could use a JavaScript fallback.
    – alex
    Commented Sep 9, 2011 at 4:47
  • Thanks alex for your information,I will try to check any plugin is there for customizing the scrollbar like jscrollpane(jquery plugin)
    – Bhaskar
    Commented Sep 9, 2011 at 4:51
1

You can style all the scrollbars by using webkit

::-webkit-scrollbar {
  width: 20px;
}

/* Track */
::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px grey; 
  border-radius: 10px;
}

/* Handle */
::-webkit-scrollbar-thumb {
  background: red; 
  border-radius: 10px;
}

/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
  background: #b30000; 
}

The above code will change all the scrollbars in your application. You just need to add it somewhere

Here is the demo of the same in codepen

https://codepen.io/DineshNadimpalli/pen/WNNabZM

Not the answer you're looking for? Browse other questions tagged or ask your own question.