使用 material-ui 的 API,如何在 body 中设置背景图片?

Ram*_*aro 6 reactjs material-ui

我想使用 material-ui-next 的 CSS-in-JS、withStyles 或主题在我的 Web 应用程序正文中设置背景图像?如果可能,我想避免使用外部 CSS 文件。

Ram*_*aro 8

您可以使用 "@material-ui/core/styles" 中的 withStyles 将 css 注入到带有 "@global" 的外部作用域中

import React, { Fragment } from "react";
import CssBaseline from "@material-ui/core/CssBaseline";
import { withStyles } from "@material-ui/core/styles";
import Main from "./Main";

const styles = theme => ({
	"@global": {
		body: {
			backgroundImage: "url('/media/landing.jpg')",
			backgroundRepeat: "no-repeat",
			backgroundPosition: "center center",
			backgroundSize: "cover",
			backgroundAttachment: "fixed",
			height: "100%"
		},
		html: {
			height: "100%"
		},
		"#componentWithId": {
			height: "100%"
		}
	}
});

const App = () => {
	return (
		<Fragment>
			<CssBaseline />
			<Main />
		</Fragment>
	);
};

export default withStyles(styles)(App);
Run Code Online (Sandbox Code Playgroud)