Skip to content Skip to sidebar Skip to footer

Hide Scrollbar With Overflow:scroll Enabled

I need to hide the scrollbar on a div that has overflow:scroll; enabled so that the div will scroll with mouse and keyboard but the scrollbar itself will not be displayed. is there

Solution 1:

You can do this with pure CSS (at least in webkit browsers). You have to use special scrollbar pseudo-classes to achieve this

::-webkit-scrollbar {
    display: none;
}

Read this excellent blogpost for further information.

Solution 2:

You could put the scrolling div inside of a second div with overflow hidden, then just make the inner div a little wider and taller (the amount may vary depending on the browser, however).

Something like this:

#outer {
    overflow:hidden;
    width:200px; 
    height:400px;
    border:1px solid #ccc;
}
#inner {
    overflow:scroll; 
    width:217px; 
    height:417px;
}​

Full example at http://jsfiddle.net/uB6Dg/1/.

Edit: Unfortunately you can still get to the scrollbars by highlighting the text and dragging, and it does make padding etc a bit more of a pain, but other than this I think javascript is the way to go.

Solution 3:

@Maloric answer pointed me in the correct direction, however I needed fluid width, and I also wanted to be more accurate on the width of the scrollbar.

Here is a function that will return the exact width of the scrollbar based on what the browser reports.

var getWidth = function () {
    var scrollDiv = document.createElement('div'),
        scrollbarWidth;

    scrollDiv.style.overflow = 'scroll';
    document.body.appendChild(scrollDiv);

    scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
    
    document.body.removeChild(scrollDiv);

    return scrollbarWidth;
};

var width = getWidth();
var container = document.querySelector('.overflowing-container');

container.style.paddingRight = width + 'px';
container.style.marginRight = (width * -1) + 'px';

// Just for testing purposesdocument.querySelector('.scrollbar-width').innerHTML = 'scrollbar height: ' + getWidth()
.container {
  height: 200px;
  overflow-x: hidden;
  overflow-y: auto;
  width: 500px;
}

.overflowing-container {
  height: 100%;
  overflow-y: auto;
  width: 100%;

}
<divclass="container"><divclass="overflowing-container">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris tristique feugiat metus, eget mollis nibh vestibulum eu. Nullam eros orci, gravida eu quam nec, maximus posuere dui. Maecenas erat magna, elementum eget nunc eget, tincidunt varius nisl. Phasellus pretium congue consectetur. Donec rutrum nisi sed eros posuere, vel pretium nunc viverra. Praesent consequat sagittis urna, quis convallis magna gravida et. In sed eleifend arcu.

  Duis ornare condimentum est luctus malesuada. Morbi nec sodales nunc. Morbi vehicula tristique massa, nec lacinia tellus vulputate fringilla. Nam eget pulvinar libero. Vestibulum ligula mi, tincidunt ac pellentesque vitae, convallis eu tortor. Cras varius dolor sit amet libero rhoncus, mattis aliquet augue porttitor. Etiam sollicitudin, sem ut mollis imperdiet, erat enim gravida tortor, et imperdiet sem nibh in ex. Aliquam ac aliquam risus. Suspendisse gravida suscipit sapien, et ultrices massa ornare eget. Nulla venenatis pellentesque arcu at auctor. Sed libero ligula, pretium in metus a, malesuada ullamcorper leo. Vivamus tempor velit in ante fringilla rhoncus. Nam ac iaculis arcu. Mauris a nisi quis arcu feugiat posuere.
  </div></div><divclass="scrollbar-width"></div>

The above snippet shows this in action.

Solution 4:

You need to make use of the jquery plugin from this site http://jscrollpane.kelvinluck.com/

Post a Comment for "Hide Scrollbar With Overflow:scroll Enabled"