2017-09-01pre自适应
pre{white-space: pre-wrap;word-wrap: break-word;}
文字超出折行
增加属性word-break: break-all;
单行省略号
overflow: hidden;text-overflow:ellipsis;white-space: nowrap;
多行省略号
overflow: hidden;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;
适用范围
因使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;
多行省略号广适配
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%);
}
适用范围:
该方法适用范围广,但文字未超出行的情况下也会出现省略号,可结合js优化该方法。
注:
1.将height设置为line-height的整数倍,防止超出的文字露出。
2.给p::after添加渐变背景可避免文字只显示一半。
3.由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:<span>…<span/>)。