1

Hi i am shortening long strings using ellipsis and putting dotes(...) at end. The problem is if a string contains space then i want to break string first then if it overflows then put dotes(...). E.g. I have two strings

Choreographers  Farm Houses

If i don't use ellipsis then on resizing window Choreographers overlaps Farm Houses...

When i use ellipsis then on resizing window i get

Choreogra...    Farm Ho...

What i want is

Choreogra...      Farm
                  Houses

And in case of more smaller screen

Chore...         Farm
                 Hous...

Fiddle: https://jsfiddle.net/7fpt4m5e/3/

Ashish
  • 303
  • 5
  • 22

3 Answers3

5

Remove the white-space: nowrap ... though text-overflow has bad browser support for multiline

Updated fiddle

.block2 p {
  overflow:hidden;
  text-overflow:ellipsis;
  width:98%;
}

Check this post, applying-an-ellipsis-to-multiline-text, there you will find many more ways how to

Community
  • 1
  • 1
Asons
  • 84,923
  • 12
  • 110
  • 165
0

Unfortunately, text-overflow: ellipsis will only truncate single lines.

However what you can do here is to use line-clamp which should work on webkit browsers & Firefox.

Here you mention the number of lines you want after which truncation to happen in -webkit-line-clamp & -moz-line-clamp.

Example of a line-clamp usage is:

div {
  max-height: 42px;
  width: 40px;
  margin-bottom: 10px;
  font-size: 16px;
  overflow: hidden;
  color: #000;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  -moz-line-clamp: 2;
  -moz-box-orient: vertical;
}
<div>the quick brown fox jumps over the lazy dog</div>
<div>the quick brown fox jumps over the lazy dog</div>

Otherwise, you can go the handle-your-own-pseudo-selectors-with-content-("...") way too.

nashcheez
  • 5,067
  • 1
  • 27
  • 53
-1

Instead of writing "Farm Houses" you should write "Farm&nbsp;Houses", this way it does the effect you want to achieve. This solution also answers to the bad support in browsers of white-space: nowrap or text-overflow

https://jsfiddle.net/7fpt4m5e/6/

Works on all browsers as it doesn't need special support.