[英]Position components using Material-UI Grid
I have a codesandbox ( https://codesandbox.io/s/determined-pascal-ypmwk?file=/src/App.js ) that shows how my current app is set up with Material-UI grid.我有一个代码沙盒( https://codesandbox.io/s/determined-pascal-ypmwk?file=/src/App.js ),它显示了我当前的应用程序是如何使用 Material-UI 网格设置的。 I have 5 components that are being positioned and this is how I want them to look on the
lg
setting and above.我有 5 个正在定位的组件,这就是我希望它们在
lg
设置及更高版本上的外观。
Then on the md
and below setting to look like this然后在
md
和以下设置看起来像这样
I have tried a few different ways and I think the closest I have come is what is currently in my codesandbox.我尝试了几种不同的方法,我认为最接近的是我目前在我的代码和盒子中的东西。 I cannot quite figure out how to put component
5
on the same row for the bigger screens and on the next row for the smaller screen.我无法弄清楚如何将组件
5
放在大屏幕的同一行和小屏幕的下一行。
Thanks in advance for any help!在此先感谢您的帮助!
You can make use of Material UI breakpoints hook as per the documentation material-ui-breakpoints您可以根据文档material-ui-breakpoints使用 Material UI 断点挂钩
I created a demo app for you.我为您创建了一个演示应用程序。 Do have a look in case it fits your use case link-to-demo
看看它是否适合您的用例链接到演示
Here's some sample code for the same这是相同的一些示例代码
import React from "react";
import withWidth, { isWidthDown, isWidthUp } from "@material-ui/core/withWidth";
import { Grid } from "@material-ui/core";
import { makeStyles } from "@material-ui/core/styles";
const useStyles = makeStyles((theme) => ({
item1: {
height: "200px",
border: "1px solid black"
},
item2: {
height: "400px",
border: "1px solid black"
},
item3: {
height: "200px",
border: "1px solid black"
},
item4: {
height: "200px",
border: "1px solid black"
},
item5: {
height: "200px",
border: "1px solid black"
}
}));
const App = (props) => {
const classes = useStyles();
if (isWidthUp("lg", props.width)) {
return layout1(classes);
} else if (isWidthDown("md", props.width)) {
return layout2(classes);
}
return <React.Fragment />;
};
const layout1 = (classes) => {
return (
<Grid container>
<Grid item lg={12} className={classes.item1}>
1
</Grid>
<Grid item lg={4} className={classes.item2}>
2
</Grid>
<Grid item lg={8}>
<Grid container>
<Grid item lg={6} className={classes.item3}>
3
</Grid>
<Grid item lg={6} className={classes.item4}>
4
</Grid>
<Grid item lg={12} className={classes.item5}>
5
</Grid>
</Grid>
</Grid>
</Grid>
);
};
const layout2 = (classes) => {
return (
<Grid container>
<Grid item xs={12} className={classes.item1}>
1
</Grid>
<Grid item xs={9} className={classes.item2}>
2
</Grid>
<Grid item xs={3}>
<Grid container>
<Grid item xs={12} className={classes.item3}>
3
</Grid>
<Grid item xs={12} className={classes.item4}>
4
</Grid>
</Grid>
</Grid>
<Grid item xs={12} className={classes.item5}>
5
</Grid>
</Grid>
);
};
export default withWidth()(App);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.