CSS文字换行与省略号

14

只使用CSS,我想要在容器垂直填满后实现单词换行并加省略号。根据研究,如果使用white-space: nowrap就可以加省略号,但这只能显示一行。

这个场景是一个已知高度和可变宽度的父div,其中有一个子元素a,如果a没有到max-height: 100%,则应垂直居中。

如果在父级中指定display: table,并在子级(即a)中指定display: table-cellmax-height: xxxtext-overflow: ellipsis,似乎也可以实现这个效果。

我觉得这不是什么难事,但现在时间很晚,可能会漏掉些什么。是否有一个名为still-cant-be-done-in-html5.com的网站?

演示

2个回答

12

有一个仅适用于webkit的功能叫做“行夹紧”,可以实现我认为你正在寻找的效果。不幸的是,它是非标准的,目前没有计划(至少我所知道的)将其纳入任何标准中。

https://css-tricks.com/line-clampin/

这可以通过以下CSS来实现:
.line-clamp {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;  
}

使用这段代码,如果元素的内容超过三行文字,则会在第三行用省略号截断。

有几个不同的JavaScript库尝试实现类似的功能,您可以在我在答案中提供的链接中看到。不幸的是,使用标准的CSS,text-overflow: ellipsis仅限于单行。

作为替代,目前在W3C邮件列表中"有一个关于“区域片段”的线程",以及如何使用它们使多行元素具有省略号,但规范仍处于起草阶段,可能需要几年才能进入任何浏览器。


这就是诀窍,它是一个Chrome扩展程序,所以WebKit很好。现在我只需要想办法垂直对齐a文本,是否有一些WebKit的魔法可以做到呢? - Mark Robbins
@Mark 你可以使用Flexbox相对容易地垂直对齐元素。http://the-echoplex.net/flexyboxes/ - Dan Herbert

4

据我所知,你无法仅使用 CSS 来实现此操作。

你可以使用 Clamp.js。但是我必须警告你,它的结果有时会出错。因此,我使用了一个小技巧,将 clamp 的行数设置为 3,并将包装元素的 line-height CSS 属性设置为恰好是元素高度的 1/3rd

// Copying Clamp.min.js here because we cannot execute RAW file from github as it's content type is 'text/plain'.
// Scroll down to see the last line of code.
/*!
 * Clamp.js 0.5.1
 *
 * Copyright 2011-2013, Joseph Schmitt http://joe.sh
 * Released under the WTFPL license
 * http://sam.zoy.org/wtfpl/
 */
(function(){window.$clamp=function(c,d){function s(a,b){n.getComputedStyle||(n.getComputedStyle=function(a,b){this.el=a;this.getPropertyValue=function(b){var c=/(\-([a-z]){1})/g;"float"==b&&(b="styleFloat");c.test(b)&&(b=b.replace(c,function(a,b,c){return c.toUpperCase()}));return a.currentStyle&&a.currentStyle[b]?a.currentStyle[b]:null};return this});return n.getComputedStyle(a,null).getPropertyValue(b)}function t(a){a=a||c.clientHeight;var b=u(c);return Math.max(Math.floor(a/b),0)}function x(a){return u(c)*
a}function u(a){var b=s(a,"line-height");"normal"==b&&(b=1.2*parseInt(s(a,"font-size")));return parseInt(b)}function l(a){if(a.lastChild.children&&0<a.lastChild.children.length)return l(Array.prototype.slice.call(a.children).pop());if(a.lastChild&&a.lastChild.nodeValue&&""!=a.lastChild.nodeValue&&a.lastChild.nodeValue!=b.truncationChar)return a.lastChild;a.lastChild.parentNode.removeChild(a.lastChild);return l(c)}function p(a,d){if(d){var e=a.nodeValue.replace(b.truncationChar,"");f||(h=0<k.length?
k.shift():"",f=e.split(h));1<f.length?(q=f.pop(),r(a,f.join(h))):f=null;m&&(a.nodeValue=a.nodeValue.replace(b.truncationChar,""),c.innerHTML=a.nodeValue+" "+m.innerHTML+b.truncationChar);if(f){if(c.clientHeight<=d)if(0<=k.length&&""!=h)r(a,f.join(h)+h+q),f=null;else return c.innerHTML}else""==h&&(r(a,""),a=l(c),k=b.splitOnChars.slice(0),h=k[0],q=f=null);if(b.animate)setTimeout(function(){p(a,d)},!0===b.animate?10:b.animate);else return p(a,d)}}function r(a,c){a.nodeValue=c+b.truncationChar}d=d||{};
var n=window,b={clamp:d.clamp||2,useNativeClamp:"undefined"!=typeof d.useNativeClamp?d.useNativeClamp:!0,splitOnChars:d.splitOnChars||[".","-","\u2013","\u2014"," "],animate:d.animate||!1,truncationChar:d.truncationChar||"\u2026",truncationHTML:d.truncationHTML},e=c.style,y=c.innerHTML,z="undefined"!=typeof c.style.webkitLineClamp,g=b.clamp,v=g.indexOf&&(-1<g.indexOf("px")||-1<g.indexOf("em")),m;b.truncationHTML&&(m=document.createElement("span"),m.innerHTML=b.truncationHTML);var k=b.splitOnChars.slice(0),
h=k[0],f,q;"auto"==g?g=t():v&&(g=t(parseInt(g)));var w;z&&b.useNativeClamp?(e.overflow="hidden",e.textOverflow="ellipsis",e.webkitBoxOrient="vertical",e.display="-webkit-box",e.webkitLineClamp=g,v&&(e.height=b.clamp+"px")):(e=x(g),e<=c.clientHeight&&(w=p(l(c),e)));return{original:y,clamped:w}}})();


// CODE BEGINS HERE
$clamp(document.getElementById('toclamp'), {
  clamp: 3
});
div {
  width: 100px;
  height: 58px;
  border: 1px solid red;
  line-height: 20px;
}
<div id="toclamp">
  The quick brown fox jumps over the lazy dog.
</div>
<br />
<div id="noclamp">
  The quick brown fox jumps over the lazy dog.
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接