CSS代码:
<style>
.hiddentext{
width:100px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
border:1px solid #ddd;}
<style>
.hiddentext{
width:100px;
overflow:hidden;
text-overflow:ellipsis;
white-space:nowrap;
border:1px solid #ddd;}
</style>
HTML代码:
<div class="hiddentext">
使用CSS限制显示内容的长度,被隐藏的部分使用“...”显示
</div>
其中: width:200px:指定 div 的宽度; overflow:hidden:超出的内容隐藏; text-overflow:ellipsis:/IE专用属性,文本溢出时显示省略标记();其他浏览器不支持; white-space:nowrap:强制内容不换行。强制在同一行内显示所有文本,直到文本结束或者遭遇 br 元素。
如果想让某个容器(div或者li或者...块级元素)显示一行文字,当文字内容过多时,不换行,而是出现省略号,可以使用text-overflow属性,还必须定义:强制文本在一行内显示(white-space:nowrap)及溢出内容为隐藏(overflow:hidden)。只有这样才能实现溢出文本显示省略号的效果。
如果你想要多行文本最后出现省略号,那么单纯的html和css是做不到的,必须用js或者其他后台语言实现。
以下代码可以限制到显示几行
.content{line-height:20px;display: -webkit-box;
-webkit-line-clamp:3;
-webkit-box-orient: vertical;
overflow: hidden;}