How to horizontally center an item in Material UI grid item?

心不动则不痛 提交于 2021-02-07 11:23:06

问题


How do I center elements inside a Material UI Grid item? Here is a snippet from my React application:

<Grid container>
  <Grid item xs={4}>
    // Unrelated stuff here
  </Grid>
  <Grid item xs={4}>
    // How do I centre these items?
    <IconButton className={classes.menuButton} color="inherit">
      <EditIcon/>
    </IconButton>
    <IconButton className={classes.menuButton} color="inherit">
      <CheckBoxIcon/>
    </IconButton>
  </Grid>
  <Grid item xs={4}>
    // Unrelated stuff here
  </Grid>
</Grid>

I've tried applying alignContent, justify, alignItems (to the parent <Grid item>) with no success.

I thought this would be quite simple, but I've failed to find any information on centring items inside of grid items.


回答1:


Two seconds later... I solved this through some simple CSS:

<Grid item xs={4} style={{textAlign: "center"}}>
</Grid>

If there is another approach that is somehow more "correct", please feel free to post another answer.




回答2:


 <Grid container  className = {classes.root} align = "center" justify = "center" alignItems = "center"  >
            <CssBaseline/>
            <Grid item xs = {false} sm = {4} md = {6}>

            </Grid>
            <Grid item xs = {12} sm = {4} md = {6}>

            </Grid>
        </Grid>`enter code here`



回答3:


This is the correct way:

<Grid container item xs={4} justify="center">

if container property is set to true the component will have the flex container behavior.

Grid API



来源:https://stackoverflow.com/questions/52357717/how-to-horizontally-center-an-item-in-material-ui-grid-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!