单行文字
核心的3个声明:
.ell{
text-overflow:ellipsis;
white-space:nowrap;
overflow:hidden;
}
多行文字
.ellM{
height: 40px;
width: 200px;
overflow: hidden; /*超出文本隐藏*/
text-overflow:ellipsis; /*超出部分省略号显示*/
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /*自定义行数*/
}
效果图:
完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文字溢出后用省略号表示</title>
<style>
.ell{
height: 20px;
width: 200px;
background-color: antiquewhite;
text-overflow:ellipsis;
overflow: hidden;
white-space: nowrap;
}
.ellM{
height: 40px;
width: 200px;
background-color: #c8c8db;
overflow: hidden; /*超出文本隐藏*/
text-overflow:ellipsis; /*超出部分省略号显示*/
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /*自定义行数*/
}
</style>
</head>
<body>
<h3>单行文字溢出省略号效果</h3>
<div class="ell">
一个人想像自己不懂得的事很容易浪漫。
</div>
<br>
<h3>最多显示2行内容,之后用省略号</h3>
<div class="ellM">
我对自己的要求很低:我活在世上,无非想要明白些道理,遇见些有趣的事。倘能如我愿,我的一生就算成功。
</div>
</body>
</html>