35

How do I get the scrollbar to overlay div content?

already tried to use track with transparent background, but it does not work

::-webkit-scrollbar-track {background: transparent}

remains the same, occupying part of the content

enter image description here

.faq-body {
  width: 250px;
  height: 400px;
  background: #fff;
  overflow: scroll;
  border: 1px solid #7b7d7f;
}

.faq-body::-webkit-scrollbar {
  width: 16px;
}
.faq-body::-webkit-scrollbar-thumb {
  background-color: #7b7d7f;
  border: 5px solid #fff;
  border-radius: 10rem;
}
.faq-body::-webkit-scrollbar-track {
  position: absolute;
  right: -3rem;
  top: -50rem;
  background: transparent;
}

.faq-question {
  padding: 20px;
  border-bottom: 1px solid #000;
  line-height: 1.3;
  color: #15191b;
  font-size: 0.8rem;
}
<div class="faq-body">
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
  </div>

Can someone help me?

3
  • do you need a custom or default scrollbar? Sep 10, 2019 at 1:29
  • @VitorAvanço custom scroll bar within a div, overriding content, as in the picture as in my question. Sou brasileiro também ;)
    – Yung Silva
    Sep 10, 2019 at 1:33
  • I tried to reach this with an iframe, but had no luck. May 2, 2021 at 14:08

2 Answers 2

9

There are two ways to get a similar result:

Javascript Lib

Use http://grsmto.github.io/simplebar/

https://jsfiddle.net/w0a5Ls6h/

Pro:

  • Browser compatibility
  • Satisfactory result

Cons:

  • 3rd javascript

or

Only CSS

<style>
  .faq-body {
      width: 250px;
      height: 400px;
      background: #fff;
      overflow-y: scroll;
      border: 1px solid #7b7d7f;
    }

    .faq-body::-webkit-scrollbar {
      width: 7px;
    }
    .faq-body::-webkit-scrollbar-thumb {
      background-color: rgba(0,0,0,0.4);
      border-radius: 10rem;
      border: 1px solid #fff;
    }


    .faq-body::-webkit-scrollbar-track-piece:start {
      background: transparent;
    }

    .faq-body::-webkit-scrollbar-track-piece:end {
      background: transparent;
    }
    .faq-question {
      padding: 20px;
      border-bottom: 1px solid #000;
      line-height: 1.3;
      color: #15191b;
      font-size: 0.8rem;
    }
</style>

Cons:

  • Browser compatibility
  • Similar result but not satisfactory
2
  • CSS solution doesn't work, lib javascript worked perfectly.
    – Yung Silva
    Sep 10, 2019 at 2:23
  • 1
    Unable to get the CSS version to work.This is trickier than it looks.
    – Tom Shaw
    Jan 8, 2020 at 2:12
5

overflow: overlay works in some browsers (Chrome, Edge) but not Firefox. Supposedly scrollbar-gutter is suppose to help, but it doesn't look like any browsers have implemented this yet.

Here's a reference to browser support: https://caniuse.com/css-overflow-overlay

.faq-body {
  width: 250px;
  height: 400px;
  background: #fff;
  overflow-y: overlay;
  border: 1px solid #7b7d7f;
}

.faq-body::-webkit-scrollbar {
  width: 16px;
}
.faq-body::-webkit-scrollbar-thumb {
  background-color: #7b7d7f;
  border: 5px solid #fff;
  border-radius: 10rem;
}
.faq-body::-webkit-scrollbar-track {
  position: absolute;
  right: -3rem;
  top: -50rem;
  background: transparent;
}

.faq-question {
  padding: 20px;
  border-bottom: 1px solid #000;
  line-height: 1.3;
  color: #15191b;
  font-size: 0.8rem;
}
<div class="faq-body">
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
    <div class="faq-question">
      Question
    </div>
  </div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.