我知道您可以使用CSS规则的组合,使文本在溢出(超出父边界)时以省略号(…)结束。
是否有可能(请随意说,不)达到同样的效果,但让文本换行不止一行?
这是一个演示。
div {
width: 300px;
height: 42px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
如您所见,当文本的宽度超过div的宽度时,文本以省略号结束。但是,仍然有足够的空间让文本换行成第二行并继续。这被空白符:nowrap打断,这是省略号工作所必需的。
什么好主意吗?
附注:没有JS解决方案,如果可能的话,纯CSS。
你可以使用一个溶解的效果代替省略号,纯CSS,看起来更专业:
<div style="width: 293px; height:48px; overflow: hidden; ">
More than two line of text goes here-More than two line of text goes here
</div>
<div style="position: relative; top: -24px; width: 293px; height:24px;
background: linear-gradient(90deg, rgba(255,0,0,0) 40%, rgba(255,255,255,1) 99%);">
</div>
这里我假设你的背景颜色是白色。