CSS实现单行、多行文本溢出显示省略号的实现方法
1,单行溢出,超出部分显示...或者截取。前提必须有宽度。
CSS:{width:xxpx;overflow:hidden;white-space:nowrap;text-overflow:ellipsis;},截取为clip;
实现代码:
- css">
- width:300px;
- overflow: hidden;
- text-overflow:ellipsis;
- whitewhite-space: nowrap;
效果如图:
- css">
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-line-clamp: 3;
- overflow: hidden;
效果如图:
- p{position: relative; line-height: 20px; max-height: 40px;overflow: hidden;}
- p::after{content: "..."; position: absolute; bottombottom: 0; rightright: 0; padding-left: 40px;
- background: -webkit-linear-gradient(left, transparent, #fff 55%);
- background: -o-linear-gradient(rightright, transparent, #fff 55%);
- background: -moz-linear-gradient(rightright, transparent, #fff 55%);
- background: linear-gradient(to rightright, transparent, #fff 55%);
- }
适用范围:
该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。
注:
1.将height设置为line-height的整数倍,防止超出的文字露出。
2.给p::after添加渐变背景可避免文字只显示一半。
3.由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:<span>…<span/>);兼容ie8需要将::after替换成:after。