我知道您可以使用CSS规则的组合,使文本在溢出(超出父边界)时以省略号(…)结束。

是否有可能(请随意说,不)达到同样的效果,但让文本换行不止一行?

这是一个演示。

div {
  width: 300px; 
  height: 42px; 
  overflow: hidden; 
  text-overflow: ellipsis; 
  white-space: nowrap;
}

如您所见,当文本的宽度超过div的宽度时,文本以省略号结束。但是,仍然有足够的空间让文本换行成第二行并继续。这被空白符:nowrap打断,这是省略号工作所必需的。

什么好主意吗?

附注:没有JS解决方案,如果可能的话,纯CSS。


当前回答

将两个类组合在一起似乎更优雅。如果只有一行,你可以删除两行类:

.ellipse { white-space: nowrap; display:inline-block; overflow: hidden; text-overflow: ellipsis; } .two-lines { -webkit-line-clamp: 2; display: -webkit-box; -webkit-box-orient: vertical; white-space: normal; } .width{ width:100px; border:1px solid hotpink; } <span class='width ellipse'> some texts some texts some texts some texts some texts some texts some texts </span> <span class='width ellipse two-lines'> some texts some texts some texts some texts some texts some texts some texts </span>

其他回答

简单的CSS属性可以做到这一点。下面是一个三行省略号。

display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;

看看这个纯css版本:http://codepen.io/martinwolf/pen/qlFdp

h2 { 显示:-webkit-box; max-width: 400 px; -webkit-line-clamp: 2; -webkit-box-orient:垂直; 溢出:隐藏; 文本溢出:省略; } <h2> .</h2> .</h2> .</h2> .</h2> .</h2> .</h2> .

对于那些在scss中工作的人,你需要在注释的开头添加!autoprefixer,这样它就会为postcss保留:

我遇到了这个问题,这就是为什么把它贴在这里

line-height: 1em;
max-height: 2em;
display: -webkit-box;
/*! autoprefixer: off */
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;

参考

你可以使用一个溶解的效果代替省略号,纯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>

这里我假设你的背景颜色是白色。

限制为少数行几乎在所有浏览器中都可以工作,但省略号(3点)将不会在Firefox和IE中显示。演示- http://jsfiddle.net/ahzo4b91/1/

div {
width: 300px;
height: 2.8em;
line-height: 1.4em;
display: flex;
-webkit-line-clamp: 2;
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden; 
}