css 在MUI v5中覆盖排版变化样式

rbl8hiat  于 2023-03-25  发布在  其他
关注(0)|答案(3)|浏览(183)

我有一个与Can't override root styles of Typography from Materil-UI类似但不同的问题
在v4中我有一个主题:

import { createTheme } from '@material-ui/core/styles';

export const theme = createTheme({
  palette: {
    type: 'light'
  },
  typography: {
    fontFamily: ['Work Sans', 'sans-serif'].join(','),
    fontSize: 14,
    h1: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 20
    },
    h2: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 18
    },
    h3: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 16
    },
    h4: {
      fontFamily: ['Rubik', 'sans-serif'].join(','),
      fontSize: 16
    }
  }
});

这很好地覆盖了<Typography variant='h2'>h2 heading</Typography>的字体大小
在升级到v5时,此覆盖不再起作用
我还尝试添加一个组件部分的主题

components: {
    MuiTypography: {
      styleOverrides: {
        h2: {
          color: 'red',
          fontSize: '18px'
        }
      }
    }
  }

这也行不通。
我注意到,HTML中添加了一个新类,它覆盖了以前所有的CSS
需要什么才能根据v4覆盖所有版式变体?Extra class added这不是v4 HTML中的内容

imzjd6km

imzjd6km1#

使用样式覆盖,可以覆盖变量,如下所示:

export const theme = createTheme({
  components: {
    MuiTypography: {
      variants: [
        {
          props: { variant: 'h2' }, /* component props */
          style: {
            /* your style here: */
            color: 'red',
          },
        },
      ],
    },

这对我很有效,希望对你有帮助:)

s2j5cfk0

s2j5cfk02#

app.ts中我有

import { ThemeProvider } from '@mui/styles';

本该如此

import { ThemeProvider } from '@mui/material/styles';
ffvjumwh

ffvjumwh3#

我做了上面的,仍然没有改变。只有当我也删除了我的package.json中的任何MUI V4引用时,它才为我整理出来。希望这对某人有帮助:)

相关问题