Customize the delete icon margin styles in Material-UI Chip component

Currently, I have implemented the V4 MUI Chip component with a small size and outlined variant, complete with a specified deleteIcon. However, I am encountering difficulties when attempting to override the margin-right property of the deleteIcon due to MUI's more specific styles taking precedence.

If you'd like to view the documentation for the V4 MUI Chips component, please visit: https://v4.mui.com/components/chips/

The preset styles applied by MUI to the delete icon are as follows:

.MuiChip-outlined-253 .MuiChip-deleteIconSmall-267 {
    margin-right: 3px;
}
.MuiChip-outlined-253 .MuiChip-deleteIcon-266 {
    margin-right: 5px;
}

Could someone provide guidance on how I can successfully apply a style to override the margin-right of the deleteIcon and set it to a custom value such as '10px'?

Answer №1

To customize the delete icon in a Chip, you can utilize the class .MuiChip-deleteIcon as specified in the Chip api documentation (https://v4.mui.com/api/chip/)

If you want to experiment with this, here is a functioning example on codesandbox that covers both default and outlined variants.

Below is the code snippet to implement the desired changes:

import React from "react";
import { makeStyles } from "@material-ui/core/styles";
import Chip from "@material-ui/core/Chip";
import FaceIcon from "@material-ui/icons/Face";

const useStyles = makeStyles((theme) => ({
  root: {
    display: "flex",
    justifyContent: "center",
    flexWrap: "wrap",
    "& > *": {
      margin: theme.spacing(0.5)
    },
    //Customize the CSS for the delete icon below
    "& .MuiChip-deleteIcon": {
      marginRight: "20px", // Adjust values based on your preferences
    },
    //Customize the CSS for the delete icon in outlined variant 
    "& .MuiChip-outlined": {
      "& .MuiChip-deleteIcon": {
        marginRight: "10px", // Adjust values based on your preferences
      }
    }
  }
}));

export default function Chips() {
  const classes = useStyles();

  const handleDelete = () => {
    console.info("You clicked the delete icon.");
  };

  const handleClick = () => {
    console.info("You clicked the Chip.");
  };

  return (
    <div className={classes.root}>
      <Chip label="Deletable primary" onDelete={handleDelete} color="primary" />
      <Chip
        icon={<FaceIcon />}
        label="Deletable secondary"
        onDelete={handleDelete}
        color="secondary"
      />
      <Chip
        icon={<FaceIcon />}
        label="Deletable secondary"
        onDelete={handleDelete}
        color="secondary"
        variant="outlined"
      />
    </div>
  );
}

Another approach would be to make similar changes within the theme settings.

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

Issue #98123 encountered during execution of `npm run develop` command, related to WEBPACK

I want to start a brand new Gatsby site following the instructions provided on . Here's what I did: npm init gatsby # see note below cd my-gatsby-site npm run develop Note: I didn't make any changes to the configuration, so I'm using JavaS ...

The color of the text on the Homepage appears differently on iOS Safari

At the top of my homepage, I have displayed the company phone number with white text color. It appears correctly as white on my desktop browsers (Firefox and Chrome), also on my Droid phone, but shows up as dark gray on my iOS phone using Safari. Why is th ...

How do I access and read a map within an array from Firebase using ReactJS?

I have an array that contains a map with two values: title and content. https://i.stack.imgur.com/WLWVG.png I am trying to read all the values in the array as if it were a map. However, I have attempted this code without success. Can anyone assist me? {d ...

What steps should I take to resolve the 'Uncaught Error: Cannot find module 'path'' issue in my React.js application?

While developing a web application in react.js, I encountered errors that I couldn't solve despite trying various solutions found online. The console displays the following error: Error in Console: Uncaught Error: Cannot find module 'path' ...

React JS - The error message "TypeError: this.state.data.map is not a function" indicates an

Currently, I am attempting to retrieve data from an API using axios and React JS. However, upon implementing the code, I encountered the following error message: TypeError: this.state.countries.map is not a function Within my state, I have initialized an ...

Incorporating a divider into a Material-UI MenuItem causes an additional border

I needed a way to separate the MUI MenuItem using a divider, but it resulted in an extra border around the item where the divider was placed. The highlighted section in the image shows the item that has the divider= true setting and the extra border. Is th ...

Adjusting the position of a stationary element when the page is unresponsive and scrolling

Managing a large web page with extensive JavaScript functionality can be challenging, especially when dealing with fixed position elements that update based on user scroll behavior. A common issue that arises is the noticeable jumping of these elements whe ...

Problem with React Material UI syled-engine resolution inconsistency during test execution (Functions properly during serve/build process)

Currently, I am utilizing Material UI components from mui.com with styled-components instead of the default emotion library. I have made changes to my tsconfig.json file to include: "compilerOptions": { ..., "paths": { .. ...

Unable to execute application due to invalid element type

I'm just diving into learning React Native, and as I attempt to launch my app, an error message pops up: Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Verif ...

Image hover effect displayed when hovering over an image

Is it possible to create a hover effect using purely CSS when hovering over an image, like in this example: https://i.sstatic.net/jjAWm.jpg I've come across several similar examples online, but they all seem to rely on jquery or javascript. I'm ...

Utilize the display flex property within a nested flex container

I can't seem to get my link text centered both vertically and horizontally, it keeps aligning to the left. I was under the impression that using flex-based alignments would solve this issue. I have gone through the information on using a flex item as ...

The design of Next.js takes the spotlight away from the actual content on the

Recently, I've been working on implementing the Bottom Navigation feature from material-ui into my Next.js application. Unfortunately, I encountered an issue where the navigation bar was overshadowing the content at the bottom of the page. Despite my ...

I updated the color of my a:link using jQuery, but now my a:hover is not functioning as expected

Due to the readability issues of the navigation color against a specific image, I am looking to modify it for a particular page. Below is the HTML code: <div id='nav'> <ul> <li id='navBiog'> ...

The Bootstrap toast fails to appear on the screen

I am currently working on a website project using HTML with bootstrap and javascript. I have been attempting to include a toast feature by implementing the code provided on the bootstrap website: <div class="toast" role="alert" aria-live="assertive" ...

I am attempting to access and display the properties of a higher-level component within a nested component

Trying to access the props passed to my parent component using this.state.props in my child component. Searching high and low on the internet for a solution. Choose(){ console.log(this.props.size); } {this.props.size && <DropdownIte ...

What are some techniques for applying masking to various elements beyond just input fields within React?

I am currently developing an admin dashboard using NextJS and MaterialUI (mui), and I am facing a challenge with masking certain values, such as phone numbers, that are retrieved from the backend without any masking applied. While I have successfully impl ...

When a React component written in TypeScript attempts to access its state, the object becomes

Throughout my app, I've been consistently using a basic color class: const Color = { [...] cardBackground: '#f8f8f8', sidebarBackground: '#eeeeee', viewportBackground: '#D8D8D8', [...] } export defau ...

When attempting to utilize Link in a Mapbox GL JS popup, an error is thrown: Invariant failed - It is advised not to use <Link> outside a <Router>

Currently, I am developing a React (16) Single Page Application that incorporates a Mapbox GL JS map with markers. The issue I am facing involves attempting to display a Link within each marker popup, causing the following error message to appear: Error: I ...

How to Increase the Font Size of a TextField in Material UI using a Functional Component

I have a functional component with a TextField that I want to adjust the font size of, but I'm struggling to do so. Typically with a regular component, I could use withStyles and modify the className or InputProps, but I'm unsure how to approach ...

How can I reset the mousemove event in jQuery?

I need to create a mechanism where I can move a div by clicking on it, and then click again to stop the div in that position. However, encountering an issue when trying to move the div again as the mousemove event does not get activated. How can this be ...