【发布时间】:2021-03-15 13:46:09
【问题描述】:
我在 Material UI 中创建了一张包含文本的卡片。我想修复卡片的高度,如果超过 3 行,则将其中的文本截断。最好的方法是什么?
下面是我的代码,如您所见,我尝试使用 CSS 来执行此操作,但是 (a) 省略号不显示 (b) 溢出会在水平方向切掉文本,所以我会喜欢有办法通过线高的倍数而不是像素来做到这一点。如果显示器尺寸调整,我也希望它仍然可以工作。
import React from 'react';
// Material UI
import {
Card,
CardActionArea,
CardContent,
CardMedia,
makeStyles,
Typography,
} from '@material-ui/core';
const useStyles = makeStyles(theme => ({
media: {
height: 140,
},
title: {
height: 50,
overflow: 'hidden',
textOverflow: 'ellipsis',
},
description: {
height: 50,
overflow: 'hidden',
textOverflow: 'ellipsis',
},
}));
const ContentThumbnail = ({ image, title, description, date }) => {
const classes = useStyles();
return (
<Card>
<CardActionArea>
<CardMedia
image={image}
title=""
className={classes.media}
/>
<CardContent>
<Typography gutterBottom variant="h5" component="h2" className={classes.title}>
{title}
</Typography>
<Typography variant="body2" component="p" className={classes.description}>
{description}
</Typography>
</CardContent>
</CardActionArea>
</Card>
);
};
export default ContentThumbnail;
【问题讨论】:
标签: javascript css reactjs material-ui