In spite of its elegance, the scrollbar can make a website look like it was designed in the 1990s. However, thanks to the latest CSS properties, we can hide the scrollbar without impacting our usersβ ability to scroll.
I'll show you the step by step process to hide the scroll bar in each browser.
Reason For Removing Scroll Bar
- Hiding a scrollbar also helps you save space for other content on your page β it can make for a more consistent user experience across different devices because different systems may not render the scrollbar in the same way.
- However, the most common reason to hide a scrollbar is for mobile viewing experiences. When viewing an app, like Facebook, on their phone, most users expect scrolling to be vertical, with no need for horizontal movement since screens are usually tall and narrow.
Hiding the scrollbar
Because of the fundamental differences between browsers, thereβs no universal way of hiding scrollbars with CSS. Each browser has its method, and we will look at all of them below.
To hide the scrollbar in WebKit-based browsers, you have to use the ::-webkit-scrollbar
pseudo selector. ::-webkit-scrollbar
is a pseudo-element in CSS used by developers to modify the look of a browserβs scrollbar.
With the ::-webkit-scrollbar
pseudo selector, thereβs a wide range of things you can do to a scrollbar. You can modify the arrows pointing up and down, change the trackβs color, change the background, and more. But weβre only going to look at how to hide the scrollbar without impacting scrolling. Letβs look at an example.
As you can see, in the stackblitz below, the scrollbar is visible on the right. To make it invisible without impacting scrolling, you just have to target the body element using the ::-webkit-scrollbar
pseudo selector and set the display to none.
As you can see below, the scrollbar is no longer visible, but the page is still scrollable for both the mouse and keyboard.
In IE (Internet Explorer) and Edge
The syntax for hiding a scrollbar in IE and Edge is a bit different from WebKit-based browsers like Opera, Chrome, and Safari. Here, we have to use the -ms-overflow-style
property.
Unlike ::-webkit-scrollbar
, you canβt use the -ms-overflow-style
to customize the scrollbar. To use it to hide a scrollbar, you just have to use the code below.
If youβre targeting a particular element or section, use the following code.
If youβre targeting the entire page, use this code.
In Firefox
To successfully hide the scrollbar in Firefox, you just need to use the scrollbar-width:none
property to target a section like this.
To target the scrollbar of the entire page in Firefox, you have to be a bit more careful. In the two sections above, we applied the scrollbar code to the body
tag, and it worked, but with Firefox, we have to target the html
tag instead.
Alternatively, you can choose to use this other method to hide the scrollbar in Firefox.
Conclusion
If you are reading this, then by now, you fully understand the art of hiding scrollbars with CSS without impacting scrolling and accessibility. Itβs important to note that when targeting sections that have scrollable content, leaving the scrollbar visible as a visual cue will help your users notice the section and not skip it by accident.
Top comments (1)
Π‘ongratulations π₯³! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up π