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

Grid布局文本换行问题:需实现第三行文本换行效果

解决Grid布局中文本换行异常问题

你的问题出在h4标签的CSS属性冲突以及a标签的display设置上,直接修改以下两处即可实现文本最多3行、超出显示省略的效果:


关键修改点

  1. 移除h4中的white-space: nowrap
    这个属性会强制文本单行显示,和你设置的-webkit-line-clamp: 3(最多3行)完全冲突,必须删除。

  2. 修改h4内a标签的display属性
    把display: inline-block !important改成display: block,让链接文本能继承h4的多行换行规则,同时保留点击区域覆盖整个文本块。


修改后的完整CSS代码

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  grid-gap: 10px;
  padding: 0px 0 50px 0;
  justify-content: start;
  margin: 0;
  list-style: none;
}

.grid__item {
  background: #2774ae;
  border: 1px solid #ccc;
  padding: 10px 0 10px 0;
  margin: 0 !important;
  border-radius: 5px;
  position: relative;
  transition: all 0.2s ease;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  min-height: 190px;
  
  &:hover,
  &:focus-within {
    background-color: #005587 !important;
  }

  a {
    position: relative;
    z-index: 1;
    display: inline-block;
    color: #ffffff !important;
    text-decoration: none !important;
    text-decoration-skip-ink: auto;
  }

  h4 {
    font-size: 1.05rem !important;
    font-weight: 700;
    letter-spacing: 1px;
    color: #ffffff !important;
    margin: 1rem 1.5rem !important;
    text-overflow: ellipsis;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    max-width: 190px;
    -webkit-box-orient: vertical;
  }

  h4 a {
    position: static;
    display: block !important; /* 修改此处 */
    color: #ffffff !important;
    text-decoration: none !important;
    text-decoration-skip-ink: auto;
  }

  h4 a:hover,
  h4 a:focus {
    color: #ffffff !important;
    background-color: transparent !important;
  }

  h4 a:before {
    content: "";
    display: block;
    position: absolute;
    z-index: 0;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    transition: background-color 0.1s ease-out;
    background-color: transparent; 
  }

  h3 {
    font-weight: 500;
    font-size: 1.05rem !important;
    line-height: 1;
    color: #ffffff !important;
    text-align: left !important;
    margin: 1.5rem 1.5rem 0.25rem 1.5rem !important;
  }
  
  h3:after {
    display: block;
    content: '';
    width: 55px;
    height: 4px;
    background: #FFD100;
    margin: 1rem 0 0.7rem 0;
    border-radius: 99px;
  }

  ul {
    display: flex;
    margin: 0 0 6px 0 !important;
  }

  ul li {
    margin-right: 10px !important;
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 17:44:56