Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar track thinner than thumb

I am trying to style a scrollbar using CSS. I want to achieve the following look (ignore the background):

enter image description here

In other words, I want the thumb to be thicker than the track, which I only want to be a line.

I have researched this and found solutions for people wanting to do the opposite. That is a thumb smaller than the track, but no way of achieving what I want.

As an alternative option, I have thought of using the border-right property with negative offset, but again no luck. The only outline has offset and outline four-sided.

Any suggestions will be appreciated.

like image 422
Paul Avatar asked Dec 15 '16 21:12

Paul


People also ask

How do I change the width of my scroll bar?

Change Scrollbar Width in Windows 11/10Press Win+R in combination to bring up the Run dialog box. In the dialog box that appears, type REGEDIT and hit the OK button. If prompted by UAC, click on Yes. Select the key and in the right pane of WindowsMetrics, double-click 'ScrollHeight' word.

What is thumb size in scrollbar?

the height of the thumb is based in the size of content, you can change the width inside the ::-webkit-scrollbar but the height will always be based on the content.

How do I make my horizontal scroll bar smaller?

To resize the horizontal scroll bar, place the mouse pointer over the three vertical dots, then click-and-drag to the right or left. To fix problems with the vertical scroll bar slider range, find and delete the row containing the last activated cell.

What is width of scroll bar?

Write the code that returns the width of a standard scrollbar. For Windows it usually varies between 12px and 20px . If the browser doesn't reserve any space for it (the scrollbar is half-translucent over the text, also happens), then it may be 0px .


2 Answers

I believe I found an answer. I had the same issue today; I was tempted to use Javascript. I am that CSS type of guy though...

If you wish to know what each part of the scrollbar associates to which part of the css; then you might first of all want to check the CSS Tricks post for Custom Scrollbars. (That helped me a lot)

The trick here is to give the scrollbar the same width as your "thumb". Then you will want to give the "track" a transparent background, with a background image. Repeat the background image vertically, and you'll have yourself a good looking scroll bar.

To demonstrate that, I have this image that is 8 pixels wide and 1 pixel tall. Only the middle 2 pixels are colored blue. You can find the image here.

Please note that the image is 8 pixels because in my css, the scrollbar is 8 pixels wide.

Now the CSS needs to come into play. So we're gonna do the following.

::-webkit-scrollbar,
::-webkit-scrollbar-thumb,
::-webkit-scrollbar-track { 
    width: 8px;
    border: none;
    background: transparent;
}

::-webkit-scrollbar-button,
::-webkit-scrollbar-track-piece,
::-webkit-scrollbar-corner,
::-webkit-resizer {
    display: none;
}

::-webkit-scrollbar-thumb {
    border-radius: 6px;
    background-color: #1A5FAC;
}

::-webkit-scrollbar-track {
    background-image: url("https://i.imgur.com/GvV1R30.png");
    background-repeat: repeat-y;
    background-size: contain;
}

To help with demonstration, I arranged a small snippet. Note that I restricted the scrollbar to divs and for that, you'll only need to remove the div before every ::. ( ^ Or just use the CSS above ^ )

::-webkit-scrollbar,
::-webkit-scrollbar-thumb,
::-webkit-scrollbar-track { 
    width: 8px;
    border: none;
    background: transparent;
}

::-webkit-scrollbar-button,
::-webkit-scrollbar-track-piece,
::-webkit-scrollbar-corner,
::-webkit-resizer {
    display: none;
}

::-webkit-scrollbar-thumb {
    border-radius: 6px;
    background-color: #1A5FAC;
}

::-webkit-scrollbar-track {
    background-image: url("https://i.imgur.com/GvV1R30.png");
    background-repeat: repeat-y;
    background-size: contain;
}

/* CUSTOM STYLING HERE - IGNORE */

div {
    width: 500px;
    height: 160px;
    margin: auto auto;
    overflow-x: hidden;
    overflow-y: auto;
}

hr {
    width: 450px;
    height: 160px;
    border: none;
    margin: 0 auto;
    background-color: #FFFFFF;
}
  hr:nth-of-type(1) { background-color: #5BC0EB; }
  hr:nth-of-type(2) { background-color: #FDE74C; }
  hr:nth-of-type(3) { background-color: #9BC53D; }
  hr:nth-of-type(4) { background-color: #E55934; }
  hr:nth-of-type(5) { background-color: #FA7921; }
<div><hr /><hr /><hr /><hr /><hr /></div>
like image 140
Nizar Avatar answered Sep 19 '22 12:09

Nizar


There are 7 different scrollbar options to use:

::-webkit-scrollbar {/ * 1 - scrollbar * /}
::-webkit-scrollbar-button {/ * 2 - button * /}
::-webkit-scrollbar-track {/ * 3 - track * /}
::-webkit-scrollbar-track-piece {/ * 4 - the visible part of the track */}
::-webkit-scrollbar-thumb {/ * 5 - slider * /}
::-webkit-scrollbar-corner {/ * 6 - corner * /}
::-webkit-resizer {/ * 7 - resizing * /}

For what you're trying to achieve, you can do this:

::-webkit-scrollbar {
    width: 12px;
}

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

::-webkit-scrollbar-thumb {
    box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
    border-radius: 10px;
}
like image 40
Aram Mnatsakanyan Avatar answered Sep 17 '22 12:09

Aram Mnatsakanyan