简体   繁体   中英

Make two grid items for Material-UI both same height with Flex-box

I have two grid items that I want to make the same height by trying to use flex, however nothing seems to work. I believe I might now be using the Grid element in Material UI correctly. I have tried using the height and width properties as well.

  <Paper elevation={10} style={{padding:'1.5em'}}>

      <Grid container direction='row' spacing={2} style={{display:'flex'}}>

          <Grid item style={{height:'100%', width:'50%'}}>
          </Grid>

          <Grid item style={{height:'100%', width:'50%'}}>
          </Grid>

      </Grid>

  </Paper>

If you are using flex on the parent element, you can try setting align-items: stretch on the parent too. This should stretch all children of that element to be the same height. Eg

.container {
  display: flex;
  align-items: stretch;
}

In material ui you can use alignItems="stretch" on the container Grid as you can see in the below code:

<Paper elevation={10} style={{padding:'1.5em'}}>
    <Grid container direction='row' spacing={2} alignItems="stretch">

        <Grid item style={{height:'100%', width:'50%'}}>
        </Grid>

        <Grid item style={{height:'100%', width:'50%'}}>
        </Grid>

    </Grid>
</Paper>

And you didn't need to set style={{display:'flex'}} because of grid style set it too.

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM