material-ui-next: Setting image size to fit a container
Asked Answered
C

2

12

I've started out with Material-ui-next and have some problems with displaying images that they use the entire size of a container. E.g. I use:

const styles = theme => ({
  Card: {
    width: 300,
    margin: 'auto'
  },
  Media: {
    height: 550
  }
});

In render:

<Card className={classes.Card}>
   <CardMedia
        className={classes.Media}
        image={ImgPomodoR}
        title="a pomodoro tomatoe timer in material design"
   />
   <CardContent>
      <Typography gutterBottom variant="headline" component="h2">
        ...

The documentation says I have to specify a height for the image to get displayed. The 'media' example gives the image a height of 0, however, if I apply that my image is not getting displayed - mentioned example.

Right now, for me it's a trial and error of the Media-height, that it fits the Card container without being cropped. Is there no 'auto' way of doing this?

Any help is highly appreciated, cheers mates,

Tobias

Edit: I should mention that height: "100%" // maxHeight: "100%" does also not work for me.

Charged answered 9/5, 2018 at 11:37 Comment(0)
V
16

I was having the same issue. Setting both width and height to '100%' worked for me.

const styles = theme => ({
  Card: {
    width: 300,
    margin: 'auto'
  },
  Media: {
    height: '100%',
    width: '100%'
  }
});

However, if your images have different heights, this will cause cards to be different heights and most of the time that is not what you want. For that, you can specify a height and keep the width at '100%'.

const styles = theme => ({
  Card: {
    width: 300,
    margin: 'auto'
  },
  Media: {
    height: 550,
    width: '100%'
  }
});

This will stretch the images to fit the container. For my case, I wanted part of the image to be shown without stretching the images. To achieve this, simply set the objectFit property to cover. This worked nicely for me.

const styles = theme => ({
  Card: {
    width: 300,
    margin: 'auto'
  },
  Media: {
    height: 550,
    width: '100%',
    objectFit: 'cover'
  }
});

Hope this helps someone,

Ventilate answered 21/3, 2019 at 0:23 Comment(0)
P
0

I think it would work

const styles = theme => ({
  card:{
    backgroundColor: 'white',
    marginBottom: 40,
  },
  media: {
    height: 0,
    // paddingTop: '56.25%', // 16:9,
    paddingTop: '100%', // 1:1,
  },
});
Pushcart answered 7/6, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.