hide scrollbar but keep scroll functionality in react.js
Asked Answered
S

7

17

I've tried all methods for vanilla HTML, JS and CSS but it don't seem to work and when it does its not responsive for instance when I reduce the screen it hides but if its Maximized it shows up

Please is there away to solve this issue in material-ui and reactjs

is there a way to make it compatible with various browsers too?

Speechmaking answered 20/2, 2020 at 22:15 Comment(0)
S
29

This worked for me, i created an external CSS file just like plain HTML and CSS and then linked it to the react file. It's also cross platform.

.parentDiv{
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: relative;
}
.childDiv{
    position: absolute;
    top: 0;
    left: 0;
    bottom: -20px;
    right: -20px; 
    overflow: scroll;
 }

if that's too long for you, try this shorter method in your CSS file:

*{
    -ms-overflow-style: none;
}
::-webkit-scrollbar {
    display: none;
}
Speechmaking answered 22/2, 2020 at 13:14 Comment(1)
webkit-scrollbar shouldn't be used in production and is a non standard. developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbarJenn
S
7

According to the spec, you can hide the scroll bars and keep the functionality in some browsers with this:

/* Hide scrollbar for Chrome, Safari and Opera */
.example::-webkit-scrollbar {
  display: none;
}

/* Hide scrollbar for IE and Edge */
.example {
  -ms-overflow-style: none;
}
Substitution answered 20/2, 2020 at 22:32 Comment(1)
webkit-scrollbar shouldn't be used in production and is a non standard. developer.mozilla.org/en-US/docs/Web/CSS/::-webkit-scrollbarJenn
K
7

In 2021:

Add in App.css, and import App.css into App.js

::-webkit-scrollbar {
  display: none;
}
Kyrakyriako answered 26/12, 2021 at 19:15 Comment(0)
T
4

In case you are using MUI styled components, you can use it while defining the custom element as well,

const MyScrollingElement = styled(Box)(() => ({
    overflow: "auto",
    scrollbarWidth: "none", // Hide the scrollbar for firefox
    '&::-webkit-scrollbar': {
        display: 'none', // Hide the scrollbar for WebKit browsers (Chrome, Safari, Edge, etc.)
    },
    '&-ms-overflow-style:': {
        display: 'none', // Hide the scrollbar for IE
    },
}));

From the above snippet, its obvious "scrollbarWidth" is most semantic way of doing it, however as pointed out here it only supports firefox browser for now. For chrome and other webkit based browsers, we have to follow the non-standard way of controlling pseudo-elements.

Try it out here.

Thingumajig answered 24/7, 2023 at 5:12 Comment(0)
R
1

This worked for me on IE and Chrome. Nothing worked for firefox(79.0), a hack could be to make it transparent.

CSS

.scrollhost::-webkit-scrollbar {
  display: none;
}

.scrollhost ::-moz-scrollbar {
  display: none;
 
}

.scrollhost {
  overflow: auto;
  -ms-overflow-style: none;
  scrollbar-color: transparent transparent; /*just hides the scrollbar for firefox */
}
Royceroyd answered 24/8, 2020 at 17:21 Comment(0)
R
1
::-webkit-scrollbar {
    width: 0;
}
Reinforce answered 19/11, 2021 at 12:44 Comment(0)
T
0

There is another method, which worked for me. If you have different pages and, for all of them, you want to hide scrollbar, this is also an equally useful way

html{ overflow-y: scroll; }

html::-webkit-scrollbar{ display: none; }

Works on Chrome, cant say for other browsers. Add this in your global CSS or App.css and then import it in App.jsx or App.js or a file where all the pages are imported, like a main file.

Thankful answered 6/8 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.