简体   繁体   English

无法覆盖嵌套 Material UI 组件的 styles

[英]Cant override styles of nested Material UI components

I am using the card and cardContent component of material ui.我正在使用材质 ui 的 card 和 cardContent 组件。 I have both in functional components and am trying to override the root style of each.我在功能组件中都有,并且正在尝试覆盖每个组件的根样式。 however, I cant figure out how to modify the css of the cardContent component.但是,我不知道如何修改 cardContent 组件的 css。 It seems like that by modifying the root style of card.通过修改card的根样式似乎是这样的。 it wont let me modify the root style of cardcomponent.它不会让我修改 cardcomponent 的根样式。 instead my css shows up in the inspector as being in相反,我的 css 在检查器中显示为

.jss14 {
    height: 100%;
    display: flex;
    padding: 0;
    flex-direction: column;
    justify-content: space-between;
}

rather than being in the .MuiCardContent-root而不是在.MuiCardContent-root

Is there something i am missing with using makeStyles?使用makeStyles有什么我遗漏的吗?

my attempt我的尝试

import React from "react"
import { makeStyles } from "@material-ui/core/styles"

import CardContent from "@material-ui/core/CardContent"

const useStyles = makeStyles({
  root: {
    display: "flex",
    justifyContent: "space-between",
    flexDirection: "column",
    height: "100%",
    padding: 0,
  },
})

export default function AccountCardContent(props) {
  const classes = useStyles()

  return <CardContent className={classes.root}> {props.children}</CardContent>
}

import React from "react"
import { makeStyles } from "@material-ui/core/styles"
import Card from "@material-ui/core/Card"

import AccountCardContent from "../AccountCardContent"

const useStyles = makeStyles({
  root: {
    width: "324px",
    height: "194px",
    borderRadius: "8px",
  },
})

export default function AccountCard({ icon, title, description, onClick }) {
  const classes = useStyles()

  return (
    <Card className={classes.root} onClick={onClick}>
      <AccountCardContent>asdf</AccountCardContent>
    </Card>
  )
}

Basically, the Material UI components accepts a classes prop that can be used to override its styles.基本上,Material UI 组件接受可用于覆盖其 styles 的classes属性。

Please check it out here在这里查看

So you can this:所以你可以这样:

<Card
  classes={{
    root: classes.root, // class name, e.g. `classes-nesting-root-x`
  }}
>

Your code is correct for the most part and your problem is not related to MUI or React, but CSS specificity .您的代码大部分是正确的,并且您的问题与 MUI 或 React 无关,而是与 CSS specificity相关。 You are trying to overwrite您正在尝试覆盖

.MuiCardContent-root:last-child

and the added pseudo class results in a higher specificity than your .jss14 ( makeStyles-root-14 in development) class.并且添加的伪 class 导致比您的.jss14更高的特异性( makeStyles-root-14正在开发中)class。 Ideally you always mimic the selector you are trying to overwrite, in this case:理想情况下,您总是模仿您尝试覆盖的选择器,在这种情况下:

  root: {
    // other styles
    "&:last-child": {
      paddingBottom: 0
    }
  }

and in case of doubt, you can simply increase specificity step by step by adding additional & to your selector until it works:如果有疑问,您可以通过在选择器中添加额外的&来逐步增加特异性,直到它起作用:

  root: {
    // other styles
    "&&": {
      paddingBottom: 0
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM