简体   繁体   English

使用纯 CSS 为父组件中的 Mui 子组件设置样式

[英]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.

相关问题 如何将样式 css 从父组件传递给子组件 vue - how to pass style css from parent component to child component vue 使用 styled-components 从父组件设置子组件的样式 - Style child component from parent component using styled-components MUI-Datatable,子组件检测父组件的 state 的变化 - MUI-Datatable, child component detecting change in the state of the parent component 在父组件内有条件地渲染子组件 - Conditionally Render Child Component within Parent Component 是否无法在 MUI 的“styled()”function 中设置嵌套组件的样式? - Is it not possible to style a nested component within MUI's 'styled()' function? 子组件使用 onChange(),更新父组件 - child component using onChange(), update parent component 如何覆盖 MUI 父组件 css 以显示:反应中按钮的“块” - How to override MUI parent component css to display: 'block' for buttons in react 在样式化组件中的子组件的输入焦点上设置父组件 - Style parent component on input focus of child component in styled-components 如何在不影响 React Native 中的子组件的情况下设置父组件的样式 - How to style parent component without affecting child component in React Native 在父组件内的子组件上触发onPress事件 - Trigger onPress event on Child component within Parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM