文本溢出

单行文本显示省略号

 
p {
    width:100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
1.容器宽度:width(px、%都可以)
2.强制文本在一行内显示:white-space:nowrap;
3.溢出内容我隐藏:overflow:hidden;
4.溢出文本时显示省略号:text-overflow:ellipsis;
 

多行文本溢出显示省略号

 
 

a.兼容pc或移动webkit内核浏览器

p {
    overflow : hidden;
    text-overflow: ellipsis;  //溢出部分显示省略号
    display: -webkit-box;    //将对象作为弹性伸缩盒子模型显示 
    -webkit-line-clamp: 2;   //限制块元素内显示的文本行数
    -webkit-box-orient: vertical;  //设置或检索伸缩盒对象的子元素的排列方式
}
 

b.跨浏览器兼容

p{
    position: relative;
    width:200px;
    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%);
    /*background:url(http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;*/
}
1.height高度真好是line-height的3倍;
2.结束的省略好用了半透明的png做了减淡的效果,或者设置背景颜色;
3.IE6-7不显示content内容,所以要兼容IE6-7可以是在内容中加入一个标签,比如用<span class="line-clamp">...</span>去模拟;
4.要支持IE8,需要将::after替换成:after;

猜你喜欢

转载自www.cnblogs.com/onlycare/p/9263497.html