- 单行文本溢出
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
我们只需要这3个属性就好了
overflow:hidden 表示的是将超出的部分隐藏
text-overflow:ellipsis超出的部分显示为省略号
white-space:nowrap表示的是规定段落的文本不进行换行
-多行文本溢出
第一种方法
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
这种方法因为使用了webkit的css拓展属性,所以只能用于webkit浏览器和移动端
-webkit-box-orient: vertical;设置伸缩盒子对象子元素的排列方式
-webkit-line-clamp: 3;限制一个盒子模型显示的文本的行数
第二种方法
#div3{
border: 1px solid;
width: 450px;
position: relative;
line-height: 1.4em;
height: 4.2em;
overflow: hidden;
}
#div3::after{
content:'...';
font-weight:bold;
position:absolute;
bottom:0;
right:0;
padding:0 20px 1px 45px;
background:#fff;
background: -webkit-linear-gradient(left, transparent, #fff 55%);
background: -o-linear-gradient(right, transparent, #fff 55%);
background: -moz-linear-gradient(right, transparent, #fff 55%);
background: linear-gradient(to right, transparent, #fff 55%);
}
这种是跨浏览器的解决方法
使用绝对定位
将height设置成line-height的整数倍,防止超出的文字漏出
利用伪类::after设置渐变的背景以避免文字显示出一半,
使用伪类来实现立体效果,需要注意的是必须为其赋值一个content属性