62

How can I style webkit scrollbars with CSS3?

I mean these properties:

::-webkit-scrollbar {
    width: 12px;
}
                    
::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius: 10px;
}
                    
::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
}

Here is my div tag:

<div class="scroll"></div>

Here is my current CSS:

.scroll {
    width: 200px; height: 400px;
    overflow: hidden;
}    
Mehvix
  • 296
  • 3
  • 12
Om3ga
  • 30,465
  • 43
  • 141
  • 221
  • 3
    `overflow: hidden` hides any scrollbars, so there's nothing to decorate with CSS3. You have to make scrollbars visible with `overflow:scroll` or `overflow:auto` – keaukraine Sep 09 '12 at 09:07

4 Answers4

145

Setting overflow: hidden hides the scrollbar. Set overflow: scroll to make sure the scrollbar appears all the time.

To use the ::webkit-scrollbar property, simply target .scroll before calling it.

.scroll {
   width: 200px;
   height: 400px;
    background: red;
   overflow: scroll;
}
.scroll::-webkit-scrollbar {
    width: 12px;
}

.scroll::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius: 10px;
}

.scroll::-webkit-scrollbar-thumb {
    border-radius: 10px;
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
}
​

See this live example

jacktheripper
  • 13,953
  • 12
  • 57
  • 93
1

You're setting overflow: hidden. This will hide anything that's too large for the <div>, meaning scrollbars won't be shown. Give your <div> an explicit width and/or height, and change overflow to auto:

.scroll {
   width: 200px;
   height: 400px;
   overflow: scroll;
}

If you only want to show a scrollbar if the content is longer than the <div>, change overflow to overflow: auto. You can also only show one scrollbar by using overflow-y or overflow-x.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
1

The problem with the css3 scroll bars is that, interaction can only be performed on the content. we can't interact with the scroll bar on touch devices.

0
.scroll {
   width: 200px; height: 400px;
   overflow: auto;
}
Meisam Mulla
  • 1,845
  • 3
  • 23
  • 37