Customizing Material-UI elements in React using CSS styling

My goal is to style all the <Chip> elements within a <Grid> that has the class .table-header, but they are not changing their style (I have a <p> that should be colored in red and it works). Why does the styling work for the <p> element but not for the <Chip>? I want to achieve this without adding the same class to each individual <Chip>.

I am using MUI v5.11.4.

Here is a portion of the tsx code:

<Grid className={classes['table-header']}>
  <Grid item xs={1} />
  <Grid item xs={2}>
    <Chip avatar={<Avatar>1</Avatar>} label="Element1" />
    <p>Red text</p>
  </Grid>
  <Grid item xs={4}>
    <Chip avatar={<Avatar>2</Avatar>} label="Element2" />
  </Grid>
  <Grid item xs={5} />
</Grid>

And here is the module.css code:

.table-header Chip {
  width: 100%;
}

.table-header p {
  color: red;
}

CODE SANDBOX

Answer №1

To ensure your selectors are set up correctly, follow these steps to style the component for the Chip:

// Import styled components
import { styled } from "@mui/system";

// Create the styled component for the Chip
const StyledChip = styled(Chip)({
    "& .MuiChip-label": {
      color: "red"
    }
});

// Implement it in your code like this example:
<StyledChip avatar={<Avatar>1</Avatar>} label="Element1" />

Check out the live demo of your solution on codesandbox.

UPDATE: If you prefer not to use styled components, you can achieve the same result with this CSS selector:

.table-header :global .MuiChip-label {
  color: red;
}

Make sure to denote the MuiChip-label class as :global.

See the alternative solution in action on codesandbox.

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

Several socket.io sessions have been initiated

I'm fairly new to working with node.js and currently attempting to set up a server using socketio to send messages to the frontend (React). However, when running the server and multiple connections are being established, I encounter the following outp ...

What is the method for centering an input field with inline CSS?

Is there a way to center align an input text box on a webpage using only inline CSS? I have tried various techniques, including the following code snippet, but nothing seems to work: <input type="text" id="myInput" style= "margi ...

Customize the color of the Material-UI Rating Component according to its value

Objective I want to dynamically change the color of individual star icons in a Ratings component (from material-ui) based on the value selected or hovered over. For example, hovering over the 1st star would turn it red, and hovering over the 5th star woul ...

Unable to align text within a DIV using CSS

Recently, I started learning jQuery and building my own website. You can find my project on JSFIDDLE. The issue I'm facing is that when hovering over a new div, the text extends beyond the borders of that div instead of staying within it. I've sp ...

How can I align the text in a dropdown menu to the center on Safari?

How can I center the option text in a drop down menu using CSS? display: block; height: 60px; margin-left: 10%; margin-right: 10%; margin-top: 10px; min-height: 60px; text-align: center; The option text is centered in Firefox browser, but not in Safari. ...

The previous page retains the outdated value of useState

Each page is subjected to an AuthGuard component that determines whether child components can be displayed or if the user should be redirected to a different page. While it functions well upon initial page load, I have observed some unusual behavior when ...

Deploying a NextJS blank page in production build exclusively within a Docker environment

Encountering a peculiar issue that surfaced unexpectedly on December 7th, 2021 around midday EST. The problem revolves around a NextJS application deployed within a Docker container. After deploying the app in production mode from inside the container, a b ...

Updating the Navbar title in React depending on the displayed page

I am using a BrowserRouter component as my top level element, with a title string stored in its state. Within this component, I have a NavigationComponent that functions as a navigation bar. I want the title displayed in the NavigationComponent to dynamica ...

Switching a class component to a functional component with react hooks (specifically useRef) - tips for preventing the dreaded "undefined" error

I have a code snippet that works as a class component and I'm trying to convert it into a functional component using the react-rewards library. Class component (working): import { Checkbox } from "@chakra-ui/react"; import React, { Compone ...

Strategies for successfully passing and showcasing the result of a calculation on a fresh view or component within Angular

I have developed a basic calculator application with two main components: Calculator and Result. The Angular router is used to navigate between these components. Now, I am looking for a way to dynamically display the calculation result from the Calculator ...

Why won't the CSS style in my HTML file display properly in Django?

I've been encountering an issue with my CSS file not working in Django, and I'm unsure of the reason behind it. Despite numerous attempts to troubleshoot, the problem persists. I have included {%load static%} at the beginning of my HTML file, and ...

Is it possible to utilize fetch from the local drive in conjunction with App Router in the latest version of Next JS?

I encountered a 404 error and realized that the expected response from an on message listener had gone out of scope. Initially, I was working on creating an API for user authentication and was advised to convert it to route.js. So, I made the necessary ch ...

Implementing an animation feature to facilitate the item filtering process when clicked on

I have recently developed a filter gallery and I am encountering an issue with animating the filter items when clicking on buttons. The problem with my current code is that it only toggles the animation effect rather than smoothly animating each time a but ...

How to bring in images from a local source in a React.js file

I've been looking into relative paths and absolute paths, but I'm having trouble importing local images correctly. My main objective is to create a React slideshow using these images. Why does it keep trying to find the images in the "components" ...

An unusual character "" popped up while working with react native on iOS platform

Currently, I am in the process of learning how to use ReactNative for iOS. I have completed all the initial setup steps and am now following a tutorial for basic understanding. React Native However, I have encountered an error during this learning process ...

What is the best way to align my Navbar in the center?

Previously, I searched on this platform for solutions but couldn't find anything that worked. Currently, I am using the Bulma Framework which is not very well-known. However, I need assistance in aligning the brand link on the navbar to the center. Be ...

unable to use ref to scroll to bottom

Can someone explain to me why the scroll to bottom feature using ref is not functioning properly in my code below? class myComponent extends Component { componentDidMount() { console.log('test') // it did triggered this.cont ...

When deciding between utilizing a Javascript animation library and generating dynamically injected <style> tags in the header, consider the pros and cons of each

We are currently in the process of developing a sophisticated single-page application that allows users to create animations on various widgets. For example, a widget button can be animated from left to right with changes in opacity over a set duration. Ad ...

hover over the picture with your cursor

Struggling with jquery and css, could use some assistance :) Check out my html code below: <html> <head> //myscripts <script> $(document).ready(function(){ $(".cover").hover(function(){ //the function i need to write } ...

There was a TypeError encountered while trying to read properties of undefined in Next.js version 13

I've encountered a problem in the title with my post API endpoint that I created in Next.js. The purpose of my endpoint is for form submission, where I gather inputs and send them to my email. Currently, my code successfully sends the email and I rec ...