最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - CardMedia images not fitting inside tag - Stack Overflow

programmeradmin2浏览0评论

I am building a demo of a website selling games. My images due to their heights and widths aren't fitting uniformly to CardMedia.

So in the first row they don't have any padding top and in the second row all but one do. Also they have different sizes.

I tried using objectFit but it didn't help.

My markup:

  return (
    <>
      <Card
        elevation={3}
        sx={{
          width: 280,
          borderRadius: 3,
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'space-evenly',
        }}
      >
        <CardMedia
          component='img'
          sx={{height: 280, backgroundSize: 'cover' , objectFit: 'contain', width: '100%'}}
          image={game.pictureUrl}
          title={game.name}
        />
        <CardContent>
           {/* other markup */}
        </CardContent>

        <CardActions>
          <Button>Details</Button>
        </CardActions>
      </Card>
    </>
  );

I am building a demo of a website selling games. My images due to their heights and widths aren't fitting uniformly to CardMedia.

So in the first row they don't have any padding top and in the second row all but one do. Also they have different sizes.

I tried using objectFit but it didn't help.

My markup:

  return (
    <>
      <Card
        elevation={3}
        sx={{
          width: 280,
          borderRadius: 3,
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'space-evenly',
        }}
      >
        <CardMedia
          component='img'
          sx={{height: 280, backgroundSize: 'cover' , objectFit: 'contain', width: '100%'}}
          image={game.pictureUrl}
          title={game.name}
        />
        <CardContent>
           {/* other markup */}
        </CardContent>

        <CardActions>
          <Button>Details</Button>
        </CardActions>
      </Card>
    </>
  );

Share Improve this question asked Mar 31 at 17:18 X HOxhaX HOxha 1771 gold badge3 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

change from

objectFit: 'contain'

to

objectFit: 'cover',

发布评论

评论列表(0)

  1. 暂无评论