Search code examples
cssgoogle-chromewebkitscrollbar

Chrome: custom scrollbar problem when both scroll bars are applied


Check the following snippet in Chrome/Safari/new Edge, and scroll the container to the very bottom:

body { background-color: #fff }

section {
  background-color: #f8f8f8;
  width: 400px;
  height: 150px;
  padding: 10px;
  overflow: overlay;
}

.force-overflow {
  background-image: linear-gradient(45deg, orange, yellow, orange);
  height: 600px;
}

::-webkit-scrollbar {
  background-color: #F5F5F5;
  height: 10px;
  width: 10px;
}

::-webkit-scrollbar-track {
  background-color: #F5F5F5;
  box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
}

::-webkit-scrollbar-thumb {
  background: #999;
}

::-webkit-scrollbar-thumb:hover {
  background: #444;
}

::-webkit-scrollbar-corner {
  background: transparent;
}
<section>
  <div class="force-overflow"></div>
</section>

This works as expected.

Now if we add a horizontal overflow as well:

body { background-color: #fff }

section {
  background-color: #f8f8f8;
  width: 400px;
  height: 150px;
  padding: 10px;
  overflow: overlay;
}

.force-overflow {
  background-image: linear-gradient(45deg, orange, yellow, orange);
  height: 600px;
  width: 1200px;
}

::-webkit-scrollbar {
  background-color: #F5F5F5;
  height: 10px;
  width: 10px;
}

::-webkit-scrollbar-track {
  background-color: #F5F5F5;
  box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
}

::-webkit-scrollbar-thumb {
  background: #999;
}

::-webkit-scrollbar-thumb:hover {
  background: #444;
}

::-webkit-scrollbar-corner {
  background: transparent;
}
<section>
  <div class="force-overflow"></div>
</section>

and scroll to the very right/bottom, we get this:

enter image description here enter image description here

I know I can fix this using overflow: auto; instead of overflow: overlay;, but I really would like to stick with overlay because it prevents your layout from "jumping" as soon as a scrollbar shows up/vanishes.

Does anyone know how to fix the issue?


Solution

  • a hacky solution is to add some box-shadow to cover this part:

    ::-webkit-scrollbar-thumb:horizontal {
      box-shadow: 3px 0 0 0;
    }
    ::-webkit-scrollbar-thumb:vertical {
      box-shadow: 0 3px 0 0;
    }
    

    body { background-color: #fff }
    
    section {
      background-color: #f8f8f8;
      width: 400px;
      height: 200px;
      padding: 10px;
      overflow: overlay;
    }
    
    .force-overflow {
      background-image: linear-gradient(45deg, orange, yellow, orange);
      height: 800px;
      width: 1200px;
    }
    
    ::-webkit-scrollbar {
      background-color: #F5F5F5;
      height: 10px;
      width: 10px;
    }
    
    ::-webkit-scrollbar-track {
      background-color: #F5F5F5;
      box-shadow: inset 0 0 3px rgba(0,0,0,0.2);
    }
    
    ::-webkit-scrollbar-thumb {
      background: #999;
      color:#999;
      transition-duration: 2s;
    }
    ::-webkit-scrollbar-thumb:horizontal {
      box-shadow: 3px 0 0 0;
    }
    ::-webkit-scrollbar-thumb:vertical {
      box-shadow: 0 3px 0 0;
    }
    
    
    ::-webkit-scrollbar-thumb:hover {
      background: #444;
      color:#444;
      transition-duration: 2s;
    }
    ::-webkit-scrollbar-corner {
      background: transparent;
    }
    <section>
      <div class="force-overflow"></div>
    </section>