Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI Alerts using custom colors

Tags:

material-ui

Can I set a custom color for a MUI Alert component? The docs suggest that the only color options are the four that match the four severity props.

like image 788
mcmatt Avatar asked Aug 28 '26 15:08

mcmatt


2 Answers

For MUI V5 in case someone is looking to change the background color in the theme this worked for me.

export const theme = createTheme({
  components: {
    MuiAlert: {
      styleOverrides: {
        standardSuccess: {
          backgroundColor: 'green',
          color: 'white'
        },
        standardError: {
          backgroundColor: 'red',
          color: 'white'
        },
        standardWarning: {
          backgroundColor: 'orange',
          color: 'white'
        },
        standardInfo: {
          backgroundColor: 'grey',
          color: 'black'
        }
      }
    },
    
  },
});

And is working by setting the variant:

<Alert severity="success">Success</Alert>
<Alert severity="error">Error</Alert>
<Alert severity="warning">Warning</Alert>
<Alert severity="info">Info</Alert>
like image 169
Akis Avatar answered Sep 02 '26 03:09

Akis


It's possible. Quoting the API docs:

You can override the style of the component thanks to one of these customization points:

  • With a rule name of the classes object prop.
  • With a global class name.
  • With a theme and an overrides property.

A crude example in this codesandbox

like image 30
Gustav Avatar answered Sep 02 '26 05:09

Gustav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!