reactjs - 如何在反应材料ui CardMedia组件中包含图像
问题描述
我最接近正确的方法是玩弄宽度和高度:
const useStyles = makeStyles((theme) => ({
...
media: {
height: 100,
width: 100,
margin: 'auto',
},
...
}));
const Brands = (props) => {
...
return <div style={{ marginTop: props._marginTop }}>
<Grid container justify='center'>
<Grid item xs={10}>
<Grid container spacing={2}>
{brands.map((brand, i)=> {
return <Grid item key={i} lg={3} xs={12}>
<Card>
<CardMedia
className={classes.media}
image={brand.image.length > 0 ? brand.image : knightdemon}
title={brand.name}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2">
{brand.name.toUpperCase()}
</Typography>
<Typography
onClick={()=>setFlip(true)}
className={classes.description}
gutterBottom variant="body2"
component="p"
>
DESCRIPTION
</Typography>
</CardContent>
</Card>
</Grid>
})}
</Grid>
</Grid>
</Grid>
</div>
}
export default Brands;
高度看起来都不错,问题出在宽度上,如果我增加它,它也会影响高度。如何将它们包含在给定的空间中,使它们看起来像这样:
解决方案
要适合图像CardMedia
,请添加以下道具component="img"
:
<CardMedia
className={classes.media}
image={brand.image.length > 0 ? brand.image : knightdemon}
title={brand.name}
component="img"
/>
这应该可以解决您的问题。
推荐阅读
- unreal-engine4 - 如何在同一网络中的多台电脑上托管 UE4 射击游戏
- push-notification - FMC push notification android background
- reactjs - react-js-pagination如何用react hook数据表实现
- javascript - 推送后数组推送始终为空(Javascript)
- c# - JsonConvert.SerializeObject 移除对象名称
- python-3.x - 如何找到两个分类变量 num_chicken_pox 与疫苗接种次数之间的相关性
- dataframe - csv 的 pyspark 模式验证
- python - 在 Python 中执行任务的计时器 --CLOSED--
- prolog - Prolog递归代码返回false,添加变量作为递归列表解析的输入
- javascript - Measurement Protocol Google Analytics Post 请求未注册页面浏览量