溢出显示省略号
单行文本溢出显示省略号
.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;
background-color: red;
width:1em;
}
多行文本溢出显示省略号
方法一:after实现
.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;
background-color: red;
width:1em;
}
该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。
说明:
- 将height设置为line-height的整数倍,防止超出的文字露出。
- 给p::after添加渐变背景可避免文字只显示一半。
- 由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:…);兼容ie8需要将::after
替换成:after。
方法二:弹性盒
.box {
width: 200px;
line-height:30px;
background-color: red;
/* 旧版弹性盒 */
display:-webkit-box;
/* 弹性盒子元素垂直排列 */
-webkit-box-orient: vertical;
/* 控制要显示的行数 */
-webkit-line-clamp: 4;
overflow: hidden;
}
注意:因使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端
说明:
-webkit-line-clamp 用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其
他的WebKit属性。常见结合属性:
display: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。
-webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。