[英]Style Mui child component within parent component using plain CSS
I am following these docs in order to style a material ui component (Paper) within a component (Menu) I am using.我正在关注这些文档,以便在我正在使用的组件(菜单)中设置材质 ui 组件(Paper)的样式。
I have :我有 :
// menu.js // 菜单.js
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import styles from './styles.css'
const MyMenu = (props) => {
...
return (
<div id="my-menu">
<Button id="button-react-component" onClick={handleClick}>
My Menu
</Button>
<Menu
id="menu-react-component"
...
className={styles.menu}
>
<MenuItem ...>
<span> Example 1 <span>
</MenuItem>
</Menu>
</div>
);
}
// styles.css // 样式.css
.menu {
color: white;
}
.menu .MuiPaper-root {
background-color: red
}
My goal is to have the MuiPaper
component have a given background-color.我的目标是让
MuiPaper
组件具有给定的背景颜色。 MuiPaper
is a component that comes from the Menu
component, but I am not using MuiPaper
directly as I am only declaring the parent ( <Menu>
). MuiPaper
是一个来自Menu
组件的组件,但我没有直接使用MuiPaper
,因为我只是声明了父级 ( <Menu>
)。
Ideally I want to use .css
files for styling.理想情况下,我想使用
.css
文件进行样式设置。
Here's what I see in my browser :这是我在浏览器中看到的内容:
Notice how the background-color "red" is not applied on that last screenshot.请注意背景颜色“红色”如何未应用于最后一个屏幕截图。
Thanks :)谢谢 :)
Your issue is in this part of your code:您的问题在代码的这一部分:
<Menu
id="menu-react-component"
...
className={styles.menu}
>
You should not be using className={styles.menu}
, but rather className="menu"
.您不应该使用
className={styles.menu}
,而应该使用className="menu"
。 This is reflected in the docs near the top in the "Plain CSS" section.这反映在“Plain CSS”部分顶部附近的文档中。
The {styles.menu}
type syntax is only used in the CSS Modules implementation, in this section of the docs: https://mui.com/guides/interoperability/#heading-css-modules because webpack is handling the CSS injection. {styles.menu}
类型语法仅用于 CSS Modules 实现,在文档的这一部分: https ://mui.com/guides/interoperability/#heading-css-modules 因为 webpack 正在处理 CSS 注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.