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

如何阻止相对定位div的文本内容溢出边界并避免重叠?

解决文本溢出重叠问题

问题核心是.details容器未继承父容器的高度限制,导致Grid布局的行高随内容自动扩展,超出父容器max-height后,子元素的overflow-y: hidden无法生效,最终文本溢出重叠。

修复方案

给.details添加height: 100%,让Grid布局高度严格限制在父容器的max-height范围内,两行将平分父容器高度,子元素的溢出截断即可正常工作:

.parent {
  margin: 5px;
  max-height: 100px;
  max-width: 400px;
  background: pink;
}

.details {
  width: 100%;
  text-align: center;
  font-size: 14pt;
  display: grid;
  grid-template-rows: 1fr 1fr;
  height: 100%; /* 新增:继承父容器高度 */
}

.detail1 {
  grid-row: 1;
  overflow-y: hidden;
}

.detail2 {
  grid-row: 2;
  overflow-y: hidden;
}

如果希望文本截断时显示省略号,可给子元素补充以下样式(可选):

.detail1, .detail2 {
  white-space: nowrap;
  text-overflow: ellipsis;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 00:22:21