简体   繁体   English

文本溢出省略号不适用于多行文本框

[英]Text-Overflow Ellipsis not working for multi-line text boxes

Why is it so hard to have this: 为什么这么难:

  • Fixed height DIV with multiple lines of text 固定高度DIV带有多行文字
  • If text is too long for this box, show "..." at the end 如果此框的文字太长,请在结尾处显示“...”
  • Do not cut words! 别切词!

The following JSFiddle shows a demo. 以下JSFiddle显示了一个演示。

Questions : 问题

  • How can I make the "..." also appear in Firefox, IE, Edge and Safari? 如何使“...”也出现在Firefox,IE,Edge和Safari中? It only works in Chrome at this moment (see .chrome css) in JSFiddle 它目前仅适用于Chrome(请参阅.chrome css)
  • How can I make sure that only spaces get cut but no words? 我怎样才能确保只有空格被剪切但没有文字?
  • How can this be done with CSS only? 如何只用CSS完成?

Example : 示例

  • The second box cuts the word abcdefghijk, I want it to cut after the second word and then add the "..." 第二个方框切断了abcdefghijk这个词,我希望它在第二个单词之后剪切,然后添加“...”

在此输入图像描述

 .truncate { border-width: 1px; border-style: solid; border-color: #aaaaaa; padding: 10px; margin-bottom: 10px; width: 260px; overflow: hidden; display: block; box-sizing: border-box; -moz-box-sizing: border-box; } .truncate.ellipsis { height: 50px; text-overflow: ellipsis; } .truncate.ellipsis.chrome { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; } 
 <div class="truncate"> No truncating at all, height adjusts to text: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk </div> <div class="truncate ellipsis"> Truncating at 50px height: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk </div> <div class="truncate ellipsis chrome"> Truncating at 50px height: abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk abcdefghijk </div> 

There is currently no cross browser way to do this. 目前没有跨浏览器方式来执行此操作。 That said, there are a few approches you could try: 也就是说,您可以尝试一些方法:

  • Estimate the number of characters that fit in your DIV, truncate the text to the proper length, and add your ellipsis at the end 估计适合DIV的字符数,将文本截断为适当的长度,并在末尾添加省略号
  • You could use the :after pseudo element and to absolutely position it on the bottom right corner of your DIV 您可以使用:after伪元素并将其绝对放置在DIV的右下角

Example of that last one: 最后一个示例:

HTML: HTML:

<div class="ellipsis-div">
  <p>Long text Cras justo odio, dapibus ac facilisis in, egestas eget quam. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.<p>
</div>

CSS: CSS:

.ellipsis-div {
    position: relative;
    width: 150px;
    height: 150px;
    overflow: hidden;
}

.ellipsis-div:after {
    position: absolute;
    display: block;
    content: "...";
    bottom: 0;
    right: 0;
    background-color: white;
}

You will need to tweak the position of the :after element depending on your DIV's line-height, etc... 您将需要调整:after元素的位置,具体取决于您的DIV的线高等...

show ... (ellipsis) for Chrome, Firefox, IE etc. show ...(省略号)适用于Chrome,Firefox,IE等

.truncate {
  width:240px;
  overflow:hidden;
  text-overflow:ellipsis;
  white-space:nowrap;
}

see this link: https://jsfiddle.net/n2Lvnqmc/3/ 看到这个链接: https//jsfiddle.net/n2Lvnqmc/3/

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

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