I am struggling to apply custom CSS styles to the scrollbar within a Card component using MUI react

import React from "react";
import Card from "@mui/material/Card";
import CardActions from "@mui/material/CardActions";
import CardContent from "@mui/material/CardContent";
import CardMedia from "@mui/material/CardMedia";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid";
import ShareIcon from "@mui/icons-material/Share";
import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
import { makeStyles } from '@mui/styles';

const useStyles = makeStyles({
  scrollBar: {
    "::-webkit-scrollbar": {
      width: "3px",
    },

    "::-webkit-scrollbar-track": {
      boxShadow: "inset 0 0 5px rgb(255, 251, 251)",
      borderRadius: "10px",
    },
 
    "::-webkit-scrollbar-thumb": {
      background: "#077DFA",
      borderRadius: "10px",
    },

    "::-webkit-scrollbar-thumb:hover": {
      background: "rgb(255, 251, 251)",
    }
  }
});

export default function MediaCard(props) {
  const classes = useStyles()
  
  return (
    
    
        <Grid item md={4} >
          
          <Card className={classes.scrollBar} sx={{ maxWidth: 345 , maxHeight : 345, overflowY: "scroll"}}>
            <CardMedia
              component="img"
              height="190"
              image={props.img}
              alt={props.alt}
              
            />
          
            <CardContent>
              <Typography gutterBottom variant="h5" component="div">
                {props.headline}
              </Typography>
              <Typography variant="body2" color="text.secondary">
                {props.content}
              </Typography>
            </CardContent>
            <CardActions>
              <Button
                size="small"
                href=""
                variant="contained"
                endIcon={<ShareIcon />}
              >
                {" "}
                Share
              </Button>
              <Button
                sx={{ marginLeft: ".5rem" }}
                target="_blank"
                size="small"
                variant="contained"
                href={props.learnMore}
                endIcon={<LocalLibraryIcon />}
              >
                Learn More
              </Button>
            </CardActions>
          </Card>
        </Grid>     
  );
}

While incorporating custom scrollbar styles using the makeStyles function in React and applying the defined classes to the Card component, I am encountering an issue where the scrollbar styles are not being applied correctly. What could be causing this problem?

Answer №1

Enhance the default scroll bar using these CSS codes

::-webkit-scrollbar {
  width: 8px;
}

::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  -webkit-border-radius: 5px;
  border-radius: 5px;
}

::-webkit-scrollbar-thumb {
  -webkit-border-radius: 5px;
  border-radius: 5px;
  background: #BB2020;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}

::-webkit-scrollbar-thumb:window-inactive {
  background: #BB2020;
}

.scroll-box {
  overflow-y: scroll;
  height: 200px;
  border: 1px solid #ccc;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div class="scroll-box">
      <p>
        Transform the default scroll bar with these CSS codes to achieve a customized look and feel.
      
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas vero,
        tempore earum blanditiis corporis nesciunt pariatur, et illo quidem
        ipsum minus perferendis! Deleniti sapiente, doloremque eaque sit id
        similique nulla.
        
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Quas vero,
        tempore earum blanditiis corporis nesciunt pariatur, et illo quidem
        ipsum minus perferendis! Deleniti sapiente, doloremque eaque sit id
        similique nulla.
        
      </p>
    </div>
  </body>
</html>

Answer №2

Make sure to include the ampersand (&) before the colon in the scrollBar css code block. Give it another shot:

import React from "react";
import Card from "@mui/material/Card";
import CardActions from "@mui/material/CardActions";
import CardContent from "@mui/material/CardContent";
import CardMedia from "@mui/material/CardMedia";
import Button from "@mui/material/Button";
import Typography from "@mui/material/Typography";
import Grid from "@mui/material/Grid";
import ShareIcon from "@mui/icons-material/Share";
import LocalLibraryIcon from "@mui/icons-material/LocalLibrary";
import { makeStyles } from '@mui/styles';

const useStyles = makeStyles({
  scrollBar: {
    "&::-webkit-scrollbar": {
      width: "3px",
    },

    "&::-webkit-scrollbar-track": {
      boxShadow: "inset 0 0 5px rgb(255, 251, 251)",
      borderRadius: "10px",
    },
 
    "&::-webkit-scrollbar-thumb": {
      background: "#077DFA",
      borderRadius: "10px",
    },

    "&::-webkit-scrollbar-thumb:hover": {
      background: "rgb(255, 251, 251)",
    }
  }

});

export default function MediaCard(props) {
  const classes = useStyles()
  
  return (
    
    
        <Grid item md={4} >
          
          <Card className={classes.scrollBar} sx={{ maxWidth: 345 , maxHeight : 345, overflowY: "scroll"}}>
            <CardMedia
              component="img"
              height="190"
              image={props.img}
              alt={props.alt}
              
            />
          
            <CardContent>
              <Typography gutterBottom variant="h5" component="div">
                {props.headline}
              </Typography>
              <Typography variant="body2" color="text.secondary">
                {props.content}
              </Typography>
            </CardContent>
            <CardActions>
              <Button
                size="small"
                href=""
                variant="contained"
                endIcon={<ShareIcon />}
              >
                {" "}
                Share
              </Button>
              <Button
                sx={{ marginLeft: ".5rem" }}
                target="_blank"
                size="small"
                variant="contained"
                href={props.learnMore}
                endIcon={<LocalLibraryIcon />}
              >
                Learn More
              </Button>
            </CardActions>
          </Card>
        </Grid>     
  

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

How can I declaratively bind the properties in Dojo's _hasDropDown method?

UniqueSearchComponent.html: <div class="${baseClass}"> <div data-dojo-type="dijit/_HasDropDown" data-dojo-props="dropDown: 'containerNode'"> <div data-dojo-type="dijit/form/TextBox" name="${SearchViewFieldName} ...

What is an easy method for including a broader div within a Y-scrolling container containing multiple divs?

Here is a scenario for consideration: http://codepen.io/anon/pen/NPGKPq Is there a CSS approach, without incorporating Javascript, to display the full width of the blue div? Essentially, rather than having a horizontal scrollbar, only show a vertical scr ...

What are some ways to achieve a smoother hover effect?

.proposal p{ font-size: 14px; color: #494949; margin-bottom: 15px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; line-height: 20px; max-height: 100px; -webkit-line-clamp: 3; -webkit-box-orient: vertical; } .proposal: ...

Update the .erb code with Ajax after successfully creating the object

What is the best way to use Ajax in Rails 4 to automatically refresh the code in ".erb" files after successfully creating a new "Box"? The relevant file is located in: views/modifications/show.html.erb <tbody id="boxes_count"> <% @modificat ...

Modifying dynamic input fields based on the selected input field type

Seeking advice on a challenge I'm facing while testing a website. My task is to mask input fields in screenshots after executing tests because we share data with other teams. I've tried using JS before the script by changing the input type to &ap ...

Customizing YearPicker style in MUI DatePicker - Tips and tricks

While playing around with the DesktopDatePicker component and exploring ways to customize it, I came across an issue. It appears that there is no documentation on how to style the YearPicker within the DesktopDatePicker. When customizing the PickersDay co ...

How can you selectively display a server component based on the state of a client component?

How can I conditionally display a server component based on the state of a client component? Components involved Parent server component Get Child client component Button Child server component AverageSalary Objective When clicking on a <button> ...

Display iframe as the initial content upon loading

Seeking solutions for loading an iframe using jQuery or Ajax and outputting the content to the page once it has been loaded. The challenge lies in loading an external page that may be slow or fail to load altogether, leading to undesired blank spaces on th ...

The function cannot be accessed during the unit test

I have just created a new project in VueJS and incorporated TypeScript into it. Below is my component along with some testing methods: <template> <div></div> </template> <script lang="ts"> import { Component, Vue } from ...

Error: JSON data contains an unexpected token '<', which is causing a SyntaxError and making the JSON invalid

This PERN application is throwing an error that I have never encountered before, and after searching on Google, I couldn't find any relevant information. Upon reviewing the index.html file, everything seems to be in order: <!DOCTYPE html> <h ...

What is the best way to pass a variable between different routing functions?

I am currently developing a server-side parser for an API. Each GET request made to my website must first initiate a request to the API, and since this request is always the same, I would like to encapsulate it within its own function. What is the best wa ...

Troubleshooting problem with CSS scaling on smartphones

I'm attempting to create a website (the first one I've ever designed) dedicated to my girlfriend's cat. However, when viewed on a mobile device, it doesn't look good. I've made an effort to adjust the meta viewport tag, but it does ...

What is the best way to dynamically disable an input based on the value of another input in "React-hook-form"?

I'm looking for a way to conditionally disable an input based on the value of another input. This is commonly needed when we have a checkbox that determines whether or not another input should be enabled in the same form. How can I achieve this using ...

Error message: Unable to iterate through a non-iterable object in React reducer

I find myself in a unique predicament and could use some assistance. userData : { isValidCheckup: true, accounts: { userAccount: [ { accountType: 'checkings', includeInCheckup: false }, { accountType: 'check ...

Reduce the flexbox container to match the dimensions of its child elements

I have a set of blocks with fixed dimensions that I want to arrange in a grid layout. Using flex-wrap: wrap, I can distribute as many blocks as possible in each row. However, I need the entire wrapped column to be centered on the page, similar to this: Th ...

What is the best way to resume a Jquery function if it has not

How do I make my form alert re-trigger when the user clicks the button again if the input is still empty? What I've tried: After clicking the button, it checks if the inputs are empty and shows an alert. However, once the alert appears, if I click th ...

Clear out any unnecessary white space beneath the content on your webpage

I am relatively new to Web Development and currently following Colt Steele's udemy course. I have been working on a landing page, but as I try to set the height of HTML to 100%, it leaves whitespace at the bottom. After some research, I suspect that t ...

what is the method to extract the value of a JSON object nested within another JSON object

Can someone please assist me? "_attachments": { "kiran.jpg": { "content_type": "image/jpeg", "revpos": 6, "digest": "md5-mEsoX4ljN1iJlF2bX1Lw2g==", "length": 4601, "stub": true } } I ...

Ways to obtain a referrer URL in Angular 4

Seeking a way to obtain the referrer URL in Angular 4. For instance, if my Angular website is example.com and it is visited from another PHP page like domaintwo.com/checkout.php, how can I detect the referring URL (domaintwo.com/checkout.php) on my Angul ...

Guide to enabling cross-domain uploads of files

After following a tutorial, I successfully implemented an HTML5 uploader on my website. The source of the tutorial can be found here: The uploader works perfectly; however, I am now facing an issue where I want to upload files to a different domain. Accor ...