This tutorial will teach you how to create your custom scrollbar using only CSS.

But first, why should you create a custom scrollbar?

The answer is quite simple - the default scroll bar is ugly and does not go with the theme of any website! Moreover, a custom scroll adds uniqueness to your website.

You can see the difference for yourself!

Scrollbar comparison.png

Few things to know about Scrollbars

Before customizing our scrollbar, we should know some technical terms related to scrollbars. Don't worry, there are only two things we are concerned with most of the time - the scrollbar track and the scrollbar thumb

Scrollbar parts.png

  • Scrollbar Track: In simple words, it is the background of the scrollbar
  • Scrollbar Thumb: It is the button that we click and drag to scroll. It indicates the current scroll position.

Let's begin!

  1. Open up your index.css file

  2. Change the width of your scrollbar: I feel that the default scroll bar is too wide. So if you want to change the width of your scrollbar, add the following syntax to your code. I found 10px is the sweet spot.

    ::-webkit-scrollbar {
    width: 10px; 
    }
    
  3. Change the color of the track: Add the following code to change the track color of the scrollbar. I find a darker version of white or a light version of your primary color works best for this.
    ::-webkit-scrollbar-track {
    background-color: whitesmoke;
    }
    
  4. Change the color and border radius of the thumb: Add the following code to change the color of the thumb of your scrollbar, usually, to the primary color of your site.
    ::-webkit-scrollbar-thumb {
    background-color: lightgreen;
    border-radius: 0.5rem;
    }
    
  5. And you're done!

Alternatively, you can play around with the CSS in this Codepen and just copy the code to get your own custom scrollbar!

<iframe height="300" style="width:100%" src="https://codepen.io/abirsantra/embed/zYWMRgQ?default-tab=html%2Cresult&theme-id=dark"> See the Pen Untitled by AbirSantra (@abirsantra) on CodePen. </iframe>

That's it for this one. Happy Coding!

Logo

React社区为您提供最前沿的新闻资讯和知识内容

更多推荐