【发布时间】:2021-02-04 04:58:53
【问题描述】:
我想从 material-ui 实现网格系统,但遇到了一个问题。调整浏览器窗口的大小对我的网格项目没有影响——它们不会调整大小。我发现这个问题是由具有样式属性“display”:“flex”的父div引起的。
我正在使用 material-ui 应用栏和抽屉。此外,我为所有内容和子项构建了一个可重用的容器组件。如果没有样式属性“display”:“flex”,我的整个页面样式都会被破坏。那么我该如何解决呢?有什么想法吗?
这是我的代码和具有属性“display”的类根:“flex”
Container.tsx
return (
<div className={classes.root}>
<TopBar /> // Material-UI AppBar
<Drawer /> // Reuseable Drawer Component
<main className={classes.contentContainer}>
<div>{children}</div>
</main>
</div>
);
容器样式
const useStyles = makeStyles((theme: Theme) =>
createStyles({
contentContainer: {
flexGrow: 1,
marginTop: 63,
position: 'relative',
backgroundColor: colors.whiteB_8,
height: `calc(100vh - 63px)`,
// padding: theme.spacing(3),
'& > div:first-child': {
padding: `calc(${theme.spacing(1)}px + 35px)`,
},
},
root: {
display: 'flex',
},
}),
);
然后我可以将任何组件传递给这个容器。这是我的示例组件,我想在内容容器中包含 3 列:
return (
<Grid container item xs={12} spacing={2}>
<Grid item xs={4}>
<Paper
style={{ backgroundColor: 'grey' }}
>
Test
</Paper>
</Grid>
<Grid item xs={4}>
<Paper
style={{ backgroundColor: 'grey' }}
>
Test
</Paper>
</Grid>
<Grid item xs={4}>
<Paper
style={{ backgroundColor: 'grey' }}
>
Test
</Paper>
</Grid>
</Grid>
);
输出如下:
并且项目没有调整大小:
如果我删除 "display": "flex" 它看起来像这样 - 内容在抽屉下面:
【问题讨论】:
-
您能否使用
codesandbox或类似的东西创建一个可重现的最小示例,以便读者查看和调试您的问题。
标签: html css reactjs material-ui