How to hide scrollbar from any HTML element
Use CSS to hide the scrollbar
There are times when we need to hide the scrollbar from the HTMl elements. The uses can vary from person to person. It is opinionated topic to keep the scrollbar or not based on User Interactions(UI)/User Experience(UX).
Most of the time, I don't like to show the scrollbar to the user because of design practices I follow.
To achieve this, you just need to tickle with CSS to add some pseudo selectors for hiding it based on Browser's stylings
Let's see the default UI for scroll elements which will show the scrollbar.
We will declare a div
which will contain a list of items and then we will apply some CSS to provide max-width
and max-height
to see the scroll behaviour.
<!-- Element that contains scrollbar -->
<div class="scroll-show">
<ul>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
</ul>
</div>
.scroll-show ul {
max-height: 100px;
max-width: 200px;
overflow: auto;
border: 2px solid #f3f3f3;
padding-left: 16px;
}
Let's see how it look in the screen
{% jsfiddle https://jsfiddle.net/shashank7200/xbk8sdto result,html,css %}
You see, browser automatically adds the scrollbar by default, but If we want to hide, we can do it.
Now, we will add some css to the element to hide the scrollbar:
We will declare a div
with some other class name which will contain same number of items and then we will use the same CSS but with some extra rules to see the scroll behaviour but not see the scrollbar.
<!-- Element that contains scrollbar -->
<div class="scroll-hide">
<ul>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
<li>Some Value</li>
</ul>
</div>
.scroll-hide ul {
max-height: 100px;
max-width: 200px;
border: 2px solid #f3f3f3;
padding-left: 16px;
overflow: auto;
/* this will hide the scrollbar in mozilla based browsers */
overflow: -moz-scrollbars-none;
scrollbar-width: none;
/* this will hide the scrollbar in internet explorers */
-ms-overflow-style: none;
}
/* this will hide the scrollbar in webkit based browsers - safari, chrome, etc */
.scroll-hide ul::-webkit-scrollbar {
width: 0 !important;
display: none;
}
Let's see, whether we achieved what we wanted or not
{% jsfiddle https://jsfiddle.net/shashank7200/2v6aunq1 result,html,css %}
Here, you can see that, the scrollbar is no longer can be seen, but the functionality for scrolling remain intact.
Hope this helps you someway. Do comment your thoughts on anything, you would like to change or add.
Newsletter
If you liked this post, sign up to get updates in your email when I write something new! No spam ever.
Subscribe to the Newsletter