对于文本溢出处理的方法

来源:互联网 发布:算法工程师的年薪 编辑:程序博客网 时间:2024/06/07 02:33
1.单行文本溢出处理
{
    overflow:hidden;
    text-overflow:ellipsis;
    white-space:nowrap;
}
2.多行文本溢出处理(比较适合webkit浏览器和移动端的浏览器)
{
    overflow:hidden;
    text-overflow:ellipsis;
    display:-webkit-box;
    -webkit-line-clamp:2;
    -webkit-box-orient:vertical;
}
3.跨浏览器兼容的方案
//比较靠谱的简单做法是设置相对定位的容器高度,用包含省略号(……)的元素模拟实现
p{
    position:relative;
    line-height:1.4em;
    height:4.2rem;
    overflow:hidden;
}
p:after{
    content:'...';
    font-size:bold;
    position:absolute;
    bottom:0;
    right:0;
    padding:0 20px 1px 45px;
    background:url(http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;(半透明的png做了减淡的效果,或者设置背景颜色)
}
4.javaScript方案
    Clamp.js
    地址:https://github.com/josephschmitt/Clamp.js