单行超出显示省略号
.box{
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
width: 100px;
}
多行超出显示省略号
1、css实现多行文本超出省略号之直接用css属性设置(只有-webkit内核才有作用)
移动端浏览器绝大部分是WebKit内核的,所以该方法适用于移动端;
.box{
overflow:hidden;
text-overflow:ellipsis;
display:-webkit-box;
-webkit-line-clamp:2;
-webkit-box-orient:vertical;
}
2、css实现多行文本超出省略号之利用绝对定位和padding;(跨浏览器解决方案)
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%);
}
注:
① 将height设置为line-height的整数倍,防止超出的文字露出。
② 给p::after添加渐变背景可避免文字只显示一半。
③ 由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:…);兼容ie8需要将::after替换成:after。