Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create scrollable element in Tailwind without a scrollbar

I'm trying to recreate a horizontal scroll navbar with tailwind without a scrollbar on the bottom like this example (reduce the width of your screen to be able to scroll)

https://getbootstrap.com/docs/5.0/examples/blog/

I tried the following using Tailwind but I wasn't able to figure out how to remove the horizontal scrollbar that appears like the bootstrap example above. Could someone help?

<ul class="flex overflow-x-auto whitespace-nowrap p-4">
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
  <li><a href="/" class="p-2">Nav Item</a></li>
</ul>
like image 497
vince Avatar asked Mar 01 '21 04:03

vince


People also ask

How do I hide vertical scrollbar but still scrollable?

How to Hide the Vertical Scrollbar in CSS. To hide the vertical scrollbar and prevent vertical scrolling, use overflow-y: hidden like so: HTML.

How do you make a scrollable section?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

How do you make a horizontal scroll in Tailwind CSS?

​Use overflow-x-scroll to allow horizontal scrolling and always show scrollbars unless always-visible scrollbars are disabled by the operating system.

How do I hide the scrollbar in embed?

Notice that 'overflow:hidden' is in the HTML file of the embedded element. Show activity on this post. Use your embed tag inside a container with overflow hidden. Then set the width of the embed with 100% + 17px (the default width of a scrollbar).


3 Answers

To hide the scrollbar you'll need to style it directly:

/* 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 */
}

You could easily add these as Tailwind utilities using a plugin in your config: https://tailwindcss.com/docs/plugins#adding-utilities


Further reading:

https://css-tricks.com/almanac/properties/s/scrollbar/ https://www.w3schools.com/howto/howto_css_hide_scrollbars.asp

like image 95
Steve O Avatar answered Oct 21 '22 08:10

Steve O


/*
    https://github.com/tailwindlabs/tailwindcss/discussions/2394
    https://github.com/tailwindlabs/tailwindcss/pull/5732
*/
@layer utilities {
    /* Chrome, Safari and Opera */
    .no-scrollbar::-webkit-scrollbar {
      display: none;
    }

    .no-scrollbar {
      -ms-overflow-style: none; /* IE and Edge */
      scrollbar-width: none; /* Firefox */
    }
}

Then you just add the class no-scrollbar as you would typically like so, notice I added overflow-y-auto to keep the scrollbar automatically the correct size too.

<div className="no-scrollbar overflow-y-auto">

ALTERNATIVELY:

You could try this tailwindcss plugin for hide scrollbar

https://github.com/reslear/tailwind-scrollbar-hide

like image 42
jasonleonhard Avatar answered Oct 21 '22 07:10

jasonleonhard


To answer @wataru's question in the comments, the syntax to add these classes as a plugin to tailwind.config.js is this:

const plugin = require('tailwindcss/plugin')

module.exports = {
  content: [
    "./pages/**/*.{js,ts,jsx}",
    "./components/**/*.{js,ts,jsx}",
  ], 
  theme: {
    extend: {},
  },
  plugins: [
    plugin(function ({ addUtilities }) {
      addUtilities({
        '.scrollbar-hide': {
          /* IE and Edge */
          '-ms-overflow-style': 'none',

          /* Firefox */
          'scrollbar-width': 'none',

          /* Safari and Chrome */
          '&::-webkit-scrollbar': {
            display: 'none'
          }
        }
      }
      )
    })
  ],
}

The lines to inspect are the const plugin and the plugins: [] array

I learned this by inspecting the source code of the https://github.com/reslear/tailwind-scrollbar-hide package linked by @jasonleonhard above.

like image 41
Michael McGreal Avatar answered Oct 21 '22 08:10

Michael McGreal