我有一个具有特定宽度和高度的<p>
标签。
我想使用text-overflow:ellipsis
来得到...
,如果标签中的文本太长。
这个问题能否用CSS解决多行文本?
通过谷歌搜索,没有找到任何有前途的信息,因此我认为这是不可能的。
我确实发现了text-overflow: -o-ellipsis-lastline
,但它只在Opera浏览器中有效:http://people.opera.com/dstorey/text/text-overflow.html (镜像:http://jsbin.com/exugux/)
还有一个类似的基于WebKit的解决方案:http://dropshado.ws/post/1015351370/webkit-line-clamp
display: -webkit-box;
-webkit-line-clamp: $lines-to-show;
-webkit-box-orient: vertical;
height: $font-size*$line-height*$lines-to-show; /* Fallback for non-webkit */
除此之外:
max-width: $maxwidth;
overflow: hidden;
text-overflow: ellipsis;
这里是代码演示:演示链接
<!-- The block level container. `clamped-2` for 2 lines height -->
<p class="clamped clamped-2">
<!-- The inline wrapper -->
<span class="text">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
lobortis nisl ut aliquip ex ea commodo consequat.
<!-- The ellipsis, which can contain anything you want -
(a 'more' link, for example) -->
<span class="ellipsis">
…
</span>
<!-- The fill, which covers the ellipsis when the text doesn't overflow -->
<span class="fill"></span>
</span>
</p>
body {
/* We need a solid background or background-position: fixed */
background: #fff;
/* You'll need to know the line height to clamp on line breaks */
line-height: 1.5;
}
.clamped {
overflow: hidden;
position: relative;
}
/* Clamp to 2 lines, ie line-height x 2:
Obviously any number of these classes can be written as needed
*/
.clamped-2 {
max-height: 3em;
}
/* The ellipsis is always at the bottom right of the container,
but when the text doesn't reach the bottom right...
*/
.clamped .ellipsis {
background: #fff;
bottom: 0;
position: absolute;
right: 0;
}
/* ...It's obscured by the fill, which is positioned at the bottom right
of the text, and occupies any remaining space.
*/
.clamped .fill {
background: #fff;
height: 100%;
position: absolute;
width: 100%;
}
这里有一个演示用的小工具:改变浏览器窗口大小或更改文本,可看到它从省略号转换为不省略号。我写了一个Javascript函数来解决多行省略号问题。
function ellipsizeTextBox(id) {
var el = document.getElementById(id);
var keep = el.innerHTML;
while(el.scrollHeight > el.offsetHeight) {
el.innerHTML = keep;
el.innerHTML = el.innerHTML.substring(0, el.innerHTML.length-1);
keep = el.innerHTML;
el.innerHTML = el.innerHTML + "...";
}
}
修改了用户1152475的函数,使其按单词(以空格分隔)而非按字符处理。
function ellipsizeTextBox(id) {
var el = document.getElementById(id);
var wordArray = el.innerHTML.split(' ');
while(el.scrollHeight > el.offsetHeight) {
wordArray.pop();
el.innerHTML = wordArray.join(' ') + '...';
}
}
HTML没有提供这样的功能,这非常令人沮丧。
这就是为什么我开发了一个小型库来处理这个问题。该库提供了用于建模和执行字级文本呈现的对象。这应该正好满足您的需求:
阅读更多内容,请访问http://www.samuelrossille.com/home/jstext获取截图、教程和下载链接。
jstext-examples.html
并调整大小会产生这种情况,显然有点破碎:http://i.stack.imgur.com/aoA1i.png - thirtydot正如之前所指出的,有一种奇怪的方法可以通过David DeSandro发布的webkit-box来实现:
elements_to_style {
display: -webkit-box;
overflow : hidden;
text-overflow: ellipsis
-webkit-line-clamp: number_of_lines_you_want;
-webkit-box-orient: vertical;
}
嘿,你可以使用CSS这种方式来实现。
适用于Chrome和Safari浏览器
.block-with-text {
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
适用于Firefox和Internet Explorer浏览器
* styles for '...' */
.block-with-text {
/* hide text if it more than N lines */
overflow: hidden;
/* for set '...' in absolute position */
position: relative;
/* use this value to count block height */
line-height: 1.2em;
/* max-height = line-height (1.2) * lines max number (3) */
max-height: 3.6em;
/* fix problem when last visible word doesn't adjoin right side */
text-align: justify;
/* place for '...' */
margin-right: -1em;
padding-right: 1em;
}
/* create the ... */
.block-with-text:before {
/* points in the end */
content: '...';
/* absolute position */
position: absolute;
/* set position to right bottom corner of block */
right: 0;
bottom: 0;
}
/* hide ... if we have text, which is less than or equal to max lines */
.block-with-text:after {
/* points in the end */
content: '';
/* absolute position */
position: absolute;
/* set position to right bottom corner of text */
right: 0;
/* set width and height */
width: 1em;
height: 1em;
margin-top: 0.2em;
/* bg color = bg color under block */
background: white;
}
text-overflow:ellipsis
在火狐浏览器中目前还不能正常工作(据说计划在发布FF7时解决)。 - Spudley