I am new to using Material-UI. In my project I was unable to override the root Material-UI root class for Avatar more specifically MuiAvatar-root.
Material-Ui Version: 4.11.3
I followed material-ui's examples as shown here to override but to no good effect.
In the Avatar below
1st
<Grid item>
<Avatar alt="avatar" src={avatar} classes={{root: classes.root }} />
</Grid>
since the one below didn't work and almost no other methods did either.
root: {
width: "128px",
height: "128px",
margin: "8px",
},
However, I was able to override this using the overrides key as below in my global theme styles
const theme = createMuiTheme({
// Not Required
overrides: {
MuiAvatar: {
root: {
width: "128px",
height: "128px",
margin: "8px",
},
}
},
palette: {
primary: {
light:'#fff',
main: '#158ade', //Curious Blue
contrastText: '#f1de39', //golden dream
dark: '#cd4315', //tia maria
},
secondary: {
light: '#DAE9F1', //Black Squeeze
main: '#D1D100', //Corn
dark: '#2D344D', //Martinique
contrastText: '#000',
},
},
});
But this also messed all the Avatars that I was using in my project which I didn't want. An example below:
2nd
<Container className={classes.center}>
<Avatar className={ classes.avatar }>
<ContactMailIcon />
</Avatar>
</Container>
const useStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
},
heading: {
color: theme.palette.primary.dark,
},
avatar: {
margin: theme.spacing(1),
backgroundColor: "#ff4838",
alignItems: 'center',
},
form: {
width: '100%', // Fix IE 11 issue.
marginTop: theme.spacing(1),
},
submit: {
margin: theme.spacing(3, 0, 2),
},
center: {
display: 'flex',
alignItems: 'center',
flexDirection: 'column'
}
}));
However, I somehow solved the issue using '&&' (came across '&' when searching about override and just worked during hit and trial) in both cases as below:
In reference to 1st
root: {
'&&': {
width: "128px",
height: "128px",
margin: "8px",
}
},
and 2nd with below
export const useContactStyles = makeStyles((theme) => ({
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
},
heading: {
color: theme.palette.primary.dark,
},
avatar: {
"&&": {
margin: theme.spacing(1),
backgroundColor: "#ff4838",
alignItems: 'center',
}
},
form: {
width: '100%', // Fix IE 11 issue.
marginTop: theme.spacing(1),
},
submit: {
margin: theme.spacing(3, 0, 2),
},
center: {
"&&": {
display: 'flex',
alignItems: 'center',
flexDirection: 'column'
}
}
}));
This did override the default MuiAvatar-root class and I was able to individually change styles, however, I'm not sure what the '&&' or even '&' does, I understand it has something to do with SASS, but I've never worked with that and how it's working here.
I just want to understand how using the double ampersands overrides the root classes. i.e. MuiAvatar-root in this case. I just made it work during hit & trial, And don't know if this is even the right way or not.
EDIT
**As explained by @Bassem and this article the '&&' is used to increase the specificity/priority.
For me, the '&&' solved my problem, and I don't think it messed up anything.
However, I am still wondering why or why not is this a good practice to use this method? Are there any downsides to using the '&&'?**