2016-05-13 60 views
3

在特定行上截斷文本(或行夾)的最佳方式是什麼?在行號N上截斷文本CSS

可以說我有一段8行文字,但我只想顯示3?

這是可能通過CSS或我需要別的東西?

+1

檢查此[博客] (http://hackingui.com/front-end/a-pure-c ss解決方案爲多行文本截斷/)和[codepen示例](https://codepen.io/martinwolf/pen/qlFdp) – Venugopal

+0

好貼@Venugopal,似乎正是我一直在尋找! – jdawg

回答

1

設置line-height,並且max-height多重的n行你想展示。例如,如果line-height是30像素,僅顯示2行:

HTML

<p class="text"> 
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum 
</p> 

CSS

.text { 
    line-height: 30px; 
    max-height: 60px; 
    overflow: hidden; 
} 

https://jsfiddle.net/rk8y0rsd/

+0

太簡單了!打的好!解決問題!謝謝 – jdawg

0

是的,它很簡單,看下面的例子。

HTML:

<div class="i-speak-too-much"> 
    Hello I like speak, I live in the future I am the Universe 
</div> 

CSS:

.i-speak-too-much { 
    width: 100px; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; //making dots 
    } 

的jsfiddle:https://jsfiddle.net/ot9excbr/

+0

是的,這適用於1行,但想象如果你的文本是7行,你想顯示3 .. 文本溢出:省略號打破第一行,是否有可能將其改爲第n行? – jdawg