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

如何用CSS消除文本元素行间距?line-height负值无效

解决文本行重叠/消除行间距的方案

针对单个文本元素内行间距过大(需移除红色空白区域),且无法使用负line-height、不能拆分元素的问题,推荐以下两种可行方法:

方法1:垂直缩放元素

通过transform: scaleY()缩小元素的垂直尺寸,配合line-height: 0实现行重叠:

#my_line{
  overflow-wrap: break-word;
  line-height: 0;
  display: inline-block; /* 使transform生效 */
  transform: scaleY(0.4); /* 数值越小,行重叠程度越高,按需调整 */
  transform-origin: top left; /* 固定缩放原点,避免整体偏移 */
}

方法2:相对定位偏移文本

利用position: relative设置负top值,让文本行向上偏移重叠:

#my_line{
  overflow-wrap: break-word;
  line-height: 0;
  display: inline-block;
  position: relative;
  top: -6px; /* 根据字体大小调整负值,直到红色区域消失 */
}

两种方法都无需拆分元素,完美避开line-height不能设负值的限制,可根据实际效果调整参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 22:40:24