DEV Community

Cover image for How to hide scrollbar on your element in TailwindCSS
Derick Zihalirwa
Derick Zihalirwa

Posted on • Edited on

How to hide scrollbar on your element in TailwindCSS

before
Before

Final result
After

Here are two steps to follow in order to hide a scrollbar in your HTML elements using TailwindCSS.

Step I

Go to your global.css file styles/global.css and past this code:

/* global index.css */ 
@tailwind base;
@tailwind components;
@tailwind utilities;

/* add the code bellow */ 
@layer utilities {
      /* Hide scrollbar for Chrome, Safari and Opera */
      .no-scrollbar::-webkit-scrollbar {
          display: none;
      }
     /* Hide scrollbar for IE, Edge and Firefox */
      .no-scrollbar {
          -ms-overflow-style: none;  /* IE and Edge */
          scrollbar-width: none;  /* Firefox */
    }
  }
Enter fullscreen mode Exit fullscreen mode

At this point we added ::-webkit-scrollbar to target the scrollbar style in Chrome,Safari, Edge and Opera.

no-scrollbar is the class that we are going to use for hidding the scrollbar.

Step II

Now use no-scrollbar to hide the scrollbar in your elements. Like this:

<div className="w-full h-42 overflow-y-scroll no-scrollbar">...</div>
Enter fullscreen mode Exit fullscreen mode

I hope you find this post useful.

Feel free to connect with me on X

Latest comments (42)

Collapse
 
prince_george_ece690e13b5 profile image
Prince George

Thank you πŸ‘

Collapse
 
najib_ullah_7a9646bab175f profile image
Najib ullah

must add overflow-auto**

Collapse
 
derick1530 profile image
Derick Zihalirwa

can you elaborate

Collapse
 
ahammednibras8 profile image
Ahammed Nibras

thanks

Collapse
 
azeem_asim01 profile image
Azeem Asim

Great. Works like a charm.

Collapse
 
tomartisan profile image
tomartisan
@layer base {
  * {
    @apply border-border;
  }
  html,
  body {
    @apply no-scrollbar;
  }
}
Enter fullscreen mode Exit fullscreen mode

Combine the author's above, global scrollbar was hidden 😁

Collapse
 
derick1530 profile image
Derick Zihalirwa

Nice one πŸ’―

Collapse
 
kunal_4ae6bda8bb34e688855 profile image
Kunal

Thank you, That worked for me

Collapse
 
aditya_gupta profile image
Aditya

thanks for your work it helps me a lot. thanks once again

Collapse
 
derick1530 profile image
Derick Zihalirwa

Glad its helped!

Collapse
 
amirkermanshahani profile image
Amir Kermanshahani

How should I make scrollbar visible in desktop and hidden in mobile?

Collapse
 
derick1530 profile image
Derick Zihalirwa • Edited

I don't know but try this and let me know if it works:
in your global.css add this code:

/* global.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer utilities {
  /* Hide scrollbar for Chrome, Safari and Opera */
  .no-scrollbar::-webkit-scrollbar {
    display: none;
  }
  /* Hide scrollbar for IE, Edge and Firefox */
  .no-scrollbar {
    -ms-overflow-style: none;  /* IE and Edge */
    scrollbar-width: none;  /* Firefox */
  }
  /* Show scrollbar */
  .scrollbar::-webkit-scrollbar {
    display: block;
  }
  .scrollbar {
    -ms-overflow-style: auto;  /* IE and Edge */
    scrollbar-width: auto;  /* Firefox */
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, use the scrollbar class in combination with the lg (large screen) prefix from Tailwind to make the scrollbar visible only on desktop:

<div className="w-full h-42 overflow-y-scroll no-scrollbar lg:scrollbar">...</div>

Enter fullscreen mode Exit fullscreen mode
Collapse
 
abnet_wolde_39cc5c5851afc profile image
Abnet Wolde

am the witness: it works

Thread Thread
 
derick1530 profile image
Derick Zihalirwa

that's pretty cool

Collapse
 
minthang profile image
Minthang ML

It works!!
Thankyou bro

Collapse
 
derick1530 profile image
Derick Zihalirwa

Glad its helped!

Collapse
 
kuipid01 profile image
Stephen Busayo Adegoke

thanks boss , please how did you get this **comment **text box

Some comments have been hidden by the post's author - find out more