Ego*_*rov 3 css overlay reactjs material-ui
我在我的应用程序中使用MaterialUI Card和CardMedia组件,但无法弄清楚如何在图像上方叠加文本。这是我正在尝试的简化示例:
<Card>
<CardMedia image={this.props.preview} style={styles.media}/>
<div style={styles.overlay}>
this text should overlay the image
</div>
</Card>
const styles = {
media: {
height: 0,
paddingTop: '56.25%' // 16:9
},
overlay: {
position: 'relative',
top: '20px',
left: '20px',
color: 'black',
backgroundColor: 'white'
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试将文本div放置在CardMedia之上,之下,内部,整个Card外部,并使用不同的位置值,但根本无法弄清楚。Beta版的MaterialUI在CardMedia上包含了overlay属性,但是v1库似乎没有这样的东西。
任何人都知道如何正确执行此操作吗?在此先感谢您的帮助!
Nea*_*arl 13
Card 如果您想拥有像版本 0 中的覆盖层,请使用下面的代码。请记住将容器的位置设置为,relative以便absolute覆盖的位置可以生效:
<Card sx={{ maxWidth: 345 }}>
<Box sx={{ position: 'relative' }}>
<CardMedia
component="img"
height="200"
image="https://mui.com/static/images/cards/contemplative-reptile.jpg"
/>
<Box
sx={{
position: 'absolute',
bottom: 0,
left: 0,
width: '100%',
bgcolor: 'rgba(0, 0, 0, 0.54)',
color: 'white',
padding: '10px',
}}
>
<Typography variant="h5">Lizard</Typography>
<Typography variant="body2">Subtitle</Typography>
</Box>
</Box>
{...}
</Card>
Run Code Online (Sandbox Code Playgroud)

CSS已关闭,您将需要绝对定位styles.overlay,并确保Cardis处于位置relative
尝试这样的事情:
<Card style={styles.card}>
<CardMedia image={this.props.preview} style={styles.media}/>
<div style={styles.overlay}>
this text should overlay the image
</div>
</Card>
const styles = {
media: {
height: 0,
paddingTop: '56.25%' // 16:9
},
card: {
position: 'relative',
},
overlay: {
position: 'absolute',
top: '20px',
left: '20px',
color: 'black',
backgroundColor: 'white'
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3525 次 |
| 最近记录: |