如何使Map渲染的MUI卡片文本区域等高?附CodeSandbox示例
修复MUI Card文本区域等高问题
我在用map渲染MUI Card组件时遇到问题:部分卡片因文本内容较多导致整体高度增加,但卡片内的文本区域无法保持等高。需要修复示例代码,以便将相同逻辑应用到使用Grid布局的实际项目中。
解决方案核心思路
利用flex布局让卡片内部区域自动分配空间,确保文本区域占据剩余高度,实现等高效果。
修改后的示例代码
import * as React from "react"; import { styled } from "@mui/material/styles"; import Card from "@mui/material/Card"; import CardHeader from "@mui/material/CardHeader"; import CardMedia from "@mui/material/CardMedia"; import CardContent from "@mui/material/CardContent"; import CardActions from "@mui/material/CardActions"; import Collapse from "@mui/material/Collapse"; import Avatar from "@mui/material/Avatar"; import IconButton from "@mui/material/IconButton"; import Typography from "@mui/material/Typography"; import { red } from "@mui/material/colors"; import FavoriteIcon from "@mui/icons-material/Favorite"; import ShareIcon from "@mui/icons-material/Share"; import ExpandMoreIcon from "@mui/icons-material/ExpandMore"; import MoreVertIcon from "@mui/icons-material/MoreVert"; const ExpandMore = styled((props) => { const { expand, ...other } = props; return <IconButton {...other} />; })(({ theme, expand }) => ({ transform: !expand ? "rotate(0deg)" : "rotate(180deg)", marginLeft: "auto", transition: theme.transitions.create("transform", { duration: theme.transitions.duration.shortest }) })); export default function RecipeReviewCard() { const [expanded, setExpanded] = React.useState(false); const handleExpandClick = () => { setExpanded(!expanded); }; const cards = [ { title: "Shrimp and Chorizo Paella", description: "This impressive paella is a perfect party dish and a fun meal to cook together with your guests. Add 1 cup of frozen peas along with the mussels, if you like." }, { title: "Long Title Card", description: "This impressive paella is a perfect party dish and a fun meal to cook together with your guests. Add 1 cup of frozen peas along with the mussels, if you like. This impressive paella is a perfect party dish and a fun meal to cook together with your guests. Add 1 cup of frozen peas along with the mussels, if you like." } ]; return ( <div style={{ display: "grid", gridTemplateColumns: "repeat(2, 1fr)", gap: 20, padding: 20 }}> {cards.map((card, index) => ( <Card key={index} sx={{ height: "100%", display: "flex", flexDirection: "column" }}> <CardHeader avatar={ <Avatar sx={{ bgcolor: red[500] }} aria-label="recipe"> R </Avatar> } action={ <IconButton aria-label="settings"> <MoreVertIcon /> </IconButton> } title={card.title} subheader="September 14, 2016" /> <CardMedia component="img" height="194" image="https://mui.com/static/images/cards/paella.jpg" alt="Paella dish" /> {/* 文本区域设置flexGrow占满剩余空间 */} <CardContent sx={{ flexGrow: 1, display: "flex", flexDirection: "column" }}> <Typography variant="body2" color="text.secondary" sx={{ flexGrow: 1 }}> {card.description} </Typography> </CardContent> <CardActions disableSpacing> <IconButton aria-label="add to favorites"> <FavoriteIcon /> </IconButton> <IconButton aria-label="share"> <ShareIcon /> </IconButton> <ExpandMore expand={expanded} onClick={handleExpandClick} aria-expanded={expanded} aria-label="show more" > <ExpandMoreIcon /> </ExpandMore> </CardActions> <Collapse in={expanded} timeout="auto" unmountOnExit> <CardContent> <Typography paragraph>Method:</Typography> <Typography paragraph> Heat 1/2 cup of the broth in a pot until simmering, add saffron and set aside for 10 minutes. </Typography> <Typography paragraph> Heat oil in a (14- to 16-inch) paella pan or a large, deep skillet over medium-high heat. Add chicken, shrimp and chorizo, and cook, stirring occasionally until lightly browned, 6 to 8 minutes. Transfer shrimp to a large plate and set aside, leaving chicken and chorizo in the pan. Add pimentón, bay leaves, garlic, tomatoes, onion, salt and pepper, and cook, stirring often until thickened and fragrant, about 10 minutes. Add saffron broth and remaining 4 1/2 cups chicken broth; bring to a boil. </Typography> <Typography paragraph> Add rice and stir very gently to distribute. Top with artichokes and peppers, and cook without stirring, until most of the liquid is absorbed, 15 to 18 minutes. Reduce heat to medium-low, add reserved shrimp and mussels, tucking them down into the rice, and cook again without stirring, until mussels have opened and rice is tender, 5 to 7 minutes more. (Discard any mussels that don't open.) </Typography> <Typography> Set aside off of the heat to let rest for 10 minutes, and then serve. </Typography> </CardContent> </Collapse> </Card> ))} </div> ); }
关键修改点
- 给
Card组件添加样式:sx={{ height: "100%", display: "flex", flexDirection: "column" }},让卡片在Grid布局中填满可用高度,并启用垂直方向的flex布局 - 给文本所在的
CardContent设置flexGrow: 1,使其占据卡片除头部、图片、操作栏之外的所有剩余垂直空间 - 文本
Typography同样设置flexGrow: 1,确保文本块填充CardContent的可用区域,实现不同卡片文本区域等高
内容的提问来源于stack exchange,提问作者Rana Faraz
相关产品推荐
相关产品推荐

