How to Wrap a Long String Without any Whitespace Character

You can wrap a long string, which does not have any whitespace character by using the CSS word-wrap property, or overflow-wrap, if you use CSS3.

In this snippet, you’ll find some examples for block elements, as well as for the inline ones.

Solutions with the CSS word-wrap property

In the example below, we set the word-wrap property to its "break-word" value for the <textarea> element and specified the width.

Example of wrapping a long string without a whitespace character for block elements:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      textarea {
        width: 100px;
        word-wrap: break-word;
      }
    </style>
  </head>
  <body>
    <textarea>
      LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremI
    </textarea>
  </body>
</html>

Example of wrapping a long string without a whitespace character for inline elements:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      span {
        width: 100px;
        word-wrap: break-word;
        display: inline-block;
      }
    </style>
  </head>
  <body>
    <span>
      LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
    </span>
  </body>
</html>

Here too, we specified the word-wrap and width properties, but also set the display property to “inline-block”.

Solution with the CSS overflow-wrap property

In CSS3, the word-wrap property is renamed to overflow-warp. So, consider this when choosing the solution.

Example of wrapping a long string without a whitespace character with the CSS overflow-wrap property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      span {
        display:inline-block;
        width: 150px;
        overflow-wrap: break-word;
      }
    </style>
  </head>
  <body>
    <span>
      LoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsumLoremIpsum
    </span>
  </body>
</html>

Do you find this helpful?

Related articles