简体   繁体   English

CSS溢出文本显示在几行中而没有断字

[英]Css overflow text displayed in few lines without word break

I have some long text that is displayed within a div this div has a fixed width and height. 我在div中显示了一些长文本,该div具有固定的宽度和高度。 I want the text to be displayed on a few lines (as the div height) and that the sentence words will not break (word prefix in one line and the continue in the next line) furthermore I want to add ellipsis at the end of the last sentence. 我希望文本显示在几行上(作为div高度),并且句子中的单词不会中断(一行中的单词前缀,然后在下一行中继续),而且我想在该行的末尾添加省略号最后一句话。

CSS: CSS:

white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;

but this displays the text in one line even though the div contains a few lines. 但这会在一行中显示文本,即使div包含几行也是如此。

Second option: 第二种选择:

word-wrap: break-word;

this unlike the first break the word and the sentence is displayed in a few lines - this is no good since the words are breaking in the middle and at the end of the sentence there is no ellipsis (...) 这与单词的第一个断点不同,句子在几行中显示-这是不好的,因为单词在中间断了,在句子的末尾没有省略号(...)

How can i achieve the following: 我如何实现以下目标:

1) Few sentences as the div height 1)几句话作为div高度

2) No word break 2)没有断字

3) At the end ellipsis is displayed ( last line contains ellipsis) 3)在末尾显示省略号(最后一行包含省略号)

attached jsfiddle: https://jsfiddle.net/vghup5jf/1/ as you can see only one line is displayed 附加的jsfiddle: https ://jsfiddle.net/vghup5jf/1/,您可以看到仅显示了一行

ellipsis wont work for multiple line and its not possible with pure CSS. ellipsis不适用于多行,纯CSS不可能。 Below is the trick which works on chrome and Safari ( -webkit rendering engines). 以下是适用于chrome和Safari( -webkit渲染引擎)的技巧。

 .ellipsis { display: block; display: -webkit-box; max-width: 400px; max-height: 100px; font-size: 20px; line-height: 1.4; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; } 
 <div class="ellipsis"> <span> I have a long text that display in div this div has a fixed width and height, I want the text will be displayed in a few lines (as the div height) and that the sentence words will not break(word prefix in one line and the continue in the next line) furthermore I want to add ellipsis at the end of the last sentence. </span> </div> 

for cross compatibility you should use SASS or javascript. 为了实现交叉兼容性,您应该使用SASS或javascript。

Using JS 使用JS

https://css-tricks.com/line-clampin/ https://css-tricks.com/line-clampin/

Using CSS-SASS 使用CSS-SASS

http://codepen.io/martinwolf/pen/qlFdp http://codepen.io/martinwolf/pen/qlFdp

Thanks a lot BaTmaN this is the final result : 非常感谢BaTmaN,这是最终结果:

Added the following css will solve it: 添加以下css将解决该问题:

display: -webkit-box !important; -webkit-line-clamp: 2 !important;
   -webkit-box-orient: vertical !important;

and remove: white-space: nowrap; 并删除:空白:nowrap;

http://jsfiddle.net/f5n1wrxs/ http://jsfiddle.net/f5n1wrxs/

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

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