简体   繁体   English

webkit-line-clamp:限制没有省略号的行数

[英]webkit-line-clamp: limit amount of lines with out ellipsis

I'm trying to limit the number of lines in my text area.我正在尝试限制文本区域中的行数。 I follow this article: https://www.codegrepper.com/code-examples/css/how+to+show+specific+amount+of+multiline+text+in+html+css我关注这篇文章: https://www.codegrepper.com/code-examples/css/how+to+show+specific+amount+of+multiline+text+in+html+css

.class{
   word-break: break-word;
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   line-height: 16px; /* fallback */
   max-height: 32px; /* fallback */
   -webkit-line-clamp: 2; /* number of lines to show */
   -webkit-box-orient: vertical;
}

it works, but I don't want ellipsis at the end of the text.它有效,但我不想在文本末尾使用省略号。 Simply replacing ellipsis by clip doesn't work.简单地用clip替换ellipsis是行不通的。 How do I change it?我该如何改变它?

Here is a working example, both class a and class b give the same result:这是一个工作示例, class a 和 class b 给出相同的结果:

https://www.w3schools.com/code/tryit.asp?filename=GQI3IZL33M4T https://www.w3schools.com/code/tryit.asp?filename=GQI3IZL33M4T

<style> 
div.a {
    word-break: break-word;
    overflow: hidden;
    text-overflow: clip;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}

div.b {
    word-break: break-word;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
</style>
    
<h2>text-overflow: clip:</h2>
<div class="a">Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!</div>

<h2>text-overflow: ellipsis:</h2>
<div class="b">Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world! Hello world! Hello world! Hello world!Hello world! Hello world! Hello world!</div>

If you don't want the ellipsis you can just use a simple div with a fixed height, which is a multiple of your line height with overflow hidden.如果你不想要省略号,你可以只使用一个固定高度的简单 div,它是隐藏溢出的行高的倍数。 The div will wrap the text normally, and will hide anything that's overflown. div 将正常包装文本,并隐藏任何溢出的内容。

The only reason to use the -webkit-line-clamp: 2;使用-webkit-line-clamp: 2; solution is to automatically add the ellipsis.解决方案是自动添加省略号。 Otherwise don't use it.否则不要使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM