You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NextJS+Material UI电商网站产品名称多行截断及-webkit相关样式不生效问题解决

解决Material UI中商品名称多行截断并对齐价格的问题

嘿,我正好碰到过类似的问题!你现在的情况是长商品名撑开Card导致价格不对齐,而且尝试的多行截断CSS里webkit属性没生效,我来帮你一步步解决:

一、先搞定多行文本截断的问题

你写的twoLines类有几个小细节需要调整,这也是webkit属性没生效的核心原因:

  1. 修正样式类的写法
    把固定宽度去掉(响应式布局里固定width会适配出错),调整whiteSpace为normal,确保样式能正确作用:

    twoLines: {
      overflow: 'hidden',
      textOverflow: 'ellipsis',
      display: '-webkit-box',
      WebkitLineClamp: 2, // 驼峰式写法是对的,Material UI会自动转成-webkit-line-clamp
      WebkitBoxOrient: 'vertical',
      whiteSpace: 'normal', // 换成normal,让文本自动换行适配容器
      // 移除width: '400px',让它继承父容器的宽度
    }
    
  2. 确保样式正确应用到Typography
    把这个类直接加到显示商品名的Typography组件上:

    <Typography className={classes.twoLines}>{product.name}</Typography>
    

    注意:如果你的Typography设置了variant或者其他自带样式,检查有没有覆盖display属性——-webkit-box需要这个display值才能生效,别让其他样式把它改成inline或者别的。

二、彻底解决价格对齐问题(从根源避免高度差异)

就算文本截断了,偶尔还是可能有高度细微差异,不如直接用Flex布局让Card的价格区域固定在底部:

  1. 给Card添加Flex布局样式

    card: {
      border: 'none',
      boxShadow: 'none',
      display: 'flex',
      flexDirection: 'column',
      height: '100%', // 让Card占满Grid item的高度
    }
    
  2. 让CardContent占满剩余空间

    cardContent: {
      flexGrow: 1, // 把CardActionArea里的剩余空间占满,推动价格区域到Card底部
    }
    
  3. 修改JSX结构
    把样式应用到对应组件上,同时调整CardActions的宽度确保内容居中:

    <Grid container spacing={3}>
      {data.products.map((product) => (
        <Grid item md={2} key={product.name}>
          <Card className={classes.card}>
            <NextLink href={`/product/${product.slug}`} passHref>
              <CardActionArea style={{ flexGrow: 1 }}>
                <CardMedia component="img" image={product.image} title={product.name} />
                <CardContent className={classes.cardContent}>
                  <Typography className={classes.twoLines}>{product.name}</Typography>
                </CardContent>
              </CardActionArea>
            </NextLink>
            <CardActions className={classes.cardActionsProducts}>
              <div style={{ textAlign: 'center', width: '100%' }}>
                <Typography style={{ fontSize: '12px', color: 'black', fontWeight: 400, marginTop: '10px' }} >
                  {product.numReviews} reviews | sold {product.sold}
                </Typography>
              </div>
            </CardActions>
            <CardActions className={classes.cardActionsProducts}>
              <Typography style={{ fontWeight: 'bold', color: 'red', margin: '0px', width: '100%', textAlign: 'center' }} >
                {product.price}đ
              </Typography>
            </CardActions>
          </Card>
        </Grid>
      ))}
    </Grid>
    

最后验证一下

  • 现代浏览器(Chrome、Edge、Safari)都支持-webkit-line-clamp,所以截断效果会正常显示
  • 用Flex布局后,不管商品名是1行还是2行,价格区域都会牢牢卡在Card底部,完美对齐

内容的提问来源于stack exchange,提问作者gem173

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 13:47:46