如何在 MUI 中设置 body 元素的样式
How to style body element in MUI
我找不到任何方法来使用 MUI 中的 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',
},
},
});
Material-UI v5
您可以通过覆盖 createTheme()
中的 MuiCssBaseline
样式来更改正文样式:
import CssBaseline from "@mui/material/CssBaseline";
import darkScrollbar from "@mui/material/darkScrollbar";
import { createTheme, ThemeProvider } from "@mui/material/styles";
const theme = createTheme({
components: {
MuiCssBaseline: {
styleOverrides: {
body: {
...darkScrollbar(),
color: "darkred",
backgroundColor: "grey",
"& h1": {
color: "black"
}
}
}
}
}
});
export default function GlobalCssOverride() {
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Content />
</ThemeProvider>
);
}
Material-UI v4
您可以使用 @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>
);
}
如果您通过 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>
);
}
现场演示
Docs > Themes > Globals 部分概述的另一种方法:
If you are using the CssBaseline
component to apply global resets, it can also be used to apply global styles.
这是一个例子:
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 和堆栈片段
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>
MUI 5 更新
像这样创建您的主题:
import { createTheme } from "@mui/material";
export const theme = createTheme({
components: {
MuiCssBaseline: {
styleOverrides: {
body: {
backgroundColor: "tomato"
}
}
}
}
});
进一步阅读:Global Styles with React and Material UI
在Material-UI v5中有两种方法:
使用 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>
);
}
编辑 CodeSandbox
或者,如果您已经在使用 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>
);
}
编辑 CodeSandbox
来自官方文档:Global CSS Override
我找不到任何方法来使用 MUI 中的 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',
},
},
});
Material-UI v5
您可以通过覆盖 createTheme()
中的 MuiCssBaseline
样式来更改正文样式:
import CssBaseline from "@mui/material/CssBaseline";
import darkScrollbar from "@mui/material/darkScrollbar";
import { createTheme, ThemeProvider } from "@mui/material/styles";
const theme = createTheme({
components: {
MuiCssBaseline: {
styleOverrides: {
body: {
...darkScrollbar(),
color: "darkred",
backgroundColor: "grey",
"& h1": {
color: "black"
}
}
}
}
}
});
export default function GlobalCssOverride() {
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Content />
</ThemeProvider>
);
}
Material-UI v4
您可以使用 @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>
);
}
如果您通过 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>
);
}
现场演示
Docs > Themes > Globals 部分概述的另一种方法:
If you are using the
CssBaseline
component to apply global resets, it can also be used to apply global styles.
这是一个例子:
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 和堆栈片段
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>
MUI 5 更新
像这样创建您的主题:
import { createTheme } from "@mui/material";
export const theme = createTheme({
components: {
MuiCssBaseline: {
styleOverrides: {
body: {
backgroundColor: "tomato"
}
}
}
}
});
进一步阅读:Global Styles with React and Material UI
在Material-UI v5中有两种方法:
使用 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>
);
}
编辑 CodeSandbox
或者,如果您已经在使用 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>
);
}
编辑 CodeSandbox
来自官方文档:Global CSS Override