Selector for CSS variant of target button in Material UI

For each type of Material UI button variant, I have created 3 classes that apply a gradient: gradient, outlinedGradient, and containedGradient,

https://i.sstatic.net/paMvR.png

Instead of having to manually change the gradient class when altering button styles, I wanted to create a single class gradient and then develop more specific selectors for default, outlined, or contained buttons.

I attempted using the selectors

"& .MuiButton-contained":
and & .MuiButton-outlined, but the styles did not take effect.

https://i.sstatic.net/gwZGp.png

Not working:

const styles = (theme) => ({
  root: {
    padding: theme.spacing.unit
  },
  button: { margin: theme.spacing.unit },
  gradient: {
    background: theme.palette.primary.mainGradient,
    "-webkitBackgroundClip": "text",
    "-webkitTextFillColor": "transparent",
    "& .MuiButton-contained": {
      "-webkitBackgroundClip": "unset",
      "-webkitTextFillColor": "black"
    },
   "& .MuiButton-outlined": {
     ...more styles
    }
  }
});

CodeSandbox:

Is it possible to achieve this? Can we target these button variants in CSS within Material UI?

Answer №1

The solution can be found here

When referencing the CSS class, remember that MuiButtonBase-root is on the root element. Use &.MuiButtonBase-root (without a space after the ampersand) for the correct syntax.

To make it work, simply remove the space in the selectors like

"&.MuiButton-contained"
, not
"& .MuiButton-contained"

Answer №2

To add your own personal touch to MUI components, start by deactivating the default style with the disable method. I discovered the solution [here]. 1 similar to this approach

<Button disable id="button"></Button>   

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

Clear backdrop with solid objects

I am trying to achieve a unique design where the body has an image background, and the main div has a semitransparent background. I want the image to be visible through the main div, but in a shaded manner. However, the traditional approach may cause every ...

Unable to display the attributes of an object using console.log

I am attempting to log the properties of an object in TypeScript, but I am encountering issues. setTitleAndBody(result: { title: String; body: String; }) { console.log(result) console.log(result.title) } What's interesting is that if I only l ...

Is there a way for me to make this Select update when onChange occurs?

I am facing an issue with a react-select input that is supposed to display country options from a JSON file and submit the selected value. Currently, when a selection is made, the field does not populate with the selection visually, but it still sends the ...

JavaScript Scroller that keeps on scrolling

Unique Continuous JavaScript Scroller I am currently working on a JavaScript scroller script that enables left to right and right to left scrolling on the click of next and previous buttons. The script is designed to work with 1 to 3 div elements in a con ...

Can floating elements be disregarded by block elements?

According to W3C, the behavior of floating elements is such that: When a float is present, non-positioned block boxes that come before and after the float flow vertically as if the float doesn't exist. However, line boxes positioned next to the fl ...

What kind of data type should the value property of Material UI TimePicker accept?

While reviewing the documentation, I noticed that it mentions any, but there is no clear indication of what specific data types are supported. The value sent to the onChange function appears to be an object rather than a Date object, and in the TypeScrip ...

Transforming Material UI into a class-based component

How can I transform a React function into a class? I'm having trouble understanding how to adjust const { classes } = props; in a function to work within a class. You can find an example button function from Material UI here: import React from &apos ...

A guide on personalizing HTML for Django forms

I have implemented an HTML template on my website and need to capture information for my Django backend. Specifically, I am trying to extract the email address from this section of code: <input type="text" placeholder="Email" value="" style="height: 30 ...

Switching from single to multiSelect in Material UI Autocomplete depending on a certain condition

Encountering an issue with Material UI Autocomplete where changing from single select to multiple select based on a condition throws the error Cannot read property 'length' of null Check out this CodeSandbox example: https://codesandbox.io/s/mat ...

Streamlining programming by utilizing localStorage

Is there a more efficient way to streamline this process without hard-coding the entire structure? While attempting to store user inputs into localStorage with a for loop in my JavaScript, I encountered an error message: CreateEvent.js:72 Uncaught TypeErr ...

The Bootstrap select feature does not override the original dropdown menu; instead, it adds its own functionality alongside it

I have implemented a select combo using this library. After adding the necessary CSS and JS files, I encountered the following result. Here is what I have attempted so far: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.mi ...

Retrieving elements from the DOM based on their class name

Currently utilizing PHP DOM for my project and facing the challenge of retrieving an element within a DOM node with a specific class name. What would be the most effective method to accomplish this? Update: Ultimately decided to switch to using Mechanize ...

Tips for preventing a bootstrap card image from overflowing in the card-body

I'm currently working with bootstrap cards and I've been able to prevent the card image from overflowing outside of the card area. However, I'm facing an issue where the bottom of the image overflows into the card-body region. I'm not s ...

Troubleshooting Media Queries in HTML, CSS, Bootstrap: Why Aren't They Applying as Expected?

I am still fairly new to web programming and just starting out with bootstrap. After creating a section of my page that worked well on larger screens, I realized it wasn't responsive enough on smaller devices. The height was set at 400px, causing hor ...

Achieve a gradient effect for the border color of MUI Checkbox

I'm currently working on customizing the appearance of a MuiCheckBox component by using styleOverrides. My goal is to create linear gradient borders for it. Any suggestions on how I can achieve this? MuiCheckbox: { styleOverrides: { root: { ...

Troubleshooting ReactJs in Chrome during page load: encountering issues with undefined properties when utilizing the console

When debugging React on initial page load, I encounter a problem. Upon hovering over this, I can see the content, but using the console shows that this is undefined. Interestingly, this issue only arises when I refresh the page; clicking around does not tr ...

Ensuring a child element fills the height of its parent container in React Material-UI

Currently, I am in the process of constructing a React Dashboard using MUI. The layout consists of an AppBar, a drawer, and a content area contained within a box (Please correct me if this approach is incorrect)... https://i.stack.imgur.com/jeJBO.png Unf ...

"Troubleshoot: Issue with React's useState hook not correctly updating state within an

I'm running into an issue with updating state using the useState hook in a React functional component. The problem arises when I have an async function (getPalettesPosition) that fetches data based on the current state (paletteFilters). Despite confir ...

modifying CSS of a div element on a designated page position

A unique code snippet that I have positions a button fixed to the top of the page, causing it to overlay content or images as the user scrolls the page. HTML <div id="btn">button</div> ... images ... ... content ... CSS #btn { positio ...

Using a jQuery event to apply styles with CSS

Hello, I am using jQuery Easy UI on my webpage but feeling a bit confused. I want to create an input field that looks like a tagging field - meaning when the user types a separator character (let's say a comma), the word being typed will have its CSS ...