Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom scrollbar only in one div

I want to have a custom scroll bar in Chrome.

So, I'm using this sass:

::-webkit-scrollbar {
  width: 0.5em;
  height: 0.5em;
}

::-webkit-scrollbar-thumb {
  background-color: rgba(255,255,255,.1);
  border-radius: 3px;

  &:hover {
    background: rgba(255,255,255,.2);
  }
}

My problem is that I want this scrollbar style only in an specific div. But if I do:

#boardslist {

  ::-webkit-scrollbar {
    width: 0.5em;
    height: 0.5em;
  }

  ::-webkit-scrollbar-thumb {
    background-color: rgba(255,255,255,.1);
    border-radius: 3px;

    &:hover {
      background: rgba(255,255,255,.2);
    }
  }

}

is not working. Any ideas?

like image 618
Mati Tucci Avatar asked Jun 25 '16 00:06

Mati Tucci


People also ask

How do I get a scrollbar for a div?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do I style a specific scrollbar?

For webkit browsers, you can use the following pseudo elements to customize the browser's scrollbar: ::-webkit-scrollbar the scrollbar. ::-webkit-scrollbar-button the buttons on the scrollbar (arrows pointing upwards and downwards). ::-webkit-scrollbar-thumb the draggable scrolling handle.

How do I create a custom scrollbar for a div?

If you want to add a scrollbar to an element like a <div>, you'll have to use the CSS overflow property. Scrollbars can be vertical (Y-axis) or horizontal (X-axis). Implementing a custom scrollbar to elements is the same. In this example, I'll create vertical custom scrollbar on a <div>, which is just 5px wide.

How do I show the scroll bar only when needed CSS?

Use overflow: auto . Scrollbars will only appear when needed. (Sidenote, you can also specify for only the x, or y scrollbar: overflow-x: auto and overflow-y: auto ).


4 Answers

#boardslist {

  &::-webkit-scrollbar {
   width: 0.5em;
   height: 0.5em;
  }

  &::-webkit-scrollbar-thumb {
   background-color: rgba(255,255,255,.1);
   border-radius: 3px;

   &:hover {
    background: rgba(255,255,255,.2);
   }
  }
}

Check this out http://codepen.io/tholman/pen/tldwm

like image 99
jonidelv Avatar answered Sep 29 '22 21:09

jonidelv


If you are doing it for a class, you can do it in the following way. Side bar is the class given to the div that you want to allow scrolling.

.side_bar{
    padding-right: 20px; 
    margin-left: -12px;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: scroll;
    /* width */

}
.side_bar::-webkit-scrollbar {
        width: 5px;
    }

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

    /* Handle */
.side_bar::-webkit-scrollbar-thumb {
        background: rgb(7, 7, 7); 
        border-radius: 10px;
    }

    /* Handle on hover */
.side_bar::-webkit-scrollbar-thumb:hover {
        background: #009eb3; 
    }
like image 28
Haris Np Avatar answered Sep 29 '22 23:09

Haris Np


It's 2020 and ::-webkit-scrollbar is still not supported in Firefox. Besides overflow:auto shows scrollbar when content overflows in Chrome and Safari, but in Firefox there will be no visible scrollbar until we start scrolling. Go guess whether content is scrollable or not. Same with overflow:scroll. Not very intuitive.

like image 40
Alonad Avatar answered Sep 29 '22 23:09

Alonad


Well, 2022 and still no support in Firefox:
https://caniuse.com/?search=%3A%3A-webkit-scrollbar

However, basics can be customized in Firefox:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scrollbars
https://caniuse.com/?search=scrollbar-color

/* Chrome */
.container::-webkit-scrollbar {
    width: 5px;
}

.container::-webkit-scrollbar-track {
  /*background-color: grey;*/
  box-shadow: inset 0 0 5px grey; 
  border-radius: 15px;
}

.container::-webkit-scrollbar-thumb {
  background-color: orange; 
  border-radius: 15px;
  /*border: 1px solid red;*/
}

/*.container::-webkit-scrollbar-button {
  background-color: red;
   border-radius: 15px;
}*/

.container::-webkit-scrollbar-thumb:hover {
  background: red; 
}

/* IE */
.container {
  scrollbar-face-color: orange; 
  scrollbar-shadow-color: grey; 
  scrollbar-highlight-color: red;
}

/* FireFox */
.container {
  scrollbar-color: orange grey;
  scrollbar-width: thin;
}

/* View Scrollbar */
.container {
  overflow-y: scroll;
  overflow-x: hidden;
  width:400px;
  height: 200px;
}

Working example is here:
https://codepen.io/svigir/pen/LYOOjyj

like image 1
Ramirez Avatar answered Sep 29 '22 23:09

Ramirez