简体   繁体   English

为什么 Material UI CSS 类中的属性会覆盖自定义 CSS 中的属性?

[英]Why are properties in Material UI CSS classes overriding properties in custom CSS?

I was trying to use a npm package which has a Typography element from Material UI.我正在尝试使用 npm package,它具有来自 Material UI 的 Typography 元素。 This is written by me.这是我写的。 When I try to use it in a project, the typography css class properties override the custom css properties.当我尝试在项目中使用它时,排版 css class 属性会覆盖自定义 css 属性。 An example is margin which is present in both the CSS classes but in some scenarios I see the margin of "MuiTypography-h1" overriding the custom css. How do I prevent this?一个例子是 CSS 类中都存在的边距,但在某些情况下,我看到“MuiTypography-h1”的边距覆盖了自定义 css。我该如何防止这种情况发生?

My general idea is custom CSS properties should always take precedence over MUI default CSS class properties.我的总体想法是自定义 CSS 属性应始终优先于 MUI 默认 CSS class 属性。 How can I make this happen?我怎样才能做到这一点? 在此处输入图像描述

 <Typography variant="h1" sx={{ width: '235px', height: '96px', fontSize: '20px', fontWeight: 500, lineHeight: '1.2', color: 'primary', textOverflow: 'ellipses', overflow: 'hidden', display: '-webkit-box', WebkitLineClamp: 4, WebkitBoxOrient: 'vertical', marginTop: '11px', }} > Title </Typography>

Straight forward way to do: you can directly override the MUI component with your custom CSS properties using the class name in your CSS file, for example in if you want to change the Button component's style, you can do this by applying your required CSS properties to "css-element-class-name" class on your "CSS" file as follows直接的方法:您可以使用 CSS 文件中的 class 名称使用您的自定义 CSS 属性直接覆盖 MUI 组件,例如,如果您想更改 Button 组件的样式,您可以通过应用所需的 CSS 属性来实现到“CSS”文件中的“css-element-class-name”class,如下所示

.css-elemet-class-name{
   color: yellow;
   height: 25px;
   padding: 15px;

} }

I've found that, MUI theme should be created in order to override MUI--root properties with your css styles, so try somethins like this:我发现,应该创建 MUI 主题以便使用您的 css styles 覆盖 MUI--root 属性,所以尝试这样的事情:

add your custome styles inside overrides:{}在覆盖中添加您的客户 styles:{}

const theme = createTheme({
  overrides: {
    MuiTypography: {
      h1: {
        '&.MuiTypography-gutterBottom': {
          marginBottom: 7,
        },
      },
      h2: {
        marginBottom: props => (props.gutterBottom ? 20 : null),
      },
    },
  },
});

and for the imports和进口

import createtheme from '@material-ui/core/styles'

if you are using this version:如果您使用的是这个版本:

"@material-ui/styles": "^4.11.2",

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

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