【发布时间】:2020-07-06 14:12:52
【问题描述】:
我在尝试使用 MaterialUI 中的 Grid 组件实现一些简单的事情时遇到了困难。具体来说,我想在一个布局行上将一项左对齐,另一项右对齐。
我进行了广泛的搜索,但没有找到任何可行的解决方案。我尝试了很多建议,包括在Grid 组件和JSS 中使用justifyContent 和alignContent,以及“推送”内容的flex: 1 技术。
相关代码片段
尝试将<Typography> 元素放在左侧,将<FormGroup> 放在右侧:
<Container>
<Grid container spacing={3}>
<Grid className={classes.rowLayout}>
// Goal is to align this to the LEFT
<Grid item xs={6}>
<Typography variant="h6" gutterBottom>Some Text</Typography>
</Grid>
// Goal is to align this to the RIGHT
<Grid item xs={3}>
<FormGroup>
// Simple `Switch` button goes here
</FormGroup>
</Grid>
</Grid>
</Grid>
</Container>
MaterialUI JSS 样式:
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
width: '100%'
},
rowLayout: {
display: 'flex',
alignItems: 'baseline'
},
}));
我还发现,一般来说,这需要使用许多 Grid 组件,如果可能的话,我希望编写更简洁的代码。
您对此问题有任何提示或解决方法吗?
谢谢一百万,
戴维斯
【问题讨论】:
标签: javascript reactjs material-ui jss