How to Hide Scrollbar Using CSS Only

Hiding the scrollbar in CSS is a common technique used to enhance the aesthetics of a website. The scrollbar is the small bar that appears on the side of a web page when the content exceeds the viewable area of the browser.

It can be hidden using a combination of CSS properties, such as “overflow” and “::-webkit-scrollbar“. By setting the overflow property to “hidden” and manipulating the ::-webkit-scrollbar pseudo-element, the scrollbar can be effectively hidden from view.

However, it is important to note that this method will only work on webkit browsers such as Chrome and Safari, and will not affect the functionality of the scrollbar. It will still be available for users to interact with, they just won’t be able to see it.

How to Hide Scrollbar Using CSS & HTML

Here is an example of how to hide the scrollbar using the overflow:hidden method in an HTML document:

<!DOCTYPE html>
<html>
  <head>
    <style>
      body{
        overflow: hidden;
      }
    </style>
  </head>
  <body>
     <div class="container">
       <!-- Your content here -->
     </div>
  </body>
</html>

In this example, we use the overflow property in CSS to set the overflow of theelement to “hidden”. This will hide the scrollbar on all elements within theelement, including the entire web page.

The overflow: hidden; will hide the scrollbar on the x and y axis, this means that if the content of the website overflows on either the x or y axis, a scrollbar will not appear and the content will be hidden. However, this will still allow the user to scroll through the page using the mouse wheel or touchpad.

This method will work on all modern browsers and will affect the functionality of the scrollbar, users will not be able to interact with it. This may cause accessibility issues for users who rely on the scrollbar to navigate the page.

CSS Hide Scrollbar But Still Scroll

In this example, we start by using the ::-webkit-scrollbar pseudo-element to target the scrollbar on webkit browsers. We set the width of the scrollbar to be 0.2em and give it a background color of #F5F5F5.

Then, we use ::-webkit-scrollbar-thumb pseudo-element to target the scrollbar’s thumb (the part of the scrollbar that the user interacts with) and change it’s background color to #000000.

And finally, we use ::-webkit-scrollbar-thumb:hover pseudo-element to change the thumb’s background color to #555 when the user hovers over it.

/* Hide the scrollbar */
      ::-webkit-scrollbar {
        width: 0.2em;
        background-color: #F5F5F5;
      }
      
      /* Track */
      ::-webkit-scrollbar-thumb {
      background-color: #000000; 
      }
      
      /* Handle */
      ::-webkit-scrollbar-thumb:hover {
        background: #555; 
      }

Finally, we have a div element with overflow:auto and a fixed height of 200px. By setting overflow:auto, the scrollbar will only appear when the content inside the div exceeds the fixed height. And by setting the height of the div, we can control how much content is visible at a time.

<div style="overflow:auto; height:200px;">
  <!-- Your content here -->
</div>

CSS Hide Horizontal Scrollbar But Still Scroll

Here is an example of how to hide the horizontal scrollbar using CSS while still allowing the user to scroll through the content:

<!DOCTYPE html>
<html>
  <head>
    <style>
      /* Hide the horizontal scrollbar */
      ::-webkit-scrollbar {
        height: 0;
        overflow-x: scroll;
      }
      
      /* Track */
      ::-webkit-scrollbar-thumb {
        background-color: transparent; 
      }
      
      /* Handle */
      ::-webkit-scrollbar-thumb:hover {
        background: #555; 
      }
    </style>
  </head>
  <body>
    <div style="overflow-x: scroll; overflow-y: hidden;">
      <!-- Your content here -->
    </div>
  </body>
</html>

In this example, we use the ::-webkit-scrollbar pseudo-element to target the scrollbar on webkit browsers. We set the height of the scrollbar to be 0, making the horizontal scrollbar invisible and set the overflow-x property to “scroll” which will allow the user to scroll through the content horizontally.

We also set the overflow-y property to “hidden” to hide the vertical scrollbar.

Then, we use ::-webkit-scrollbar-thumb pseudo-element to target the scrollbar’s thumb (the part of the scrollbar that the user interacts with) and change it’s background color to be transparent. And finally, we use ::-webkit-scrollbar-thumb:hover pseudo-element to change the thumb’s background color to #555 when the user hovers over it.

CSS Scrollbar Style

You can also design your scrollbar as you want. You can change color and width etc to make it look beautiful.Here is an example of how to change the style of the scrollbar using CSS:

<!DOCTYPE html>
<html>
  <head>
    <style>
      /* Change the scrollbar style */
      ::-webkit-scrollbar {
        width: 12px; /* Width of the scrollbar */
        background-color: #F5F5F5; /* Background color of the scrollbar */
      }
      
      /* Change the scrollbar thumb style */
      ::-webkit-scrollbar-thumb {
        background-color: #000000; /* Thumb color */
        border-radius: 10px; /* Rounded corners of the thumb */
      }
      
      /* Change the scrollbar thumb style on hover */
      ::-webkit-scrollbar-thumb:hover {
        background-color: #555; /* Thumb color when hovered */
      }
    </style>
  </head>
  <body>
    <div style="overflow: scroll;">
      <!-- Your content here -->
    </div>
  </body>
</html>

In this example, we use the ::-webkit-scrollbar pseudo-element to target the scrollbar on webkit browsers. We set the width of the scrollbar to be 12px and the background-color to be #F5F5F5.

Then, we use ::-webkit-scrollbar-thumb pseudo-element to target the scrollbar’s thumb (the part of the scrollbar that the user interacts with) and change its background-color to be #000000 and set a border-radius of 10px to make it rounded.

Finally, we use ::-webkit-scrollbar-thumb:hover pseudo-element to change the thumb’s background-color to #555 when the user hovers over it.

Conclusion

In conclusion, hiding the scrollbar using CSS is a simple task that can be accomplished by setting the overflow property to “hidden” and the width or height of the scrollbar to 0.

However, it’s important to note that this method will only affect the visibility of the scrollbar and will not affect its functionality. Users will still be able to interact with the scrollbar, but it will not be visible.

It’s also possible to change the appearance of the scrollbar using CSS by targeting the ::-webkit-scrollbar and ::-webkit-scrollbar-thumb pseudo-elements and using various properties like width, background-color, and border-radius.

However, this method will only work on webkit browsers such as Chrome and Safari and will not affect other browsers unless you use vendor prefixes like -moz- and -ms-.

You Might Be Interested In: