Scrollbar track thinner than thumb

后端 未结 9 1313
时光说笑
时光说笑 2021-02-19 10:39

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

In other words, I want the thumb to be thicker than

9条回答
  •  青春惊慌失措
    2021-02-19 11:24

    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; }





提交回复
热议问题