【问题标题】:React - Material UI Items inside the grid refuse to centerReact - 网格内的材质 UI 项目拒绝居中
【发布时间】:2021-10-19 00:29:39
【问题描述】:

我想将卡片放在网格项目的中心,但它不起作用。 正如我从文档 (https://material-ui.com/components/grid/) 中了解到的那样,我应该使用 justifyContent="center" 但这不起作用,并且在堆栈溢出 (How to center a component in Material-UI and make it responsive?) 的答案之一中,有人使用 justify="center" 代替,所以我也试过了,但是没用。

<Grid container justifyContent="center">
  <Grid item xs={12} md={4} >
    <Card style={{maxWidth: 345}}>
      <CardActionArea>
        <CardMedia
          style={{height: 140}}
          image="https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cHJvZHVjdHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80"
          title="Contemplative Reptile"
          />
        <CardContent>
          <Typography variant="h5" component="h2">
            Lizard
          </Typography>
          <Typography gutterBottom variant="body2" color="textSecondary" component="p">
            Lizards are a widespread group of squamate reptiles, with over 6,000 species, ranging
            across all continents except Antarctica
          </Typography>
          <Typography gutterBottom varient="h5" color="Secondary">
            100
          </Typography>
        </CardContent>
      </CardActionArea>
    </Card>
  </Grid>
  <Grid item xs={12} md={4}>
    <Card style={{maxWidth: 345}}>
      <CardActionArea>
        <CardMedia
          style={{height: 140}}
          image="https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cHJvZHVjdHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80"
          title="Contemplative Reptile"
          />
        <CardContent>
          <Typography variant="h5" component="h2">
            Lizard
          </Typography>
          <Typography gutterBottom variant="body2" color="textSecondary" component="p">
            Lizards are a widespread group of squamate reptiles, with over 6,000 species, ranging
            across all continents except Antarctica
          </Typography>
          <Typography gutterBottom varient="h5" color="Secondary">
            100
          </Typography>
        </CardContent>
      </CardActionArea>
    </Card>
  </Grid>
  <Grid item xs={12} md={4}>
    <Card style={{maxWidth: 345}}>
      <CardActionArea>
        <CardMedia
          style={{height: 140}}
          image="https://images.unsplash.com/photo-1505740420928-5e560c06d30e?ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8cHJvZHVjdHxlbnwwfHwwfHw%3D&ixlib=rb-1.2.1&w=1000&q=80"
          title="Contemplative Reptile"
          />
        <CardContent>
          <Typography variant="h5" component="h2">
            Lizard
          </Typography>
          <Typography gutterBottom variant="body2" color="textSecondary" component="p">
            Lizards are a widespread group of squamate reptiles, with over 6,000 species, ranging
            across all continents except Antarctica
          </Typography>
          <Typography gutterBottom varient="h5" color="Secondary">
            100
          </Typography>
        </CardContent>
      </CardActionArea>
    </Card>
  </Grid>
</Grid>

【问题讨论】:

    标签: reactjs material-ui grid containers card


    【解决方案1】:

    您可以删除 xsmd 并将 justifyContent 更改为 space-around 以在它们之间提供适当的间距。看看代码沙箱。 Here

    【讨论】:

    • 我明白了,但这仍然不能解释为什么“justifyContent”不起作用,你知道为什么吗?
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2017-03-24
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多