# Hide the Scrollbar in CSS While Keeping Scroll Ability

> Use the scrollbar-width CSS property set to none to hide scrollbars in all modern browsers while preserving scroll functionality and accessibility

**URL:** https://sentry.io/answers/hide-scroll-bar-but-while-still-being-able-to-scroll/

---

## The Problem

You want to hide a page or page element scrollbar while still allowing scrolling; how do you do this? You may want to do this to improve the UI of a page. The page may not need a scrollbar to indicate that a user can scroll down, for example, a gallery of images with a scroll-down indicator icon.

**You should try to avoid hiding scrollbars if possible. Hiding a scrollbar is not a good idea in terms of accessibility. Instead of hiding a scrollbar, consider styling it.**

## The Solution

You can hide the scrollbar with CSS using the [`-webkit-scrollbar`](https://developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbar) CSS pseudo-element:

```css
body::-webkit-scrollbar {
  display: none;
}
```

The `::-webkit-scrollbar` pseudo-element is a legacy approach. The standard way, now supported in all major browsers (Chrome 121+, Firefox 64+, Edge 121+, Safari 18.2+), is to set the [`scrollbar-width`](https://developer.mozilla.org/en-US/docs/Web/CSS/scrollbar-width) CSS property to "none":

```css
html {
  scrollbar-width: none;
}
```

You can also hide the scrollbar using JavaScript. For example, you can create a button to toggle the scrollbar visibility:

```html
<button id="scrollbarToggle">
	Toggle Scrollbar
</button>
```

Create two different CSS classes to hide the scrollbar in [Blink](https://www.chromium.org/blink/)- and [WebKit](https://webkit.org/)-based browsers or Firefox:

```css
body.noScrollbarBlinkWebKit::-webkit-scrollbar {
    display: none;
}

html.noScrollbarFirefox {
    scrollbar-width: none;
}
```

You can then add an event listener to the button and toggle the CSS class on or off. The CSS class toggled will depend on the browser:

```js
const scrollbarToggle = document.getElementById("scrollbarToggle");

const { userAgent } = navigator;
const isWebkit = /\b(iPad|iPhone|iPod)\b/.test(userAgent)
  || /WebKit/.test(userAgent)
  || /Chrome/.test(userAgent)
  || /Edge/.test(userAgent)
  || window.MSStream;

function toggleScrollbar() {
  if (!isWebkit) {
    document.documentElement.classList.toggle("noScrollbarFirefox");
  } else if (isWebkit) {
    document.body.classList.toggle("noScrollbarBlinkWebKit");
  }
}

scrollbarToggle.addEventListener("click", () => {
  toggleScrollbar();
});
```

The type of browser is detected using the [user agent](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent). Browser detection is generally [not recommended](https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent#considerations_before_using_browser_detection).

---

*Source: [sentry.io/answers/hide-scroll-bar-but-while-still-being-able-to-scroll/](https://sentry.io/answers/hide-scroll-bar-but-while-still-being-able-to-scroll/)*
