Can CSS be used to hide the scroll bar? How would you do this?
To disable the vertical scroll bar, just add overflow-y:hidden;
.
Find more about it: overflow.
Can CSS be used to hide the scroll bar? How would you do this?
If you wish to remove vertical (and horizontal) scrollbars from a browser viewport, add:
style="position: fixed;"
to the <body>
element.
Javascript:
document.body.style.position = 'fixed';
CSS:
body {
position: fixed;
}
If you want scrolling to work, before hiding scrollbars, consider styling them. Modern versions of OS X and mobile OS's have scrollbars that, while impractical for grabbing with a mouse, are quite beautiful and neutral.
To hide scrollbars, a technique by John Kurlak works well except for leaving Firefox users who don't have touchpads with no way to scroll unless they have a mouse with a wheel, which they probably do, but even then they can usually only scroll vertically.
John's technique uses three elements:
It must be possible to set the size of the outer and content elements the same which eliminates using percentages, but I can't think of anything else that won't work with the right tweaking.
My biggest concern is whether all versions of browsers set scrollbars to make visible overflowed content visible. I have tested in current browsers, but not older ones.
Pardon my SASS ;P
%size {
// set width and height
}
.outer {
// mask scrollbars of child
overflow: hidden;
// set mask size
@extend %size;
// has absolutely positioned child
position: relative;
}
.middle {
// always have scrollbars.
// don't use auto, it leaves vertical scrollbar showing
overflow: scroll;
// without absolute, the vertical scrollbar shows
position: absolute;
}
// prevent text selection from revealing scrollbar, which it only does on
// some webkit based browsers.
.middle::-webkit-scrollbar {
display: none;
}
.content {
// push scrollbars behind mask
@extend %size;
}
OS X is 10.6.8. Windows is Windows 7.
You can accomplish this with a wrapper div
that has its overflow hidden, and the inner div
set to auto
.
To remove the inner div
's scroll bar, you can pull it out of the outer div
's viewport by applying a negative margin to the inner div
. Then apply equal padding to the inner div so that the content stays in view.
JSFiddle
<div class="hide-scroll">
<div class="viewport">
...
</div>
</div>
.hide-scroll {
overflow: hidden;
}
.viewport {
overflow: auto;
/* Make sure the inner div is not larger than the container
* so that we have room to scroll.
*/
max-height: 100%;
/* Pick an arbitrary margin/padding that should be bigger
* than the max width of all the scroll bars across
* the devices you are targeting.
* padding = -margin
*/
margin-right: -100px;
padding-right: 100px;
}
Here's my solution, which theoretically covers all modern browsers:
html {
scrollbar-width: none; /* For Firefox */
-ms-overflow-style: none; /* For Internet Explorer and Edge */
}
html::-webkit-scrollbar {
width: 0px; /* For Chrome, Safari, and Opera */
}
html
can be replaced with any element you want to hide the scrollbar of.
Note: I've skimmed the other 19 answers to see if the code I'm posting has already been covered, and it seems like no single answer sums up the situation as it stands in 2019, although plenty of them go into excellent detail. Apologies if this has been said by someone else and I missed it.
Cross browser approach to hiding the scrollbar.
It was tested on Edge, Chrome, Firefox, and Safari
Hide scrollbar while still being able to scroll with mouse wheel!
Codepen
/* Make parent invisible */
#parent {
visibility: hidden;
overflow: scroll;
}
/* Safari and Chrome specific style. Don't need to make parent invisible, because we can style WebKit scrollbars */
#parent:not(*:root) {
visibility: visible;
}
/* Make Safari and Chrome scrollbar invisible */
#parent::-webkit-scrollbar {
visibility: hidden;
}
/* Make the child visible */
#child {
visibility: visible;
}