CSS文本溢出显示省略号方法之让后端写
单行文本溢出显示省略号
方法1
white-space:nowrap; /*强制一行显示文本*/
overflow:hidden; /*溢出部分隐藏*/
text-overflow:ellipsis; /*文字溢出用省略号代替*/
方法2
.box {
width: 200px;
line-height:30px;
height:90px;
background-color: red;
position:relative;
text-align:justify;
overflow: hidden;
}
.box:after{
content:"...";
position:absolute;
right:0;
bottom:0;
width:1em;
}
多行文本溢出显示省略号(有兼容性问题,适合webkit或移动端)
方法1
overflow:hidden; /*溢出部分隐藏*/
text-overflow:ellipsis; /*文字溢出用省略号代替*/
display:-webkit-box;
-webkit-line-clamp:2; /*显示的行数*/
-webkit-box-orient:vertical;
方法2
.box {
width: 200px;
line-height:30px;
height:90px;
background-color: red;
position:relative;
text-align:justify;
overflow: hidden;
}
.box:after{
content:"<span>…<span/>";
position:absolute;
right:0;
bottom:0;
background-color: red;
width:1em;
}
该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。
说明:
1. 将height设置为line-height的整数倍,防止超出的文字露出。
2. 给p::after添加渐变背景可避免文字只显示一半。
3. 由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:<span>…<span/>);兼容ie8需要将p::after替换成:after。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/4914.html