42

Forcing the text split into multiple lines when it reaches the maximum width of the #div.

How can I do this? because, if I try to output data with 200 characters without spacing, I get the following:

Result 1 (no spacing):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

Result 2 (have one space):

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa (space)
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

Expected result:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

Do I need to use the following?

result+=str.substring(0,200)  "\n";

or it is a CSS styling?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
John Bobs
  • 489
  • 1
  • 6
  • 12

6 Answers6

81

Applying word-break: break-all; will make the text wrap at whatever character whenever it exceeds it's parent's width, without the need of a space or other type breakpoint.

Gust van de Wal
  • 5,211
  • 1
  • 24
  • 48
K. V. Suresh
  • 897
  • 7
  • 8
12

As already stated in the accepted answer use the following:

word-break:break-all;

The W3 specification that talks about these seem to suggest that word-break: break-all is for requiring a particular behaviour with CJK (Chinese, Japanese, and Korean) text, whereas word-wrap: break-word is the more general, non-CJK-aware, behaviour. (credit: AakashM: see post)

Word-Break Property options (W3Schools):

normal      Default value. Break words according to their usual rules

break-all   Lines may break between any two letters

keep-all    Breaks are prohibited between pairs of letters

initial     Sets this property to its default value. Read about initial

inherit     Inherits this property from its parent element. Read about

Working Example:

Word-Break W3Schools

Community
  • 1
  • 1
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
6

make a smaller div and use CSS text-align: justify and word-break:break-all;

Is this what you want?

animuson
  • 53,861
  • 28
  • 137
  • 147
Mohammad Masoudian
  • 3,483
  • 7
  • 27
  • 45
3

use div and set the width of a div In CSS File

    .respose container:{
width:200
}

And then in the html file add

<p div="response container">
if(string.length>limit)
{`return string.substring(0,limit)+'...';`
}
else
{`return string;`
}
</p>
Shreyash Shetty
  • 103
  • 1
  • 5
1

The word-break property can be used to control the text flow. The following value would fix your issue: word-break: break-all

More info can be found here: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
Shreyash Shetty
  • 103
  • 1
  • 5
1

The updated answer should be:

overflow-wrap:break-word;

It will break a word that by itself would not be able to fit on its own line, but leave all other words as they are (see overflow-wrap here).

You could also use:

overflow-wrap:anywhere; but this will allow line breaks after any word in an effort to reduce the width of an element. See the difference described below from MDN:

[break-word is] The same as the anywhere value, with normally unbreakable words allowed to be broken at arbitrary points if there are no otherwise acceptable break points in the line, but soft wrap opportunities introduced by the word break are NOT considered when calculating min-content intrinsic sizes.

Also, anywhere is not supported by Internet Explore, Safari, and some mobile browsers while break-word is supported on all major browsers (see [here][2]).

word-break: break-word; should no longer be used because it is deprecated in favor of the overflow-wrap:break-word;. Now, the word-break property is intended to be used when you want to break words regardless of whether they could fit on their own line (i.e. the OP's first example with word-break: break-all.

In contrast to word-break, overflow-wrap will only create a break if an entire word cannot be placed on its own line without overflowing.

(From overflow-wrap also linked above )

Nikhil Bhatia
  • 99
  • 1
  • 3