三种实现单行、多行文本溢出显示省略号…的方法

一、单行文字溢出显示省略号

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

**三个属性必须一起使用,其中ellipsis是省略号效果,这种方式只适合单行文本。效果如

二、多行文字溢出显示省略号

利用WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;

{display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden;}

1、-webkit-line-clamp限制在一个块元素中文本显示的行数。它需要组合其他的WebKit属性。常见结合属性:

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

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

 display: -webkit-box;
  overflow: hidden;
  white-space: normal;
  text-overflow: ellipsis;
  word-wrap: break-word;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;

三、这是网上一个大神的一个方法,比较炫酷

.nr .text{position: relative; line-height: 20px; max-height:120px;overflow: hidden;}

.text:after{

content: "...."; color:#000; position: absolute; bottom: 0; right: 0; padding-left:60px;

background: -webkit-linear-gradient(left, transparent, #C4E2D870%);

background: -o-linear-gradient(right, transparent, #C4E2D8 70%);

background: -moz-linear-gradient(right, transparent, #C4E2D8 70%);

background: linear-gradient(to right, transparent,#C4E2D8 70%);

}

**注意

1、将height设置为line-height的整数倍,防止超出的文字露出。

2、给 .text:after 添加渐变背景可避免文字只显示一半。

3、4个background是一个意思,一个效果,只是为了兼容不同浏览器;常用浏览器使用最后一个background就可以了。

word-break: break-all;让单词折断

word-wrap: break-word;让英文换行

原文地址:https://www.cnblogs.com/fqh123/p/9944964.html