简体   繁体   English

材料 ui cardmedia 高度作为纵横比还是使用 vh?

[英]material ui cardmedia height as aspect ratio or using vh?

const useStyles = makeStyles({
  media: {
    height: 183
  }
});

<CardMedia
  className={classes.media}
  image="pic.jpg"
/>

This let´s me set the size in px.这让我以 px 为单位设置大小。 Is there a way to set it to an aspect ratio?有没有办法将其设置为纵横比? For example on 300px wide card it´s 200px high and on 600px wide card its 400px high?例如,在 300 像素宽的卡片上,它的高度为 200 像素,而在 600 像素宽的卡片上,它的高度为 400 像素? This would be my preferred way.这将是我的首选方式。

As alternative, how could I use vh for the heigth?作为替代方案,我如何使用 vh 作为高度?

Looks like I found an acceptable solution for smaller screens at least.看起来我至少为较小的屏幕找到了可接受的解决方案。 Wish I could use '100vw'*1.778 instead of just 100. but now it scales okay-ish.希望我可以使用'100vw'*1.778而不是 100。但现在它可以扩展了。


    media: {
        width: '100vw', //this gives me full width
        height: props => 100 * props.ratio,
    },
});

export default function MediaCard() {
    const classes = useStyles({ratio: 1.778});

    return (
        <Card className={classes.root}>
            <CardActionArea>
                <CardMedia
                    className={classes.media}
                    image={michel}
                    title="Michel"
                />
            </CardActionArea>

        </Card>
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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