【问题标题】:reactjs: material ui: how to align two items to the two endsreactjs:材料ui:如何将两个项目对齐到两端
【发布时间】:2021-06-22 20:14:33
【问题描述】:

我在 reactjs 中使用 Material ui 主题有以下代码

const useStyles = makeStyles((theme) => ({
  root2: {
    maxWidth: 345,
    flexGrow: 1
  },
  paper2: {
    padding: theme.spacing(1),
    color: theme.palette.text.secondary,
  }
})

<div className={classes.root2}>
  <Grid container spacing={3}>
    <Grid item xs={12}>
      <Paper className={classes.paper2}>
       <Grid container alignItems="center" spacing={3}>
          <Grid item>
            <h3>Instructions</h3>
          </Grid>
          <Grid item>
            <IconButton>
              <ExpandMoreIcon />
            </IconButton>
          </Grid>
        </Grid>
      </Paper>
    </Grid>
  </Grid>
</div>

我想要的是

我在https://material-ui.com/system/flexbox/#flex-grow看到了一个例子

使用代码:

import React from 'react';
import Box from '@material-ui/core/Box';

export default function FlexGrow() {
  return (
    <div style={{ width: '100%' }}>
      <Box display="flex" p={1} bgcolor="background.paper">
        <Box p={1} flexGrow={1} bgcolor="grey.300">
          Item 1
        </Box>
        <Box p={1} bgcolor="grey.300">
          Item 2
        </Box>
        <Box p={1} bgcolor="grey.300">
          Item 3
        </Box>
      </Box>
    </div>
  );
}

我是材料 ui 的新手。这里的示例使用 Box 而不是 Grid。

那么处理这个问题的最佳方法是什么

我习惯于 bootstrap 4:在 bootstrap4 中我可以使用

https://getbootstrap.com/docs/4.4/utilities/flex/#justify-content

<div class="d-flex justify-content-between">...</div>

如何在 Material UI 中做这样的事情

【问题讨论】:

  • 在父容器上使用flexbox并添加属性为display: flex;justify-content: space-between

标签: reactjs material-ui


【解决方案1】:

justify="space-between" 添加到Grid 容器组件将解决您的问题。

请参考this

  <Grid container spacing={3}>
    <Grid item xs={12}>
      <Paper className={classes.paper2}>
       <Grid container alignItems="center" justify="space-between">
          <Grid item>
            <h3>Instructions</h3>
          </Grid>
          <Grid item>
            <IconButton>
              <ExpandMoreIcon />
            </IconButton>
          </Grid>
        </Grid>
      </Paper>
    </Grid>
  </Grid>

【讨论】:

猜你喜欢
  • 2019-10-29
  • 2021-11-27
  • 2023-03-27
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多