使用CSS将文字长度限制为n行

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

text-overflow: ellipsis; 仅适用于1行文字。

原文:

纳图克足癣,阿里卡姆,克拉斯
Tincidunt elit purus lectus,vel utquet,elementum nunc
nunc rhoncus placerat urna! 坐吧! 乌头企鹅
mus tincidunt! Dapibus sed aenean,magna sagittis,lorem velit

所需的输出(2行):

纳图克足癣,阿里卡姆,克拉斯
Tincidunt elit purus lectus,vel utquet,elementum ...


#1楼

有一种方法,但是它仅适用于webkit。 但是,将其与line-height: X;结合使用时line-height: X; ,以及max-height: X*N; ,它也可以在其他浏览器中运行,而不会出现省略号。

.giveMeEllipsis {
   overflow: hidden;
   text-overflow: ellipsis;
   display: -webkit-box;
   -webkit-box-orient: vertical;
   -webkit-line-clamp: N; /* number of lines to show */
   line-height: X;        /* fallback */
   max-height: X*N;       /* fallback */
}

演示: http//jsfiddle.net/csYjC/1131/


#2楼

您可以执行以下操作:

.max-lines {
  display: block; /* or inline-block */
  text-overflow: ellipsis;
  word-wrap: break-word;
  overflow: hidden;
  max-height: 3.6em;
  line-height: 1.8em;
}

其中max-height: = line-height: × em <number-of-lines>


#3楼

我有一个效果很好的解决方案,但省略号使用渐变。 当您具有动态文本时,它可以工作,因此您不知道它是否足够长,需要椭圆形。 优点是您不必执行任何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,
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值