简体   繁体   中英

Grid item height not matching responsive square child

I'm creating a web application with React and Material-ui. I need a responsive square div for a map component, however, using a trick from iamsteve.me breaks the grid layout as the parent Grid div does not expand.

I have tried moving the class which is square to the grid element, but this doesn't help.

map: {
    height: '100%', width: '100%', position: 'absolute'
},
mapContainer: {
    height: '0',
    paddingBottom: '100%',
    position: 'relative'
},
<Grid item>
  <div className={classes.mapContainer}>
    <Paper className={classes.map}>
    // map
    </Paper>
  </div>
</Grid>

截图

The height of the Grid item does not match the size of the square. This is an issue for items that follow.

For responsive square, you should use Grid provided by Material-UI .

A simple change in your code to use Grid could be this

<Grid container justify={"center"}>
   <Grid item xs={6}>
      <Paper className={classes.paper}>Your Map Here</Paper>
    </Grid>
</Grid>

To constrain the width of the containing element, you can do

root: {
    flexGrow: 1,
    height: "50vh",
    width: "50vh"
  },

I have updated a Codesandbox demonstrating it at https://codesandbox.io/s/material-demo-pjssu

I hope this helps. Thanks

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