若是实现单行文本的溢出显示省略号同窗们应该都知道用text-overflow:ellipsis属性来,固然还须要加宽度width属来兼容部分浏览。web
overflow: hidden;
text-overflow:ellipsis;
white-space: nowrap;浏览器
可是这个属性只支持单行文本的溢出显示省略号,若是咱们要实现多行文本溢出显示省略号呢。优化
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;spa
因使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;3d
注:对象
p{position: relative; line-height: 20px; max-height: 40px;overflow: hidden;}
p::after{content: "..."; position: absolute; bottom: 0; right: 0; padding-left: 40px;
background: -webkit-linear-gradient(left, transparent, #fff 55%);
background: -o-linear-gradient(right, transparent, #fff 55%);
background: -moz-linear-gradient(right, transparent, #fff 55%);
background: linear-gradient(to right, transparent, #fff 55%);
}blog
该方法适用范围广,但文字未超出行的状况下也会出现省略号,可结合js优化该方法。ip
将height设置为line-height的整数倍,防止超出的文字露出。
给p::after添加渐变背景可避免文字只显示一半。
因为ie6-7不显示content内容,因此要添加标签兼容ie6-7(如:…);兼容ie8须要将::after替换成:after。it