简体   繁体   English

如何更改reactjs中材质ui徽章内容的字体大小?

[英]How to change font size of material ui badge content in reactjs?

I want to change the font size of the label in material ui badge.我想在材料 ui 徽章中更改 label 的字体大小。 I am using style={{ fontSize: "15" }} but that only affect its child, which is an icon.我正在使用style={{ fontSize: "15" }}但这只会影响它的孩子,这是一个图标。

Code:代码:

<Badge badgeContent={props.cartCount} color="secondary" style={{ fontSize: "15" }}>
    <ShoppingCart className={classes.mediumIcon} />
</Badge>

Ideal way would be use classes badge as mentioned in documentation理想的方法是使用文档中提到的类badge

import { Badge } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
import "./styles.css";

const useStyles = makeStyles((theme) => ({
  badge: {
    fontSize: 30
  }
}));

export default function App() {
  const classes = useStyles();
  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Badge
        badgeContent={"h"}
        color="secondary"
        classes={{ badge: classes.badge }}
      />
    </div>
  );
}

You can modify the font-size as below.您可以如下修改字体大小。 create styles of different font-size in useStyles在useStyles中创建不同字体大小的useStyles

const useStyles = makeStyles((theme) => ({
  font1: {
    fontSize: "1rem"
  },
)}

And then assign it as below to the Badge component然后将其分配给Badge组件,如下所示

<Badge
        classes={{
          badge: classes.font1
        }}
        badgeContent={99}
        {...defaultProps}
      />

Sandbox 沙盒

If you use mui system then this can change badge font size如果您使用 mui 系统,那么这可以更改徽章字体大小

    <Badge
        badgeContent={9}
        color="error"
        overlap="circular"
        sx={{ "& .MuiBadge-badge": { fontSize: 9, height: 15, minWidth: 15 } }}
    >
        <IconButton sx={{ p: 0, color: "primary.main" }}>
            <Notifications />
        </IconButton>
    </Badge>

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

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