css文字省略号

css文字省略号一 单行溢出 1 单行溢出 超出部分显示 或者截取 前提必须有宽度 CSS width xxpx overflow hidden white space nowrap text overflow ellipsis 截取为 clip width 300px overflow hidden text overflow

大家好,我是讯享网,很高兴认识大家。

一、单行溢出

width:300px; overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 

讯享网

效果如图:
在这里插入图片描述
讯享网

但是这个属性只支持单行文本的溢出显示省略号,如果我们要实现多行文本溢出显示省略号呢。

二、多行溢出

{display:-webkit-box;overflow:hidden;text-overflow:ellipsis;-webkit-line-clamp:2;-webkit-box-orient:vertical;}

实现方法:

讯享网display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; overflow: hidden; 

在这里插入图片描述

适用范围:

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

注:

1.-webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。常见结合属性:
2.display: -webkit-box; 必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。
3.-webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。

p{position: relative; line-height: 20px; max-height: 40px;overflow: hidden;} p::after{content: "..."; position: absolute; bottombottom: 0; rightright: 0; padding-left: 40px; background: -webkit-linear-gradient(left, transparent, #fff 55%); background: -o-linear-gradient(rightright, transparent, #fff 55%); background: -moz-linear-gradient(rightright, transparent, #fff 55%); background: linear-gradient(to rightright, transparent, #fff 55%); 

注:

1.将height设置为line-height的整数倍,防止超出的文字露出。
2.给p::after添加渐变背景可避免文字只显示一半。
3.由于ie6-7不显示content内容,所以要添加标签兼容ie6-7(如:…);兼容ie8需要将::after替换成:after。

小讯
上一篇 2025-03-02 14:03
下一篇 2025-01-17 10:26

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/21511.html