最好的做法是将样式与组件或元素分开,除非您只想更改一两件事。
尝试使用 useStyles。它会让你的生活更轻松。
为了更好地理解这里发生了什么,我在下面重新创建了您的网格。
import { Grid} from "@material-ui/core";
import React from "react";
const YourComponent = () => {
const classes = useStyles();
return (
<>
<Grid className={classes.myGrid}>
Your Content Goes Here
</Grid>
</>
);
};
export default YourComponent;
Material UI 已实现支持多种屏幕尺寸的断点。假设您为您的样式创建了另一个文件,如下所示:
import { makeStyles } from "@material-ui/core/styles";
export default makeStyles((theme) => ({
toolbar: theme.mixins.toolbar,
myGrid: {
// here goes your margin for large screens and up
[theme.breakpoints.up("lg")]: {
margin: "-50px",
},
//here goes your regular size
}
}));
不过,您应该谨慎使用负边距。我相信有更好的方法来重新创建您的视图。
你一定要看看MUI's documentation on breakpoints。