CSS文本截断 - - >强行一行显示不换行并会出现省略号...

一、单行文本截断 text-overflow

文本溢出经常用到的应该就是 text-overflow:ellipsis 了,只需轻松几行代码就可以实现单行文本截断。

<style>
div {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>

显示效果:

二、-webkit-line-clamp 实现

<style>
div {
  /* autoprefixer: ignore next */   
  display: -webkit-box;
  overflow: hidden;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>

PS:
 

它需要和 display、 -webkit-box-orient 和 overflow 结合使用:

display:-webkit-box; 必须结合的属性,将对象作为弹性伸缩盒子模型显示。

-webkit-box-orient; 必须结合的属性,设置或检索伸缩盒对象的子元素的排列方式。

text-overflow:ellipsis; 可选属性,可以用来多行文本的情况下,用省略号“…”隐藏超出范围的文本。

三、定位元素实现多行文本截断

<style>
p {
    position: relative;
    line-height: 
    18px;
    height: 
    36px;
    overflow: hidden;
}
p::after {
    content:"...";
    font-weight:bold;
    position:absolute;
    bottom:0;
    right:0;
    padding:0 20px 1px 45px;
/* 为了展示效果更好 */
    background: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
    background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
    background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
    background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
    background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
}
</style>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值