Search code examples
htmlcssscrollbarcode-snippets

Is there a way to move custom HTML scrollbar behind a div


I have some code snippets on my blog post here.

There I have custom scrollbar defined with this code below:

/* width */
::-webkit-scrollbar {
  width: 20px;
}

/* Track */
::-webkit-scrollbar-track {
  box-shadow: inset 0 0 5px grey;
  border-radius: 10px;
}

/* Handle */
::-webkit-scrollbar-thumb {
  background: #0ef; 
  border-radius: 10px;
}

The place where I have included the snippet is a custom div element with border radius of 2em. Now the custom scrollbar looks some bulged out as you can see in the image below:

Bulged out scrollbar

How can I send the scrollbar behind the <div> so that the extra scrollbar may be removed?

P.S.: The border radius of the <div> should be maintained.


Solution

  • You can put a small div inside your outer div and make that the container of your scroll bar to stop the bulging out of these scroll bars eg. fiddle like

    <div class="outer">
      <div class="inner">
       ..content
      </div>
    </div>
    

    .outer{
      max-height:200px;
      width:250px;
      border-radius:10px; 
      padding:0px 0px;
      background:#ccc;
    }
    .inner{
      white-space: nowrap;
      width:250px;
      max-height:200px;
      overflow-x:scroll;
      overflow-y:hidden;
      border-radius:8px;
    }
    /* width */
    ::-webkit-scrollbar {
      width: 20px;
    }
    
    /* Track */
    ::-webkit-scrollbar-track {
      box-shadow: inset 0 0 5px grey;
      border-radius: 10px;
    }
    
    /* Handle */
    ::-webkit-scrollbar-thumb {
      background: #0ef; 
      border-radius: 10px;
    }
    <div class="outer">
      <div class="inner">
     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 velit esse
     cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
     proident, sunt in culpa qui officia deserunt mollit anim id est laborum
      </div>
    </div>