CSS customized scroll bar in div

后端 未结 19 1381
夕颜
夕颜 2020-11-21 11:05

How can I customize a scroll bar via CSS (Cascading Style Sheets) for one div and not the whole page?

19条回答
  •  迷失自我
    2020-11-21 11:44

    Webkit browsers (such as Chrome, Safari and Opera) supports the non-standard ::-webkit-scrollbar pseudo element, which allows us to modify the look of the browser's scrollbar.

    Note: The ::-webkit-scrollbar is not supported by Firefox or IE and Edge.

    * {
      box-sizing: border-box;
      font-family: sans-serif;
    }
    
    div {
      width: 15rem;
      height: 8rem;
      padding: .5rem;
      border: 1px solid #aaa;
      margin-bottom: 1rem;
      overflow: auto;
    }
    
    .box::-webkit-scrollbar {
      width: .8em;
    }
    
    .box::-webkit-scrollbar-track {
      box-shadow: inset 0 0 6px rgba(0, 0, 0, .3);
    }
     
    .box::-webkit-scrollbar-thumb {
      background-color: dodgerblue;
    }

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate

    Reference: How To Create a Custom Scrollbar

提交回复
热议问题