Hiding body scrollbars using CSS

Let me start with a little disclaimer. Just because you can, doesn't mean you should use it. Hiding scrollbars can be bad for accessibility and user experience.

But there are rare cases where it makes sense, usually when you have scrolling effects or when modal is opened. So use it wisely.

Check the demo. And find the cross browser code below:

html,
body {
  /* Firefox */
  scrollbar-width: none;
  /* IE 10+ */
  -ms-overflow-style: none;
}

/* WebKit - Safari, Chrome, Opera */
body::-webkit-scrollbar {
  width: 0;
  height: 0;
}

Don't forget you can style scrollbars in webkit based browsers. Also you can use this code on any other element, not just body. Happy coding.

Comments (1)

Petar Petrovic
04. Jun 2019, 08:44

I keep needing to toggle scrollbar on the body element while developing so I made a bookmarklet based on the CSS above. Thanks Stanko! The code is below in case others need it as well. Cheers!

 javascript:(()=>{window.hss?window.hss.remove():(()=>{window.hss=document.createElement('style');hss.innerHTML='html,body{scrollbar-width:none;-ms-overflow-style:none;}body::-webkit-scrollbar{width:0;height:0;}';document.body.appendChild(hss);})()})()