多行和单行文本截取

/* 单行截取 */

div {
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}

 

 

/* 多行 */

/* 方法一(仅支持webkit内核浏览器) */

div {
/* 将对象作为弹性伸缩盒子模型显示 */
display: -webkit-box;
overflow: hidden;
-webkit-line-clamp: 2;
/* 设置或检索伸缩盒对象的子元素的排列方式 */
-webkit-box-orient: vertical;
/* 设置是否用...显示 */
text-overflow: ellipsis;
}

 

 

/* 方法二(通过伪元素绝对定位到行尾并遮住文字,在通过overflow:hidden隐藏多余文字),文字短的时候有空白 */
p{
position: relative;
line-height: 18px;
height: 36px;
overflow: hidden;
/* word-break: break-all; */
}
p::after{
content: '...';
font-weight: bold;
position: absolute;
bottom: 0;
right: 0;
padding: 0 20px 1px 45px;
background: -webkit-linear-gradient(linear, left top, right top, from(tgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
padding: 0 20px 1px 45px;
background: -moz-linear-gradient(linear, left top, right top, from(tgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
padding: 0 20px 1px 45px;
background: -o-linear-gradient(linear, left top, right top, from(tgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
padding: 0 20px 1px 45px;
background: -ms-linear-gradient(linear, left top, right top, from(tgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
background: linear-gradient(to right, rgba(255,255,255,0), white 50%, white)
}

<p>
测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取 danci danci dancidancidancidancidancidancidancidancidancidancidancidancidancidancidanci
</p>
 
 
/* 方法三(纯css实现,利用float) */
/* 左侧有一个盒子,如果单行高20px,两行后显示省略号,这里设置40px */
.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;
content: '...';
height: 20px;
line-height: 20px;
/* 三个省略号的宽度 */
width: 30px;
/* 使盒子不占位置 */
margin-left: -30px;
/* 省略号位置 */
position: relative;
left: 100%;
top: -20px;
padding-right: 5px;
background: #fff;
}
<div class="wrap">
<div class="text">
777777777777777777777777
测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取 danci danci dancidancidancidancidancidancidancidancidancidancidancidancidancidancidanci
测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取测试多行截取 danci danci dancidancidancidancidancidancidancidancidancidancidancidancidancidancidanci
</div>
</div>
 
原文参考:https://mp.weixin.qq.com/s/hWfL0yg2aKr0Xb1qr33h8w

posted on 2018-11-07 11:37  daV_chen  阅读(179)  评论(0编辑  收藏  举报

导航