简体   繁体   English

material-ui 按钮颜色不会通过 css 样式更改

[英]material-ui Button color won't change through css styling

i just learned the basics of react and react-ui and when i tried to change the color of a material-ui Button, it doesn't work unless i use inline styling:我刚刚学习了 react 和 react-ui 的基础知识,当我尝试更改 material-ui 按钮的颜色时,除非我使用内联样式,否则它不起作用:

the button component:按钮组件:

import React from "react";
import Button from "@material-ui/core/Button";

function MyButton() {
  return (
    <Button
      // style={{ backgroundColor: "black" }}
      variant="contained"
      color="primary"
      className="signUp"
    >
      Sign Up
    </Button>
  );
}
export default MyButton;

css styling: css 造型:

.signUp{
    background-color : black;
    color : yellow;
    filter: drop-shadow(0px 8px 8px white);
    float:right;
    } 

inline style:内联样式:

style={{ backgroundColor: "black" }}

so what do you think the problem is?那么你认为问题是什么?

You need to import your CSS stylesheet in your MyButton JSX file.您需要在MyButton JSX 文件中导入 CSS 样式表。

For example, if you store your button CSS styling in a file called styles.css and this file is in the same directory of your MyButton JSX file, add the following line:例如,如果您将按钮 CSS 样式存储在名为styles.css的文件中,并且此文件位于MyButton JSX 文件的同一目录中,请添加以下行:

import "./styles.css";

So the entire code becomes:所以整个代码变成:

import React from "react";
import Button from "@material-ui/core/Button";
import "./styles.css"; // Add this line!

function MyButton() {
  return (
    <Button variant="contained" color="primary" className="signUp">
      Sign Up
    </Button>
  );
}
export default MyButton;

在 CodeSandbox 上编辑

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

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