是否可以使用CSS将文本长度限制为“n”行(或在垂直溢出时剪切)。

文本溢出:省略号;仅适用于1行文本。

原文:

Ultrices natoque mus mattis、aliquam、cras in pellentesquetincidunt elit purus lectus、vel ut aliquet、elementum nunc乌尔纳修女院!坐下!图比斯岛莫斯·廷西杜特!Dapibus sed aenean、magna sagitis、lorem velit

所需输出(2行):

Ultrices natoque mus mattis、aliquam、cras in pellentesquetincidunt elit purus lectus,velut aliquet,elementum。。。


当前回答

我一直在寻找这个,但后来我意识到,该死的我的网站使用php!!!为什么不在文本输入上使用修剪功能并播放最大长度。。。。

对于那些使用php的人,这里也有一个可能的解决方案:http://ideone.com/PsTaI

<?php
$s = "In the beginning there was a tree.";
$max_length = 10;

if (strlen($s) > $max_length)
{
   $offset = ($max_length - 3) - strlen($s);
   $s = substr($s, 0, strrpos($s, ' ', $offset)) . '...';
}

echo $s;
?>

其他回答

截至(几乎)2023年,不再需要一些旧的css规则。

这些显示了所需的最小值,并且可以跨浏览器工作,尽管有前缀语法:

.line-clamp   { overflow:hidden; display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:1; }
.line-clamp-2 { overflow:hidden; display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:2; }
.line-clamp-3 { overflow:hidden; display:-webkit-box; -webkit-box-orient:vertical; -webkit-line-clamp:3; }

据我所知,这只可能使用高度:(一些em值);溢出:隐藏,即使这样它也不会有幻想。。。最后。

如果这不是一个选项,我认为没有一些服务器端预处理(很难,因为文本流不可能可靠地预测)或jQuery(可能但可能很复杂)是不可能的。

这对我有用:

第二部分{宽度:200px;}p型{显示:块;/*非webkit的回退*/显示:-webkit框;高度:2.6em;/*非webkit的回退,线宽*2*/线高:1.3em;-webkit线夹:2;/*如果更改此设置,请确保更改回退线的高度和高度*/-webkit框方向:垂直;溢出:隐藏;文本溢出:省略号;}<div><p>Lorem ipsum dolor坐amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore</p><div>

目前不能,但将来可以使用文本溢出:ellipis lastline。目前,它可以在中使用供应商前缀Opera 10.60+:示例

我一直在寻找这个,但后来我意识到,该死的我的网站使用php!!!为什么不在文本输入上使用修剪功能并播放最大长度。。。。

对于那些使用php的人,这里也有一个可能的解决方案:http://ideone.com/PsTaI

<?php
$s = "In the beginning there was a tree.";
$max_length = 10;

if (strlen($s) > $max_length)
{
   $offset = ($max_length - 3) - strlen($s);
   $s = substr($s, 0, strrpos($s, ' ', $offset)) . '...';
}

echo $s;
?>