CSS 实现多行文字截断

用js实现多行文字截断 https://blog.csdn.net/lqyygyss/article/details/81868679css

如下用css实现。html

有个三个盒子 div,粉色盒子左浮动,浅蓝色盒子和黄色盒子右浮动:web

当浅蓝色盒子的高度低于粉色盒子,黄色盒子仍会处于浅蓝色盒子右下方。segmentfault

若是浅蓝色盒子文本过多,高度超过了粉色盒子,则黄色盒子不会停留在右下方,而是掉到了粉色盒子下。.net

.wrap{
            height: 40px;
            line-height: 20px;
            overflow: hidden;
        }
        .wrap .text{
            float:right;
            margin-left: -5px;
            width: 100%;
            word-break: break-all;
        }
        .wrap::before{
            float:left;
            width: 5px;
            content:'';
            height: 40px;;
        }
        .wrap::after{
            float: right;
            height: 20px;
            line-height: 20px;
            width:4em;
            content:'...更多';
            margin-left:-4em;
            position: relative;
            text-align: right;
            left:100%;
            top:-20px;
            padding-right: 5px;
            background:-webkit-gradient(linear,left top,right top,from(rgba(255,255,255,.7)),to(white))
        }

html结构以下code

<div class="wrap">
        <div class="text">
            我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人我是中国人
        </div>
    </div>

这里实现是伪元素before和after来实现的。htm

若是须要捕捉用户点击更多而后展开,能够用三个div来实现。blog

这里样式须要指定高度来设定,若是是动态设置的话,用内联样式覆盖,设置高度便可。get

须要计算一行文字是多好,而后显示几行文字。it

 

参考文章:http://www.noobyard.com/article/p-sdoygkla-g.html 

这个用-webkit-line-clamp来实现,可是基本原理如出一辙。你仍是须要设置中间float的高度来实现。