Modify the scrollbar's width, color, and corner radius to customize its appearance

Here is the table code where the styles are applied. By setting the tablecontainer height to 600px, we can make the table scrollable. However, I would like to customize the scrollbar with a different color, width, and corner radius.

 <TableContainer style={{height: '600px', border: '1px solid #ffcccb', marginTop: '20px', width: '1100px', paddingLeft: '10px', paddingRight: '10px', paddingTop: '20px'}}>
                <Table sx={{ minWidth: 650 }} aria-label="simple table">
                    <TableHead>
                        <TableRow>
                            {header && header.map((item, index) => (
                                <TableCell key={index}>{item}</TableCell>
                            ))}
                        </TableRow>
                    </TableHead>

Answer №1

How to apply a className to the TableContainer component

.your-css-classname {
      height: '600px';
      border: '1px solid #ffcccb';
      margin-top: '20px';
      width: '1100px';
      padding-left: '10px';
      padding-right: '10px';
      padding-top: '20px';

      &::-webkit-scrollbar {
          width: 5px;
      }

      &::-webkit-scrollbar-track {
          background-color: #eceef2;
          border-radius: 180px;
      }

      &::-webkit-scrollbar-thumb {
          background-color: #9b9b9b;
          border-radius: 180px;
      }
}

Answer №2

To enhance the styling of your table, consider using the sx property instead of the traditional style tag. Create a separate constant named tableSx to define the specific styles for your table.

const tableSx = {
  height: "600px",
  border: "1px solid #ffcccb",
  marginTop: "20px",
  width: '1100px',
  padding: '20px 10px 0 10px',
  "&.MuiTableContainer-root": {
    "&::-webkit-scrollbar": {
      width: "25px"
    },
    "&::-webkit-scrollbar-track": {
      backgroundColor: "purple",
      borderRadius: "6px"
    },
    "&::-webkit-scrollbar-thumb": {
      backgroundColor: "green",
      borderRadius: "6px"
    }
  }
};

<TableContainer component={Paper} sx={tableSx}> ... </TableContainer>

If you want to see this in action, check out this codesandbox example

Answer №3

You have the ability to customize the MUI Table scrollbar using WebKit.

  1. To specify a width, utilize -webkit-scrollbar :

    "&::-webkit-scrollbar": { width: 15, },

  2. For setting a background color, you can use "&::-webkit-scrollbar-track" :

    "&::-webkit-scrollbar-track": { backgroundColor: "#B5FF33", },

  3. If you want to modify the scrollbar thumb's background color and shape it with rounded edges, employ "&::-webkit-scrollbar-thumb".

    "&::-webkit-scrollbar-thumb": { backgroundColor: "green", borderRadius: 2, },

I have made adjustments to your table according to the changes specified above. Here is how your code will look like:

import React from "react";
import Table from "@mui/material/Table";
import TableCell from "@mui/material/TableCell";
import TableContainer from "@mui/material/TableContainer";
import TableHead from "@mui/material/TableHead";
import TableRow from "@mui/material/TableRow";

const data = [
  1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9,
];

export default function Test() {
  return (
    <div>
      <TableContainer
        sx={{
          marginTop: "20px",
          paddingRight: "10px",
          paddingTop: "20px",
          paddingLeft: "10px",
          border: "1px solid #ffcccb",
          height: "600px",
          width: "800px",
          "&::-webkit-scrollbar": {
            width: 15,
          },
          "&::-webkit-scrollbar-track": {
            backgroundColor: "#B5FF33",
          },
          "&::-webkit-scrollbar-thumb": {
            backgroundColor: "green",
            borderRadius: 2,
          },
        }}
      >
        <Table
          sx={{
            minWidth: "650",
          }}
          aria-label="simple table"
        >
          <TableHead>
            <TableRow>
              {data.map((item, index) => (
                <TableCell key={index}>{item}</TableCell>
              ))}
            </TableRow>
          </TableHead>
        </Table>
      </TableContainer>
    </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

Leveraging NextJS and React's Context API by incorporating a layout component wrapper

My current setup is as follows: I have a layout component where I include a navigation component and children. I enclose them within my Provider like this AppLayout.js <Layout> <Navigation/> <main> {children} </main> < ...

Exploring tabs functionality with Material UI, Jest, and Enzyme testing

I am currently testing a component with tabs. My initial test is to verify the correct initial state, followed by testing whether clicking on another tab changes the state. The technologies I am using for this are material-ui, jest, and enzyme. import Rea ...

Interactive JavaScript Slider for Customizing Selection

Recently, I came across a range slider and I am trying to make it more dynamic. The current JavaScript code has hardcoded references to specific HTML elements, and I want to have multiple sliders on my HTML page, each functioning independently. The code sn ...

Tips for maintaining the full width/height ratio of a child image when covering a fixed size container

I have a square container with dimensions of 300x300 and an <img src="" /> image inside. My goal is for the image to cover the entire container while maintaining its width/height ratio. If the image's width is smaller than its height ...

Identifying errors in a React component upon loading

As I delve into my project, my main focus lies on detecting errors within a component. The ultimate goal is to seamlessly redirect to the home page upon error detection or display an alternate page for redirection. I am seeking a programmatic solution to ...

I'm feeling lost trying to figure out how to recycle this JavaScript function

Having an issue with a function that registers buttons above a textarea to insert bbcode. It works well when there's only one editor on a page, but problems arise with multiple editors. Visit this example for reference: http://codepen.io/anon/pen/JWO ...

What is the correct method for setting the background of a Container or other MUI Components?

Hey there! I'm currently in the process of migrating my code to MUI components, and I've run into some challenges when it comes to styling. While I've found references for various MUI components like Box, Cards, and Paper, I haven't bee ...

Is there a way to display (PHP for loop variables) within an HTML table using echo?

for ($q = 1 ; $q < 7 ; $q++ ) { echo $q ; echo $row; } While the above code functions properly, I am interested in echoing two rows as shown in the image below: https://i.stack.imgur.com/7KmUY.jpg I prefer not to use another for loop. Is there a way t ...

My attempt at deploying my personal React App project on Vercel was unsuccessful

// encountering error during deployment on Vercel npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: @testing-library/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e99b8c888a9da9d8da ...

In my React JS project, I am using an <Add /> component on two distinct pages. However, I am encountering an issue where only one of the pages is correctly receiving the add count prop

I could use some assistance in understanding why the logic for my counter button is not functioning properly on one specific instance. My aim is to have the count displayed by the counter look like this: https://i.sstatic.net/VdJ8o.png In order to add it ...

The CORS policy has blocked access to XMLHttpRequest from 'localhost' to 'localhost'. A preflight response is required

I'm encountering the following error message: Access to XMLHttpRequest at 'https://localhost:44355/Brand' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access ...

Creating a PDF file from a series of images

I've implemented a function using the jsPDF library to generate a PDF from a list of images. The main task is to add these images to the PDF document. Here is the code snippet: const { allImgs } = useAppContext() const doc = new jsPDF(); const gener ...

Is it Possible to Update a React Component from a Separate Sibling Component in a Different File?

I am attempting to create a dynamic navbar that will display the user's logged-in information once they have successfully logged in. Here are the relevant code files: App.js class App extends Component { render(){ return ( <div> ...

Transferring information from a React application to a Node Express server

I'm having issues passing data from my React frontend to my Node/Express server using the onSubmit function. The data is not displaying in the console or on the page, and it simply shows up as "undefined." Can someone provide some assistance? App.js ...

React-Bootstrap columns are not displaying in a side by side manner and are instead appearing on separate lines

I am currently integrating Bootstrap into my React project alongside Material UI components. Below is a sample of one of my components: import { styled } from "@mui/material/styles"; import Paper from "@mui/material/Paper"; import Cont ...

Is it possible to change the position of the checkbox label to be above the checkbox instead

I was looking through the official documentation and saw that the Checkbox component has a prop called labelPosition which can be set to either right or left. However, I am trying to figure out if it's possible to position the label above the checkbox ...

Issue with the initial element in CSS and the cause remains a mystery

"first of type" dont like to work: .elements { width:70%; margin:0 auto; display:flex; flex-direction: column; .addElement { width:280px; text-align: center; border:1px solid black; font-family: ...

Resetting initial values with Velocity.js post-animation

After animating elements into view on a page, I want to defer further animation through CSS classes. However, Velocity keeps all animated properties in the style= tag, hindering CSS transitions. My solution involves resetting the CSS upon completion, but ...

Guide on automating the process of sending scheduled emails to registered users on various dates

Currently, I am working on a MERN Stack project and in my collection on MongoDB, each document includes fields such as: userName email lastDate The property lastDate varies for each user. My goal is to automatically send an email to each user three days ...

Issue with React Hook Form: I encountered an error when trying to display a TextField from materialUI and providing the Field with the onChange function, as it would show

Following the documentation and various online resources for help, I've attempted to implement the latest code version without success. Feeling extremely frustrated at this point. const { control, handleSubmit } = useForm() The component being retur ...