Enhance React scrollbar functionality without relying on third-party dependencies

Currently working on enhancing the appearance of the scrollbar within my ReactJS project.

Specifically, I am aiming for a design similar to this example:

https://i.stack.imgur.com/2Q0P4.png

Experimented with CSS properties like -webkit-scrollbar, -webkit-scrollbar-track, and -webkit-scrollbar-thumb, yet no visible modifications appeared.

After scouring the internet, only 3rd party solutions offering customized scrollbars are available.

Seeking insights on achieving this without depending on external libraries. Any suggestions?

Answer №1

Using only CSS to achieve this is possible, but there are limitations in browser support leading to the existence of numerous JavaScript solutions. To adjust the width of -webkit-scrollbar-track, you can manipulate the border-right and left properties:

border-left: 5px solid gray;
border-right: 5px solid gray;

To style the -webkit-scrollbar-thumb similar to the provided image, you can tweak the border radius like so:

border-radius: 10px;

The thickness of the scrollbar can be customized using -webkit-scrollbar:

body::-webkit-scrollbar {
    width: 15px;
}

A CodePen example has been modified to mimic your desired look: https://codepen.io/rasso/pen/QWWxPQo

html {
  background: #2a2d46;
  height: 100%;
  overflow: hidden;
}
body {
  height: 100%;

  overflow: scroll;
  width: 80%;
  max-width: 600px;
  margin: 0 auto;
  padding: 3em;
  font: 100%/1.4 serif;
  border: 1px solid rgba(0, 0, 0, 0.25);
}
p {
  margin: 0 0 1.5em;
}

body::-webkit-scrollbar {
  width: 15px;
  background: #4d4e5a;
  color: #333;
}

body::-webkit-scrollbar-track {
  background: #595a62;
  border-left: 5px solid #2a2d46;
  border-right: 5px solid #2a2d46;
}

body::-webkit-scrollbar-thumb {
  background-color: #fff;
  border-radius: 10px;
}
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quidem deleniti accusantium quasi ab ad officia cupiditate sed aliquam nobis expedita velit dolorum perspiciatis pariatur nam vero et magnam soluta tempora!</div>
<div>Eveniet deserunt consequatur porro molestiae nulla optio quis totam libero iste nemo odit eum facilis sapiente. Harum similique cupiditate voluptas ea optio sunt molestias eius ab qui obcaecati nesciunt id?</div>
<div>Necessitatibus delectus sint vitae eos quasi optio esse dignissimos enim laborum ipsum harum perspiciatis eaque. Magnam fugiat delectus neque laboriosam in accusamus a veniam inventore asperiores consequatur nihil quae quo.</div>
<div>Eligendi quaerat nesciunt sint nobis qui? Temporibus maiores esse molestiae hic cum culpa sed recusandae dolore sequi modi. Quos illum debitis odio consequatur voluptatum error aliquam rem similique officia recusandae.</div>
<div>Ex suscipit earum reiciendis eaque dolorem inventore accusantium modi voluptatum saepe cum quam tempora autem corrupti illum deleniti iure rerum mollitia quo ducimus soluta ea! Illum aliquid consectetur vitae quod?</div>
<div>Minus rem tempora quo. Deleniti mollitia dignissimos laborum quis quos facilis dolore soluta error provident recusandae veniam minus consequatur blanditiis. Facere tempore praesentium soluta fugiat quia eligendi dolores ullam doloribus!</div>
<div>Aut nulla perferendis officiis accusamus ut aspernatur hic possimus amet blanditiis incidunt repudiandae ullam inventore ipsam recusandae nihil provident facilis totam quibusdam natus eligendi optio quia sapiente rerum quos illo?</div>
<div>Porro soluta nisi impedit voluptatem ex accusantium facilis amet quod blanditiis quia similique totam enim id ratione minus quo modi sequi nam dicta iste aliquid itaque velit accusamus quibusdam consequuntur.</div>
<div>Ab assumenda in ad nesciunt recusandae labore voluptates vero suscipit tenetur ullam sint beatae odio harum quo aperiam quas temporibus pariatur consectetur saepe eveniet minus voluptatem atque iusto similique repudiandae?</div>
<div>In enim odit repellat maxime harum eum expedita fuga vel eaque quaerat optio hic sapiente eveniet neque dolorum eligendi pariatur possimus aliquid minus officiis mollitia quia voluptates sunt nulla est!</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Data is not being successfully transmitted through the ORM (Prisma) to the database

After spending hours trying to solve this issue, I've hit a roadblock. My current project is using Next.js 13. I managed to successfully connect my application to a Postgres Database, configure the Prisma schema, and test a migration with a seed.ts f ...

Difficulty switching back and forth between three varying heights

I have a container with a button labeled "Show more". Upon clicking the button, the height of the container will transition through 3 different states. <div class="segment-suggestion-content height-small"> <div class="segment-suggestion-sh ...

After removing an item from the component in reactJS, the immediate rendering of the dynamic component does not seem to be functioning correctly

I am currently creating a dynamic automobile inventory website from scratch using React.js, Express.js, and MongoDB. The data has been successfully loaded from the database and displayed in a tabular format on the client side. My next task is to enable use ...

A guide on incorporating multiple sagas into a single container while ensuring that each saga listens to distinct actions

I'm currently navigating the world of react-boilerplate and I've hit a roadblock when it comes to including another saga without disrupting the functionality of previous sagas. It seems that simply declaring the sagas as constants and passing the ...

Is there a simple way to create a clickable popup menu without using JavaScript?

/*---Creating a Popup Menu with CSS---*/ .box{ position:fixed; top: 0.1%; left: 14px; display: inline-block; cursor: pointer; } ul{ list-style-type: none; } ul li{ font-family: Lato; padding: 10px; } ul li a{ text-decoration: none; color: black; } ul li:ho ...

Is it possible to display a title tooltip only when the CSS ellipsis feature is active in a React component?

Issue: I am trying to find a neat solution to display a title tooltip on items that have a CSS ellipsis applied, within a React component. Approaches Attempted: I created a ref, but it only exists after componentDidUpdate. Therefore, I force an update w ...

The functionality of Jquery datatables seems to be faulty when navigating to the second page

While utilizing the jQuery datatables plugin, I encountered an issue where the event click function only worked on the first page and not on subsequent pages. To address this problem, I discovered a helpful resource at https://datatables.net/faqs/ Q. My ...

Methods for resolving a ViewStyle typescript issue in react native

Trying to pass a width parameter into StyleSheet is causing an error like this: <View style={styles.children(width)}>{children}</View> Here's how I'm trying to use it: const styles = StyleSheet.create({ modalContent: { flex: ...

Utilizing WebSockets effectively in React Native: Best practices

I'm venturing into the realm of React Native after being well-versed in React. As a newcomer, I aim to establish a linkage between a cloud server and react-native utilizing websockets as documented. However, the absence of a suitable example has left ...

Is it possible to use ref in React to reference various elements based on specific actions?

I'm having trouble targeting the clicked button using a ref as I always get the second one. Any ideas on how to solve this issue? Also, if I have a native select element with two optgroups, is it possible to determine from which optgroup the selection ...

What is the best way to implement a "param" feature similar to React's onChange and onClick methods?

I am interested in replicating the functionality of React's onClick, onChange, etc. An example of how React does this is: onClick={(event)=>console.log(event)} My goal is to achieve a similar effect using my custom property: someProp={(wanna_do_t ...

tips for incorporating margin without sacrificing responsiveness

Having an issue with three cards in a row, they are too close together without any margin. It doesn't look good, so I tried adding margin, but it messes up the responsiveness that Bootstrap provides. So, I decided to add padding only. Any suggestions ...

Modify the value of a CSS property through JavaScript

Hey there, I'm wondering how to change a CSS value of the document itself, rather than targeting a specific element. I've already looked into solutions like Change :hover CSS properties with JavaScript, but they all involve adding CSS rules. I a ...

React setupProxy failing to proxy request

Recently, I attempted to establish a React web application integrated with an asp.net core web API housed in two separate projects. Adhering to Microsoft's instructions for configuring the templates within two distinct Visual Studio projects proved su ...

Obtaining the node generated from a document fragment

Suppose I add a document fragment to the DOM in the following way: const ul = document.querySelector("ul"); const fruits = ["Apple", "Orange", "Banana", "Melon"]; const fragment = new DocumentFragment(); ...

Attach a sticker to the input field

I want to position a label on top of an input tag permanently, rather than having it inside the input tag where it jumps to the top when clicked. Here is my attempted solution: input { font-size: 18px; padding: 10px 10px 10px 5px; -webkit-appeara ...

Transforming jquery code into Angularjs code

I am currently working on a jQuery method that adds a 'selected' class to table rows and handles click events. Here is the code: $('.niGridTable table tr').addClass('selected').end().click(function (event) { event = ...

Bootstrap modal should be closed by using the back button in bootstrap

Currently, I am in the process of developing an angularjs application that utilizes a bootstrap modal feature. My goal is to have the modal close when the user presses the back button on their phone. I have disabled html5mode, so my URLs include the # sy ...

Implement two different styles within an element's style properties

Welcome everyone, I have a question regarding adding marquee effects to a table row along with highlighting it. Is it possible to include moving text from left to right in the same active row? For example, let's say that the current time is 12:45 AM. ...

How can getters in vuex be utilized uniquely?

After dedicating two weeks to studying Vuex, I find myself pondering the true significance and purpose of getters in the framework. I ran the following code snippet and everything seems to be functioning properly. this.$store.getters["app/url"] ...