Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crop text too long inside div

Tags:

html

css

<div style="display:inline-block;width:100px;">  very long text </div> 

any way to use pure css to cut the text that is too long rather than show on next new line and only show max 100px

like image 700
cometta Avatar asked Sep 12 '10 15:09

cometta


People also ask

How do I hide long text in CSS?

If you only want to show a single line of text in a fixed width div, give white-space:nowrap a go. Together with overflow:hidden it will force your browser not to break the line and crop the view to your forced width.

How do you handle text-overflow?

To have text-overflow property used, the text element must first overflow. This can be done by preventing the element from wrapping the overflowed content to a new line, which can be done by setting white-space: nowrap . Additionally overflow must be set to hidden .

How do you shorten text in CSS?

Solution # 1: Truncate text for single line This solution works for single-line truncation. Force text to be on a single line: white-space: nowrap; Now, our text should be on the same line and should overflow from the box if it's long enough and wrapped before.


2 Answers

You can use:

overflow:hidden; 

to hide the text outside the zone.

Note that it may cut the last letter (so a part of the last letter will still be displayed). A nicer way is to display an ellipsis at the end. You can do it by using text-overflow:

overflow: hidden; white-space: nowrap; /* Don't forget this one */ text-overflow: ellipsis; 
like image 197
Arseni Mourzenko Avatar answered Sep 18 '22 19:09

Arseni Mourzenko


<div class="crop">longlong longlong longlong longlong longlong longlong </div>​ 

This is one possible approach i can think of

.crop {width:100px;overflow:hidden;height:50px;line-height:50px;}​ 

This way the long text will still wrap but will not be visible due to overflow set, and by setting line-height same as height we are making sure only one line will ever be displayed.

See demo here and nice overflow property description with interactive examples.

like image 26
Davor Lucic Avatar answered Sep 17 '22 19:09

Davor Lucic