目录
1.单行文本溢出显示省略号
.box {
/*强制文本在一行内显示*/
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
/*强制文本在一行内显示*/
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 100px;
}
</style>
</head>
<body>
<div class="box">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</body>
</html>
2.多行文本溢出显示省略号
.box {
overflow: hidden;
text-overflow: ellipsis;
/* 将对象作为弹性伸缩盒子模型显示 */
display: -webkit-box;
/* 限制在一个块元素显示的文本的行数 */
/* -webkit-line-clamp 其实是一个不规范属性,使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;*/
-webkit-line-clamp: 2;
/* 设置或检索伸缩盒对象的子元素的排列方式 */
-webkit-box-orient: vertical;
}
实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
overflow: hidden;
text-overflow: ellipsis;
width: 200px;
/* 将对象作为弹性伸缩盒子模型显示 */
display: -webkit-box;
/* 限制在一个块元素显示的文本的行数 */
/* -webkit-line-clamp 其实是一个不规范属性,使用了WebKit的CSS扩展属性,该方法适用于WebKit浏览器及移动端;*/
-webkit-line-clamp: 2;
/* 设置或检索伸缩盒对象的子元素的排列方式 */
-webkit-box-orient: vertical;
}
</style>
</head>
<body>
<div class="box">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</body>
</html>
多行文本内容英语单词不太合适,下面的也是。
3.利用伪类实现省略号
.t3{
position: relative;
height: 40px;
line-height: 20px;
overflow: hidden;
}
.t3::after{
content: '...';
position: absolute;
bottom: 0;
right: 0;
padding-left: 40px;
background: linear-gradient(to right, transparent, #fff 55%);
}
实例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.t3{
width: 200px;
position: relative;
height: 40px;
line-height: 20px;
overflow: hidden;
}
.t3::after{
content: '...';
position: absolute;
bottom: 0;
right: 0;
padding-left: 40px;
background: linear-gradient(to right, transparent, #fff 55%);
}
</style>
</head>
<body>
<div class="t3">省略号的位置可以通过定位调整啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</body>
</html>
4.英文自动换行
- word-break:break-all;只对英文起作用,以字母作为换行依据
- word-wrap:break-word; 只对英文起作用,以单词作为换行依据
- white-space:pre-wrap; 只对中文起作用,强制换行
- white-space:nowrap; 强制不换行,都起作用
- white-space:nowrap; overflow:hidden; text-overflow:ellipsis;不换行,超出部分隐藏且以省略号形式出现(部分浏览器支持)
.t3{
position: relative;
height: 40px;
line-height: 20px;
overflow: hidden;
word-break:break-all;
}
.t3::after{
content: '...';
position: absolute;
bottom: 0;
right: 0;
padding-left: 40px;
background: linear-gradient(to right, transparent, #fff 55%);
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.t3{
width: 200px;
position: relative;
height: 40px;
line-height: 20px;
overflow: hidden;
word-break:break-all
}
.t3::after{
content: '...';
position: absolute;
bottom: 0;
right: 0;
padding-left: 40px;
background: linear-gradient(to right, transparent, #fff 55%);
}
</style>
</head>
<body>
<div class="t3">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
<div class="t3">啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊</div>
</body>
</html>