【问题标题】:How to change fontWeight globally in MaterialUI/React如何在 Material UI/React 中全局更改字体粗细
【发布时间】:2022-01-18 08:10:01
【问题描述】:

我想在我的 Material UI React 项目中全局使用 fontweight 300,但是默认设置是 400。我只是想出了如何在特定组件上覆盖 fontWeight(比如下面代码中的 h3),所以我寻求帮助为我将在未来使用的所有 Material UI 组件全局设置它。

overrides: {
            MuiTypography: {
               h3: {
                fontWeight: 300
            }},
            MuiTableCell:{
              root:{
                fontWeight: 200
              }
            },
          },

在下面的代码示例中,我使用 createMuiTheme 同时更改所有组件的 FontFamily 和 borderRadius,但我不知道如何同时为所有 Material UI 组件设置 FontWeight。

const theme = createMuiTheme({
  typography: {
    fontFamily: [
      'Sora',
      'sans-serif'
    ].join(','),
  },
  shape:{
    borderRadius:12
  },

【问题讨论】:

    标签: reactjs material-ui


    【解决方案1】:

    tru 将"" 添加到fontWeight

     h3: {
      "fontWeight": 200,
    },
    

    【讨论】:

    • 事实并非如此。我可以将 FontWeight 设置为像 h3 这样的特定标题,但我找不到一个公式可以同时对所有 Material UI 组件执行此操作。我可以使用 createMuiTheme 更改所有组件的 FontFamily 和 borderRadius,但我不知道如何同时为所有组件设置 FontWeight。
    【解决方案2】:

    您可以在主题的排版配置中使用allVariants 属性:

    import { createTheme } from "@mui/material/styles";
    
    // Create a theme instance.
    const theme = createTheme({
      typography: {
        allVariants: {
          fontWeight: 300,
        },
      },
    });
    
    export default theme;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-28
      • 2019-09-05
      • 2021-01-20
      • 2021-01-23
      • 2014-07-16
      • 2018-05-07
      • 1970-01-01
      • 2021-03-07
      相关资源
      最近更新 更多