DEV Community

Cover image for 2 Simple Steps to Create a CSS Custom Scrollbar
Shefali
Shefali

Posted on • Edited on • Originally published at shefali.dev

2 Simple Steps to Create a CSS Custom Scrollbar

Introduction

Scrollbars are an essential component of the web page. Default scrollbars provided by web browsers are functional, but they may not always align with the overall design aesthetic of a website. Fortunately, with HTML and CSS, you can create a custom scrollbar to enhance the user experience. In this blog post, we will delve into the process of creating personalized scrollbars and even provide you with a handy code snippet to kickstart your customization journey.

Basic Structure

To begin, let’s set up the basic HTML structure so the page is large enough to show a scrollbar.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      .container {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin: 5%;
      }
      p {
        font-size: 18px;
        padding: 20px;
        width: 60%;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>CSS Custom Scrollbar</h1>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
    </div>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS Styling

Let’s add CSS to style our scrollbar.

::-webkit-scrollbar {
  width: 18px; /* Width of the scrollbar */
}

::-webkit-scrollbar-track {
  background-color: rgb(170, 167, 167); /* Color of the scrollbar track */
}

::-webkit-scrollbar-thumb {
  background-color: rgb(104, 103, 103); /* Color of the scrollbar thumb */
  border-radius: 5px;
}
Enter fullscreen mode Exit fullscreen mode

It's important to note that different web browsers may have varying support and implementation for custom scrollbars. For example: ::-webkit-scrollbar is for chrome browser and ::-moz-scrollbar is for firefox browser.It's essential to consider cross-browser compatibility.

Result:

CSS Custom Scrollbar

Putting It All Together

Here’s the complete code snippet to create a custom scrollbar using HTML and CSS.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
      }
      .container {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin: 5%;
      }
      p {
        font-size: 18px;
        padding: 20px;
        width: 60%;
      }
      ::-webkit-scrollbar {
        width: 18px; /* Width of the scrollbar */
      }

      ::-webkit-scrollbar-track {
        background-color: rgb(170, 167, 167); /* Color of the scrollbar track */
      }

      ::-webkit-scrollbar-thumb {
        background-color: rgb(104, 103, 103); /* Color of the scrollbar thumb */
        border-radius: 5px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <h1>CSS Custom Scrollbar</h1>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
      <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. Lorem ipsum, dolor sit amet consectetur
        adipisicing elit. Dolor, magni.
      </p>
    </div>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Conclusion

By customizing scrollbars using HTML and CSS, you can enhance the user experience and align the appearance of your website or application with your overall design. You can experiment with the code snippets provided in this blog post, to customize the perfect scrollbar for your project.

Thanks for reading.

For more content like this click here.

Keep coding!!

Buy Me A Coffee

Top comments (6)

Collapse
 
3aluw profile image
Abdou

I tried it on the horizontal scroll bar, it worked but its width didn't change . any solution ?

Collapse
 
devshefali profile image
Shefali

Use height for the horizontal scrollbar and width for the vertical scrollbar

Collapse
 
3aluw profile image
Abdou

That makes a lot of sense. thank you

Collapse
 
diomed profile image
May Kittens Devour Your Soul

what does this html prefix do exactly?
because this
a) does not work on Firefox at all
b) works perfectly fine without html added before webkit in chromium based browsers

Collapse
 
delaterra profile image
Lucas Delaterra Ignes

Html prefix is to reference the html tag, so the entire document. I believe the author used it to improve compatibility.

This mozilla doc explains it better: developer.mozilla.org/en-US/docs/W...

Collapse
 
diomed profile image
May Kittens Devour Your Soul

how does this improve compatibility, when firefox doesn't at all pick up on this?
-webkit is for chromium based browsers