【问题标题】:Overring Material-UI style with makeStyles使用 makeStyles 覆盖 Material-UI 样式
【发布时间】:2022-01-12 19:27:46
【问题描述】:

我正在开发一个小型作品集网站,我添加了一个头像,并尝试使用 makeStyles 类覆盖它的样式,但 Mui 头像根覆盖了我的类,

不使用 !important 是否可以做到这一点?

export const HomePage = () => {
    const classes = useStyles()
    return (
        <Grid container justifyContent="center">
            <Avatar className={classes.headerAvatar} src={avatar} alt="" />
        </Grid>
    )
}


export const useStyles = makeStyles(theme => ({
headerAvatar: {
        width: theme.spacing(13),
        height: theme.spacing(13),
        margin: theme.spacing(1)
    },
}))

【问题讨论】:

    标签: css reactjs material-ui


    【解决方案1】:

    根据docs,您可以使用classes 属性覆盖root 类的css 样式,例如:

    export const HomePage = () => {
        const classes = useStyles()
        return (
            <Grid container justifyContent="center">
                <Avatar classes={classes} src={avatar} alt="" />
            </Grid>
        )
    }
    
    
    export const useStyles = makeStyles(theme => ({
      root: {
            width: theme.spacing(13),
            height: theme.spacing(13),
            margin: theme.spacing(1)
        },
    }))
    

    【讨论】:

    • 谢谢,我之前尝试过,但显然我的错误是在 & 之后添加了一个空格,例如现在这项工作 - '&.MuiAvatar-root': { width: theme.spacing(13 ),高度:theme.spacing(13),边距:'0.5rem auto',},
    • 你真的不需要使用Global class,如果你使用classes而不是className,你可以像我的例子一样使用Rule name
    猜你喜欢
    • 2021-07-28
    • 2022-01-06
    • 1970-01-01
    • 2020-01-30
    • 2019-03-07
    • 2020-05-27
    • 2020-08-10
    • 2021-06-30
    • 1970-01-01
    相关资源
    最近更新 更多