是否可以使用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。。。


当前回答

该线程的解决方案是使用jquery插件dotdotdot。这不是一个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;
}
.word-limit{
   display: block;
   text-overflow: ellipsis;
   white-space: nowrap;
   word-wrap: break-word;
   overflow: hidden;
}

我真的很喜欢线夹,但还不支持firefox。。所以我做了一个数学计算,然后隐藏溢出

.body-content.body-overflow-hidden h5 {
    max-height: 62px;/* font-size * line-height * lines-to-show(4 in this case) 63px if you go with jquery */
    overflow: hidden;
}
.body-content h5 {
    font-size: 14px; /* need to know this*/
    line-height:1,1; /*and this*/
}

现在让我们假设你想通过jQuery删除并添加这个类,你需要有一个额外的像素,所以它的最大高度是63像素,这是因为你需要每次检查高度是否大于62像素,但是在4行的情况下,你会得到一个假真,所以额外的像素可以解决这个问题,不会产生任何额外的问题

我将为此粘贴一个coffee脚本,作为一个示例,它使用了默认隐藏的两个链接,类读得更多,读得更少,它将删除溢出不需要的链接,并删除主体溢出类

jQuery ->

    $('.read-more').each ->
        if $(this).parent().find("h5").height() < 63
             $(this).parent().removeClass("body-overflow-hidden").find(".read-less").remove()
             $(this).remove()
        else
            $(this).show()

    $('.read-more').click (event) ->
        event.preventDefault()
        $(this).parent().removeClass("body-overflow-hidden")
        $(this).hide()
        $(this).parent().find('.read-less').show()

    $('.read-less').click (event) ->
        event.preventDefault()
        $(this).parent().addClass("body-overflow-hidden")
        $(this).hide()
        $(this).parent().find('.read-more').show()

这对我有用:

第二部分{宽度: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>

我有一个很好的解决方案,但省略号使用了渐变。当你有动态文本时,它会起作用,所以你不知道它是否足够长,需要一个椭圆。优点是您不必进行任何JavaScript计算,它适用于包括表格单元格在内的可变宽度容器,并且是跨浏览器的。它使用了一些额外的div,但它很容易实现。

标记:

<td>
    <div class="fade-container" title="content goes here">
         content goes here
         <div class="fade">
    </div>
</td>

CSS:

.fade-container { /*two lines*/
    overflow: hidden;
    position: relative;
    line-height: 18px; 
    /* height must be a multiple of line-height for how many rows you want to show (height = line-height x rows) */
    height: 36px;
    -ms-hyphens: auto;
    -webkit-hyphens: auto;
    hyphens: auto;
    word-wrap: break-word;
}

.fade {
        position: absolute;
        top: 50%;/* only cover the last line. If this wrapped to 3 lines it would be 33% or the height of one line */
        right: 0;
        bottom: 0;
        width: 26px;
        background: linear-gradient(to right,  rgba(255,255,255,0) 0%,rgba(255,255,255,1) 100%);
}

博客文章:http://salzerdesign.com/blog/?p=453

示例页面:http://salzerdesign.com/test/fade.html