我在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',
},
},
});
答案 0 :(得分:1)
您可以使用@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>
);
}
答案 1 :(得分:1)
Docs > Themes > Globals 部分概述的另一种方法:
<块引用>如果您使用 CssBaseline
组件应用全局重置,它也可用于应用全局样式。
这是一个例子:
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>
);
}
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>