CSS text-overflow: ellipsis
能在第二行上使用吗?我在网络上找不到答案。
例如:
我想要的效果是这样的:
I hope someone could help me. I need
an ellipsis on the second line of...
但是实际发生的情况是这样的:
I hope someone could help me. I ...
CSS text-overflow: ellipsis
能在第二行上使用吗?我在网络上找不到答案。
例如:
我想要的效果是这样的:
I hope someone could help me. I need
an ellipsis on the second line of...
但是实际发生的情况是这样的:
I hope someone could help me. I ...
这应该在 Webkit浏览器中 可以正常工作:
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
(该链接为英文原文)
div {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
* { font-family: verdana; }
<div>
The <b><a target='_blank' href='https://developer.mozilla.org/docs/Web/CSS/-webkit-line-clamp'>-webkit-line-clamp</a></b> CSS property allows limiting of the contents of a block container to the specified number of lines. It only works in combination with the display property set to <b>-webkit-box</b> or <b>-webkit-inline-box</b> and the <b>-webkit-box-orient</b> property set to vertical. In most cases you will also want to set overflow to hidden, otherwise the contents won't be clipped but an ellipsis will still be shown after the specified number of lines. When applied to anchor elements, the truncating can happen in the middle of the text, not necessarily at the end.
</div>
overflow: hidden
。 - Roberttext-overflow: ellipsis;
,也能够实现效果。 - Peter W.text-overflow: ellipsis;
的要求是使用单行版本的 white-space
( pre
, nowrap
等)。这意味着文本永远不会到达第二行。
因此,无法使用纯CSS实现。
我在寻找完全相同的东西时的来源: http://www.quirksmode.org/css/textoverflow.html(Quirksmode ftw!)
编辑如果好的CSS神将实施http://www.w3.org/TR/css-overflow-3/#max-lines,我们可以使用 fragments
(新)和 max-lines
(新)以纯CSS方式完成此操作。关于http://css-tricks.com/line-clampin/的更多信息
编辑2 WebKit / Blink具有 line-clamp
:-webkit-line-clamp:2
将在第二行上放置省略号。
-webkit-line-clamp: 3
- Muhammad Umer对于支持line-clamp的浏览器(大多数现代浏览器),只需使用它,对于较旧的浏览器,则回退到1行。
.text {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
@supports (-webkit-line-clamp: 2) {
overflow: hidden;
text-overflow: ellipsis;
white-space: initial;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
}
我发现Skeep的回答还不够,也需要一个overflow
样式:
overflow: hidden;
text-overflow: ellipsis;
-webkit-line-clamp: 2;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
,而问题是针对2行的,所以这个更正确。 - Juangui Jordán我使用了以下CSS属性来实现文本的行末省略:
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
这里的-webkit-line-clamp: 3;
是指在文本显示的时候,最多只显示三行的内容。
如果有人正在使用SASS/SCSS并遇到了这个问题,也许这个mixin可以帮助解决:
@mixin line-clamp($numLines : 1, $lineHeight: 1.412) {
overflow: hidden;
text-overflow: -o-ellipsis-lastline;
text-overflow: ellipsis;
display: block;
/* autoprefixer: off */
display: -webkit-box;
-webkit-line-clamp: $numLines;
-webkit-box-orient: vertical;
max-height: $numLines * $lineHeight + unquote('em');
}
这只在Webkit浏览器中添加省略号,其他浏览器会直接截断。
/* autoprefixer: off */
更改为/*! autoprefixer: off */
,否则在编译后,-webkit-box-orient: vertical;
会被删除,这意味着省略号永远不会显示。 - Nathan如果有人尝试在 flexbox 中使用 line-clamp,那么稍微麻烦一些——特别是在使用非常长的单词时进行 torture testing。这段代码片段中唯一真正的区别是包含截断文本的 flex 项中的 min-width: 0;
和 word-wrap: break-word;
。向https://css-tricks.com/flexbox-truncated-text/致敬,感谢他们的见解。免责声明:在撰写本答案时,除了 Chrome 浏览器外,浏览器支持较差。但是,事情可能已经得到改善。
.flex-parent {
display: flex;
}
.short-and-fixed {
width: 30px;
height: 30px;
background: lightgreen;
}
.long-and-truncated {
margin: 0 20px;
flex: 1;
min-width: 0;/* Important for long words! */
}
.long-and-truncated span {
display: inline;
-webkit-line-clamp: 3;
text-overflow: ellipsis;
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
word-wrap: break-word;/* Important for long words! */
}
<div class="flex-parent">
<div class="flex-child short-and-fixed">
</div>
<div class="flex-child long-and-truncated">
<span>asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd</span>
</div>
<div class="flex-child short-and-fixed">
</div>
</div>
http://codepen.io/AlgeoMA/pen/JNvJdx(codepen版本)
.show-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
对于多行文本,你可以使用一个Polyfill,比如在github上的jQuery autoellipsis http://pvdspek.github.com/jquery.autoellipsis/
dotdotdot
。 - adi518我不是JS专家,但我想出了几种方法可以做到这一点。
HTML代码:
<p id="truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi elementum consequat tortor et euismod. Nam commodo consequat libero vel lobortis. Morbi ac nisi at leo vehicula consectetur.</p>
然后使用jQuery将其截断到特定的字符数,但保留最后一个单词,方法如下:
// Truncate but leave last word
var myTag = $('#truncate').text();
if (myTag.length > 100) {
var truncated = myTag.trim().substring(0, 100).split(" ").slice(0, -1).join(" ") + "…";
$('#truncate').text(truncated);
}
结果看起来像这样:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
elementum consequat tortor et…
或者,你可以简单地将其截断到特定的字符数,像这样:
// Truncate to specific character
var myTag = $('#truncate').text();
if (myTag.length > 15) {
var truncated = myTag.trim().substring(0, 100) + "…";
$('#truncate').text(truncated);
}
结果看起来像这样:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi
elementum consequat tortor et euismod…
希望能对你有所帮助。
这是jsFiddle链接。