Style the scrollbar with css in Google Chrome (-webkit)
Asked Answered
G

4

51

Can somebody please help me? I want to do a simple style for the scrollbar. I found some information on the internet and tried it, but it seems that it doesn't really work for some reasons. The scrollbar looks fine when refreshing the page, but as soon as you touch the scrollbar it goes crazy. It fills with a lot of colours and you can't understand that that is a scrollbar.

Here how it looks before you touch it - http://i40.tinypic.com/a2evro.png

Here how it looks after you touch it - http://i44.tinypic.com/qzkirn.png

Here is the code that I put in css:

::-webkit-scrollbar {
width: 16px;
height: 16px; }

::-webkit-scrollbar-thumb {
background-color: rgba(0, 0, 0, 0.2);
-webkit-box-shadow: inset 1px 1px 0 rgba(0,0,0,0.10),inset 0 -1px 0 rgba(0,0,0,0.07); }

Thank you all. Cheers. Alex

Ganymede answered 12/3, 2012 at 9:16 Comment(0)
S
96

Here is an example that works:

::-webkit-scrollbar {
    height: 12px;
    width: 12px;
    background: #000;
}

::-webkit-scrollbar-thumb {
    background: #393812;
    -webkit-border-radius: 1ex;
    -webkit-box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.75);
}

::-webkit-scrollbar-corner {
    background: #000;
}

Or you can use jScrollPane.

Saffier answered 12/3, 2012 at 9:24 Comment(3)
Both link brokenZinazinah
can you post again the correct links dear @Saffier ?Buzz
@Buzz The first link is dead but the interesting content is in my original post. The second link is still working on my side, though the project has been archived by Google.Saffier
E
6
.invisible-scrollbar {
  scrollbar-width: none;
}
.invisible-scrollbar::-webkit-scrollbar {
  display: none;
}
Elane answered 21/2, 2021 at 9:45 Comment(0)
C
5

For anyone checking this answer after dec 2021, I added a snippet with @orel answer (updated the code, also added some html and changed the scrollbar style from background: #000; to background: #aaa; in order to spot the scroll better).

::-webkit-scrollbar {
    height: 12px;
    width: 12px;
    background: #aaa;
}

::-webkit-scrollbar-thumb {
    background: #393812;
    -webkit-border-radius: 1ex;
    -webkit-box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.75);
}

::-webkit-scrollbar-corner {
    background: #000;
}

.scrollable {
  max-width: 200px;
  max-height: 100%;
  
  height: 450px;
  /* change ` overflow: scroll ` to ` overflow: auto ` if you only want vertical scroll */
  overflow: scroll;
 }
<div class="scrollable">
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.

Why do we use it?
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
</div>
Caducous answered 23/12, 2021 at 8:56 Comment(2)
doesn't work on chrome mobile iphone xsmaxRidgeway
According to caniuse.com/css-scrollbar after iOS 14 (including 15.4) CSS scrollbar styling is not supported for some reason (neither on safari or on Chrome)Caducous
S
1

With Version 121 (released 2024-01-23) chrome now supports scrollbar-color. See MDN Compatibility.

In order to still use the old pseudo elements, e.g. ::-webkit-scrollbar you can use the following:

::-webkit-scrollbar {
    height: 12px;
    width: 12px;
    background: #aaa;
}

::-webkit-scrollbar-thumb {
    background: #393812;
    -webkit-border-radius: 1ex;
    -webkit-box-shadow: 0px 1px 2px rgba(0, 0, 0, 0.75);
}

::-webkit-scrollbar-corner {
    background: #000;
}

/* This next part is important */
@supports (not selector(::-webkit-scrollbar)) {
  html {
    scrollbar-width: thin;
    scrollbar-gutter: stable;
    scrollbar-color: #FFF;
  }
}
Synecious answered 4/2 at 21:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.