is it possible to use transitions on webkit scrollbars? I tried:
div#main::-webkit-scrollbar-thumb {
background: rgba(255,204,102,0.25);
-webkit-transiti
Here's another idea based on replies here. You can use color instead of background-color and then box-shadow to colorize the thumb (you can even hack text-shadow for fun like I did, obviously not a production-ready approach but you can just override color in child container):
https://codepen.io/waterplea/pen/dVMopv
*::-webkit-scrollbar-thumb {
box-shadow: inset 0 0 0 10px;
}
div {
overflow: auto;
color: rgba(0, 0, 0, 0);
transition: color .3s ease;
}
div:hover {
color: rgba(0, 0, 0, 0.3);
}
Adapted from @brillout's answer, if we transition the border-color
instead of background-color
, we can avoid using background-clip: text
, which leaves some fragments behind if you have any nested text.
Full explanation:
border-color
on your wrapper to change the color on hover.border-color: inherit
on your scrollbar.Now when we hover over the wrapper, the border color will transition. The wrapper doesn't have a border, so we don't see anything happen. However, the scrollbar is inheriting that color, so the scrollbar color changes.
Here is the most important code. A full example is available in this fiddle and the snippet below.
#scroller {
/* fill parent */
display: block;
width: 100%;
height: 100%;
/* set to some transparent color */
border-color: rgba(0, 0, 0, 0.0);
/* here we make the color transition */
transition: border-color 0.75s linear;
/* make this element do the scrolling */
overflow: auto;
}
#scroller:hover {
/* the color we want the scrollbar on hover */
border-color: rgba(0, 0, 0, 0.1);
}
#scroller::-webkit-scrollbar,
#scroller::-webkit-scrollbar-thumb,
#scroller::-webkit-scrollbar-corner {
/* add border to act as background-color */
border-right-style: inset;
/* sum viewport dimensions to guarantee border will fill scrollbar */
border-right-width: calc(100vw + 100vh);
/* inherit border-color to inherit transitions */
border-color: inherit;
}
<div id="scroller">...</div>
body {
background: whitesmoke;
}
#wrapper {
width: 150px;
height: 150px;
margin: 2em auto;
box-shadow: 0 0 15px 5px #ccc;
}
#scroller {
/* fill parent */
display: block;
width: 100%;
height: 100%;
}
#content {
display: block;
width: 300px;
height: auto;
padding: 5px;
}
#scroller {
/* The background-color of the scrollbar cannot be transitioned.
To work around this, we set and transition the property
of the wrapper and just set the scrollbar to inherit its
value. Now, when the the wrapper transitions that property,
so does the scrollbar. However, if we set a background-color,
this color shows up in the wrapper as well as the scrollbar.
Solution: we transition the border-color and add a border-right
to the scrollbar that is as large as the viewport. */
border-color: rgba(0, 0, 0, 0.0);
transition: border-color 0.75s linear;
/* make this element do the scrolling */
overflow: auto;
}
#scroller:hover {
border-color: rgba(0, 0, 0, 0.1);
transition: border-color 0.125s linear;
}
#scroller::-webkit-scrollbar,
#scroller::-webkit-scrollbar-thumb,
#scroller::-webkit-scrollbar-corner {
/* add border to act as background-color */
border-right-style: inset;
/* sum viewport dimensions to guarantee border will fill scrollbar */
border-right-width: calc(100vw + 100vh);
/* inherit border-color to inherit transitions */
border-color: inherit;
}
#scroller::-webkit-scrollbar {
width: 0.5rem;
height: 0.5rem;
}
#scroller::-webkit-scrollbar-thumb {
border-color: rgba(0, 0, 0, 0.1);
/* uncomment this to hide the thumb when not hovered */
/* border-color: inherit; */
}
#scroller::-webkit-scrollbar-thumb:hover {
border-color: rgba(0, 0, 0, 0.15);
}
#scroller::-webkit-scrollbar-thumb:active {
border-color: rgba(0, 0, 0, 0.2);
}
<div id="wrapper">
<div id="scroller">
<div id="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum pretium mi felis, pharetra ornare lorem pellentesque pulvinar. Donec varius condimentum nunc at mollis. Pellentesque posuere et quam eu tristique. Integer interdum enim non interdum mattis. Suspendisse gravida nibh enim, non efficitur lacus suscipit quis. Etiam pharetra libero auctor ultricies ornare. Duis dapibus semper semper. Nam sit amet lobortis arcu.
Maecenas fermentum risus quis justo convallis, non ornare erat fringilla. Cras eleifend leo sapien, ac iaculis orci ultricies sed. Praesent ultrices accumsan risus, pharetra pharetra lorem dignissim id. Aenean laoreet fringilla eros, vel luctus eros luctus sed. Nullam fermentum massa sit amet arcu dictum, nec bibendum lectus porta. Duis pellentesque dui sed nisi ultricies, vitae feugiat dui accumsan. Nam sollicitudin, ex et viverra ultricies, justo metus porttitor quam, quis vestibulum nibh nisl eget leo.
Integer luctus arcu et sapien accumsan fringilla. Integer mollis tellus vel imperdiet elementum. Ut consequat ac nibh ac sagittis. Duis neque purus, pellentesque nec erat id, pharetra ornare sapien. Etiam volutpat tincidunt nunc ac facilisis. Aenean sed purus pellentesque, vehicula mauris porta, fringilla nibh. Ut placerat, risus et congue rutrum, lorem arcu aliquet urna, sollicitudin venenatis lorem eros et diam. Aliquam sodales ex risus, ac vulputate ipsum porttitor vel. Pellentesque mattis nibh orci. Morbi turpis nulla, tincidunt vitae tincidunt in, sodales et arcu. Nam tincidunt orci id sapien venenatis porttitor ut eu ipsum. Curabitur turpis sapien, accumsan eget risus et, congue suscipit ligula.
Maecenas felis quam, ultrices ac ornare nec, blandit at leo. Integer dapibus bibendum lectus. Donec pretium vehicula velit. Etiam eu cursus ligula. Nam rhoncus diam lacus, id tincidunt dui consequat id. Ut eget auctor massa, quis laoreet risus. Nunc blandit sapien non massa bibendum, ac auctor quam pellentesque. Quisque ultricies, est vitae pharetra hendrerit, elit enim interdum odio, eu malesuada nibh nulla a nisi. Ut quam tortor, feugiat sit amet malesuada eu, viverra in neque. Nullam lacinia justo sit amet porta interdum. Etiam enim orci, rutrum sit amet neque non, finibus elementum magna. Sed ac felis quis nunc fermentum suscipit.
Ut aliquam placerat nulla eget aliquam. Phasellus sed purus mi. Morbi tincidunt est dictum, faucibus orci in, lobortis eros. Etiam sed viverra metus, non vehicula ex. Sed consectetur sodales felis, vel ultrices risus laoreet eget. Morbi ut eleifend lacus, ac accumsan risus. Donec iaculis ex nec ante efficitur vestibulum.
</div>
</div>
</div>
There is a fair amount you can do to style the scroll bar:
::-webkit-scrollbar {
width: 18px;
height: 20px;
}
::-webkit-scrollbar-thumb {
height: 6px;
border: 4px solid rgba(0, 0, 0, 0);
background-clip: padding-box;
-webkit-border-radius: 7px;
background-color: rgba(0, 0, 0, 0.70);
-webkit-box-shadow: inset -1px -1px 0px rgba(0, 0, 0, 0.05), inset 1px 1px 0px rgba(0, 0, 0, 0.05);
}
::-webkit-scrollbar-button{
width: 0;
height: 0;
display: none;
}
::-webkit-scrollbar-corner {
background-color: transparent;
}
However sadly I believe you cannot do what you are asking.
JSFiddle
Short answer: No, it's not possible to use transition
on a ::-webkit-scrollbar
Long answer: There are means to achieve a similar effect entirely in CSS.
Explanation:
background-color
property. This property will match the color we want to transition on for the scrollbar.background-color
that masks the outer container.background-color
will inherit the outer container's.transition
property will be applied to the background-color
of the outer container.A major disadvantage here is that you have to do some masking. This can be a bit of a hassle if your background isn't a solid color, since the inner container will most likely need to match. If that's not a worry, this will work for you. Here's the code to put it all together for a page with a horizontally scrolling component.
HTML
<div id="container-outer">
<div id="container-inner">
<!-- Content goes here -->
</div>
</div>
CSS
/* Scrollbar */
::-webkit-scrollbar {
border: 0;
height: 10px;
}
::-webkit-scrollbar-track {
background: rgba(0,0,0,0);
}
::-webkit-scrollbar-thumb {
background-color: inherit; /* Inherits from outer container */
border-radius: 20px;
}
/* Container */
#container-outer {
overflow-y: hidden;
overflow-x: scroll; /* Horizontal-only scrolling */
height: 400px;
background-color: white; /* Initial color of the scrollbar */
transition: background-color 200ms;
}
#container-outer:hover {
background-color: red; /* Hover state color of the scrollbar */
}
#container-inner {
background-color: white; /* Masks outer container */
font-size: 0;
height: inherit; /* Inherits from outer container */
width: 10000px; /* Set to see the scrolling effect */
}
Notes:
max-width
that will match whatever your inner container's width
is, otherwise you may see some oddity on extremely large displays. This is an edge case for when the browser width is larger than the horizontally scrolling content width. This is assuming you are using a horizontal scroll, as the example code does.It is fairly easy to achieve using Mari M's background-color: inherit;
technique in addition with -webkit-background-clip: text;
.
Live demo; https://jsfiddle.net/s10f04du/
@media screen and (-webkit-min-device-pixel-ratio:0) {
.container {
overflow-y: scroll;
overflow-x: hidden;
background-color: rgba(0,0,0,0);
-webkit-background-clip: text;
transition: background-color .8s;
}
.container:hover {
background-color: rgba(0,0,0,0.18);
}
.container::-webkit-scrollbar-thumb {
background-color: inherit;
}
}