javascript - Multiline Text Truncating - Stack Overflow

Does anyone know of a clean and easy way to truncate a section of text to two lines. This is what I am

Does anyone know of a clean and easy way to truncate a section of text to two lines. This is what I am trying to truncate (p element):

<blockquote><p>&#8220;<?=$test_entry ?>&#8221;</p></blockquote><cite><?=$test_name ?></cite>

I have tried various solutions including clamp.js (which doesnt even work properly in the example supplied) I have tried CSS3 solutions using text-overflow but these only truncate to one line. All other javascript examples use string length which breaks if the text size changes, if special chars are encoded and under many other conditions.

I am happy to change the markup if necessary I just want to be able to truncate text to fit two lines within a div box and have it end with an elipses (...) or even better with (..."). I thought this would be straightforward but apparantly not.

Can anyone point me to an existing solution or give me a clue where to start with this. Any help much appreciated.

Does anyone know of a clean and easy way to truncate a section of text to two lines. This is what I am trying to truncate (p element):

<blockquote><p>&#8220;<?=$test_entry ?>&#8221;</p></blockquote><cite><?=$test_name ?></cite>

I have tried various solutions including clamp.js (which doesnt even work properly in the example supplied) I have tried CSS3 solutions using text-overflow but these only truncate to one line. All other javascript examples use string length which breaks if the text size changes, if special chars are encoded and under many other conditions.

I am happy to change the markup if necessary I just want to be able to truncate text to fit two lines within a div box and have it end with an elipses (...) or even better with (..."). I thought this would be straightforward but apparantly not.

Can anyone point me to an existing solution or give me a clue where to start with this. Any help much appreciated.

Share Improve this question asked Feb 11, 2012 at 19:40 SwiftDSwiftD 6,0896 gold badges46 silver badges71 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

Here you can find an existing solution based on jQuery, which should be working cross browser, in this SO question.

This is a very simple solution to truncate the multiline paragraph and this will also add the ellipsis dots at the end of the last line.

p.truncate_text {
    color:red;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce quis justo nibh. Phasellus ac ante enim. Mauris ullamcorper mauris nec laoreet lacinia. Sed interdum tincidunt tempor. Cras euismod, tellus id consequat varius, est lectus sagittis justo, sit amet egestas sem dolor sit amet arcu. In modo nibh ac nisi tincidunt tincidunt. Nullam suscipit, mi nec posuere imperdiet, odio est pellentesque arcu, at varius massa ex nec urna.</p>

<p class="truncate_text">This paragraph will truncate into 3 lines Quisque ut purus nunc. Aenean vitae lectus maximus, fermentum justo quis, lobortis neque. Curabitur lacinia in massa in efficitur. Praesent et dolor et ex sodales volutpat vel lobortis ligula. Maecenas convallis luctus tempus. Vivamus non libero nisi. Donec facilisis, odio sed dictum consectetur, eros dolor pellentesque quam, ut ornare est ligula eu ligula. Etiam elementum nibh lectus, quis sodales massa malesuada vitae. Etiam finibus quam eget condimentum rutrum. Donec et velit ipsum. </p>

<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce quis justo nibh. Phasellus ac ante enim. Mauris ullamcorper mauris nec laoreet lacinia. Sed interdum tincidunt tempor. Cras euismod, tellus id consequat varius, est lectus sagittis justo, sit amet egestas sem dolor sit amet arcu. In modo nibh ac nisi tincidunt tincidunt. Nullam suscipit, mi nec posuere imperdiet, odio est pellentesque arcu, at varius massa ex nec urna.</p>

Use the textarea element to limit text rows!

<textarea rows="2" readonly disabled>Try enameling the ricotta broccolis with aged champaign and rum, warmed. </textarea>

You could then use a pseudo element :after to add an ellipsis or a 'Read more'.

Don't forget the attributes 'readonly' and 'disabled'. Readonly prevents textarea from being interacted with by the user. Disabled prevents any forms in the page from submitting the values.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745673370a4639540.html

相关推荐

  • javascript - Multiline Text Truncating - Stack Overflow

    Does anyone know of a clean and easy way to truncate a section of text to two lines. This is what I am

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信