简体   繁体   English

如何在 materialUI 中设置 body 元素的样式

[英]How to style body element in materialUI

I can't find any way to target root elements with the ThemeProvider from Material-UI.我找不到任何方法来使用 Material-UI 的ThemeProvider定位根元素。

const theme = createMuiTheme({
  palette: {
    background: {
      default: "#00000",
      backgroundColor : 'black',
      backgroundImage: 'url(/bg.jpg)',
      backgroundPosition: 'center',
      height:'100%'
    },
    primary: {
      light: '#757ce8',
      main: '#fff',
      dark: '#002884',
      contrastText: 'grey',
    },
  },
});

You can apply the styles to the body using@global class like this:您可以使用@global class将样式应用于body如下所示:

const useGlobalStyles = makeStyles({
  "@global": {
    body: {
      backgroundColor: "tomato"
    }
  }
});
const theme = createMuiTheme({});

function MyThemeProvider({ children }) {
  useGlobalStyles();
  return <ThemeProvider theme={theme}>{children}</ThemeProvider>;
}

function App() {
  return (
    <MyThemeProvider>
      <Button variant="contained" color="primary">
        Button
      </Button>
    </MyThemeProvider>
  );
}

If you create the project by create-react-app , you can also use css/scss module to style any element globally:如果您通过create-react-app创建项目,您还可以使用 css/scss 模块来全局设置任何元素的样式:

/* styles.css */
body {
  color: white;
  font-size: 15px;
}
import React from "react";
import Button from "@material-ui/core/Button";
import "./styles.css";

function App() {
  return (
    <Button variant="contained" color="primary">
      Hello World
    </Button>
  );
}

Live Demo现场演示

编辑 64705335/how-to-style-body-element-in-materialui

Another approach outlined in the Docs > Themes > Globals section: 文档 > 主题 > 全局部分中概述的另一种方法:

If you are using the CssBaseline component to apply global resets, it can also be used to apply global styles.如果您使用CssBaseline组件来应用全局重置,它也可用于应用全局样式。

Here's an example:下面是一个例子:

import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";
import CssBaseline from "@material-ui/core/CssBaseline";

const theme = createMuiTheme({
  overrides: {
    MuiCssBaseline: {
      "@global": {
        body: {
          backgroundColor: "tomato"
        }
      }
    }
  }
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <div className="App">
        <h1>Hello CodeSandbox</h1>
      </div>
    </ThemeProvider>
  );
}

Demo in CodeSandBox and Stack Snippets CodeSandBox和 Stack Snippets 中的演示

 const { ThemeProvider, createMuiTheme, CssBaseline } = MaterialUI const theme = createMuiTheme({ overrides: { MuiCssBaseline: { "@global": { body: { backgroundColor: "tomato", }, }, }, }, }) const App = () => { return ( <ThemeProvider theme={theme}> <CssBaseline /> <div className="App"> <h1>Hello CodeSandbox</h1> </div> </ThemeProvider> ) } ReactDOM.render(<App />, document.getElementById("root"))
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.1/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/@material-ui/core@latest/umd/material-ui.production.min.js"></script> <div id="root"></div>

Further Reading : Global Styles with React and Material UI进一步阅读带有 React 和 Material UI 的全局样式

In Material-UI v5 there are two ways to do this:Material-UI v5 中,有两种方法可以做到这一点:

With the GlobalStyles component:使用GlobalStyles组件:

import * as React from 'react';
import GlobalStyles from '@material-ui/core/GlobalStyles';

export default function App() {
  return (
    <React.Fragment>
      <GlobalStyles styles={{ h1: { color: 'grey' } }} />
      <h1>Grey h1 element</h1>
    </React.Fragment>
  );
}

Edit on CodeSandboxCodeSandbox 上编辑


Or if you are already using the CssBaseline component for setting baseline styles:或者,如果您已经在使用CssBaseline组件来设置基线样式:

import { ThemeProvider, createTheme } from '@material-ui/core/styles';

const theme = createTheme({
  components: {
    MuiCssBaseline: {
      styleOverrides: `
        h1 {
          color: grey;
        }
      `,
    },
  },
});

export default function App() {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <h1>Grey h1 element</h1>
    </ThemeProvider>
  );
}

Edit on CodeSandboxCodeSandbox 上编辑


From the official docs: Global CSS Override来自官方文档: 全局 CSS 覆盖

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

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