2

When I add overflow: scroll; to a fixed overlay div to make its content scrollable, I get two scroll bars in the right and bottom of a browser window, one is a browser's scroll bar and the second one is the div's scroll bar. How can I make the fixed scrollable div to cover up a browser's scrollbar?

div {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    overflow: scroll;
}
qwaz
  • 1,285
  • 4
  • 23
  • 47
  • 4
    have you tried changing your body overflow to hidden so the browser won't show the additional scroll bars? `html, body {overflow:hidden;}` – Howard Renollet Nov 17 '14 at 16:38
  • 1
    The browser scrollbar is outside the HTML document so you can't "cover it up". You can only turn it off as @HowardRenollet says. – Rhumborl Nov 17 '14 at 16:42

4 Answers4

2

If you're trying to make the html/body fixed, you need to specify that.

html, body {
    overflow: hidden;
}

Then whichever div you want to scroll through, add

div {
    overflow: scroll;
}

I've made a CodePen so you can see here:

http://codepen.io/brycesnyder/pen/pvomRa

Alternatively, if you want to "hide" the actual scroll bar.. This is a fix I just did to try that. Now sure why you'd want to though! http://codepen.io/brycesnyder/pen/ZYENKj

SNYDERHAUS
  • 421
  • 2
  • 8
  • 26
2

Ok, per my comment, here's the answer:

You are currently experiencing double scrollbars, one for the window and one for the div, as I show here: http://jsfiddle.net/eL5yvony/

There are a couple of ways to correct this.

First, and probably the easiest, is to set the overflow: hidden; on the html and body elements like so: http://jsfiddle.net/eL5yvony/1/

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

This eliminates the browser's scroll bar and only shows the one in the div

Alternatively, if your div is 100% x 100%, then you could do something like this by setting the margin and padding of your html and body elements, without overriding the browser's scroll functionality: http://jsfiddle.net/eL5yvony/4/

div{
    width: 100%; height: 100%;
    overflow: scroll;
}
html, body{
    width: 100%; height: 100%;
    margin: 0; padding: 0;
}
Howard Renollet
  • 4,609
  • 1
  • 24
  • 31
0

I think this is what you are looking for:

#parent{
    height: 100%;
    width: 100%;
    overflow: hidden;
}

#child{
    width: 100%;
    height: 100%;
    overflow: auto;
    padding-right: 15px; /* Increase this value for cross-browser compatibility */
}

source: Hide scroll bar, but still being able to scroll

Community
  • 1
  • 1
0

Use overflow: auto;

Demo on JSBIN:

http://jsbin.com/cedabegeju/2/edit

Demo on SE:

for (var i = 0 ; i++  < 1000; ) {
  setTimeout( function() {
    $('div').text($('div').text() + 'x ');
  }, i);
}
div {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    overflow: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>

</div>
Nabil Kadimi
  • 10,078
  • 2
  • 51
  • 58