简体   繁体   English

如何覆盖 Material UI 组件的 css 属性

[英]How to override css properties of Material UI components

I am working on project in which I have to use Material UI, just for simplicity, consider I am having a simple functional component in which I am having 2 Material UI components Button and Text Field我正在处理我必须使用 Material UI 的项目,为了简单起见,考虑我有一个简单的功能组件,其中我有 2 个 Material UI 组件按钮和文本字段

import React from 'react';
import { Button, TextField } from '@material-ui/core';

function RenderComponent(){
    return(
        <>
            <Button variant="contained">Contained</Button>
            <TextField id="outlined-basic" label="Outlined" variant="outlined" />
        </>
    )
}

export default RenderComponent

Can anyone please tell me how to override the css properties of these 2 Material UI components.谁能告诉我如何覆盖这 2 个 Material UI 组件的 css 属性。

Thankyou谢谢

Give them id's/classes and write your custom CSS for them.给他们 id's/classes 并为他们编写您的自定义 CSS。 If it doesn't override the standard Material-UI styles, add the keyword !important .如果它没有覆盖标准 Material-UI styles,请添加关键字!important If you are using create-react-app, you can import CSS right to the file like this:如果您使用的是 create-react-app,则可以将 CSS 正确导入文件,如下所示:

import "./styles.css";

If this doesn't help, that means Material-UI uses inline styles for the elements.如果这没有帮助,这意味着 Material-UI 使用内联 styles 作为元素。 In this case, you would have to write your CSS right into your components' attributes: like this .在这种情况下,您必须将 CSS 直接写入组件的属性中:就像这样 If it doesn't override standard styles, use.important again.如果它没有覆盖标准 styles,请再次使用.important。

There are several ways to do this, you can directly override the built-in CSS class of an MUI component using the class name in your imported 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 .MuiButton-root class on your "CSS" file. There are several ways to do this, you can directly override the built-in CSS class of an MUI component using the class name in your imported CSS file, for example in if you want to change the Button component's style, you can do this by将所需的CSS属性应用于“CSS”文件上的.MuiButton-root class 。 Like bellow.像下面这样。

.MuiButton-root{
   color: white;
   height: 20px;
   padding: 10px;
}

You can also use the available props of a component, you can easily find them in their individual documentation page of mui site.您还可以使用组件的可用props ,您可以在 mui 站点的各个文档页面中轻松找到它们。 Again you can use makeStyles or styled to style your component.同样,您可以使用makeStylesstyled来设置组件的样式。 Here is an example to use makeStyles in your Button component.这是在Button组件中使用makeStyles的示例。

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Button, TextField } from '@material-ui/core';

const useStyles = makeStyles({

    root: {
          background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
          border: 0,
          borderRadius: 3,
          boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
          color: 'white',
          height: 48,
          padding: '0 30px',
        },
     });

function RenderComponent(){
    const classes = useStyles();
    return(
        <>
            <Button className={classes.root} variant="contained">Contained</Button>
            <TextField id="outlined-basic" label="Outlined" variant="outlined" />
        </>
    )
}

export default RenderComponent

You can find more about styles here.您可以在此处找到有关 styles 的更多信息。

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

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