How to word wrap text in HTML?

HtmlCssWord Wrap

Html Problem Overview


How can text like aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa which exceeds the width of a div (say 200px) be wrapped?

I am open to any kind of solution such as CSS, jQuery, etc.

Html Solutions


Solution 1 - Html

Try this:

div {
    width: 200px;
    word-wrap: break-word;
}

Solution 2 - Html

On bootstrap 3, make sure the white-space is not set as 'nowrap'.

div {
  width: 200px;
  word-break: break-all;
  white-space: normal;
}

Solution 3 - Html

You can use a soft hyphen like so:

aaaaaaaaaaaaaaa­aaaaaaaaaaaaaaa

This will appear as

aaaaaaaaaaaaaaa-
aaaaaaaaaaaaaaa

if the containing box isn't big enough, or as

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

if it is.

Solution 4 - Html

div {
    /* Set a width for element */
    word-wrap: break-word
}

The 'word-wrap' solution only works in IE and browsers supporting CSS3.

The best cross browser solution is to use your server side language (php or whatever) to locate long strings and place inside them in regular intervals the html entity ​ This entity breaks the long words nicely, and works on all browsers.

e.g.

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa​aaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Solution 5 - Html

The only one that works across IE, Firefox, chrome, safari and opera if there are no spaces in the word (such as a long URL) is:

div{
    width: 200px;  
    word-break: break-all;
}

I found this to be bullet-proof.

Solution 6 - Html

This worked for me

word-wrap: normal;
word-break: break-all;
white-space: normal;
display: block;
height: auto;
margin: 3px auto;
line-height: 1.4;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;

Solution 7 - Html

Another option is also using:

div
{
   white-space: pre-line;
}

This will set all your div elements in all browsers that support CSS1 (which is pretty much all common browsers as far back as IE 8)

Solution 8 - Html

Cross Browser

.wrap
{
    white-space: pre-wrap; /* css-3 */    
    white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
    white-space: -pre-wrap; /* Opera 4-6 */    
    white-space: -o-pre-wrap; /* Opera 7 */    
    word-wrap: break-word; /* Internet Explorer 5.5+ */
}

Solution 9 - Html

<p style="word-wrap: break-word; word-break: break-all;">
    Adsdbjf bfsi hisfsifisfsifs shifhsifsifhis aifoweooweoweweof
</p>

Solution 10 - Html

Add this CSS to the paragraph.

width:420px; 
min-height:15px; 
height:auto!important; 
color:#666; 
padding: 1%; 
font-size: 14px; 
font-weight: normal;
word-wrap: break-word; 
text-align: left;

Solution 11 - Html

Example from http://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/">CSS Tricks:

div {
	-ms-word-break: break-all;

	/* Be VERY careful with this, breaks normal words wh_erever */
	word-break: break-all;

	/* Non standard for webkit */
	word-break: break-word;

	-webkit-hyphens: auto;
	-moz-hyphens: auto;
	hyphens: auto;
}

More examples https://kenneth.io/blog/2012/03/04/word-wrapping-hypernation-using-css/">here</a>;.

Solution 12 - Html

In HTML body try:

<table>
    <tr>
        <td>
            <div style="word-wrap: break-word; width: 800px">
                Hello world, how are you? More text here to see if it wraps after a long while of writing and it does on Firefox but I have not tested it on Chrome yet. It also works wonders if you have a medium to long paragraph. Just avoid writing in the CSS file that the words have to break-all, that's a small tip.
            </div>
        </td>
    </tr>
</table>

In CSS body try:

background-size: auto;

table-layout: fixed;

Solution 13 - Html

Try this

div{
  display: block;
  display: -webkit-box;
  height: 20px;
  margin: 3px auto;
  font-size: 14px;
  line-height: 1.4;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

the property text-overflow: ellipsis add ... and line-clamp show the number of lines.

Solution 14 - Html

I have used bootstrap. My html code looks like ..

<div class="container mt-3" style="width: 100%;">
  <div class="row">
    <div class="col-sm-12 wrap-text">
      <h6>
        text content
      </h6>
    </div>
  </div>
</div>
    

CSS

.wrap-text {
     text-align:justify;
}

Solution 15 - Html

you can use this CSS

p {
  width: min-content;
  min-width: 100%;
}

Solution 16 - Html

A server side solution that works for me is: $message = wordwrap($message, 50, "<br>", true); where $message is a string variable containing the word/chars to be broken up. 50 is the max length of any given segment, and "<br>" is the text you want to be inserted every (50) chars.

Solution 17 - Html

Try this

div {display: inline;}

Solution 18 - Html

try:

overflow-wrap: break-word;

Solution 19 - Html

Use word-wrap:break-word attribute along with required width. Mainly, put the width in pixels, not in percentages.

width: 200px;
word-wrap: break-word;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSatya KalluriView Question on Stackoverflow
Solution 1 - HtmlAlan Haggai AlaviView Answer on Stackoverflow
Solution 2 - HtmllukaseratView Answer on Stackoverflow
Solution 3 - HtmlKim StebelView Answer on Stackoverflow
Solution 4 - HtmlOrr SiloniView Answer on Stackoverflow
Solution 5 - HtmlKyle DearleView Answer on Stackoverflow
Solution 6 - HtmlAmolView Answer on Stackoverflow
Solution 7 - HtmlAndrew MaraisView Answer on Stackoverflow
Solution 8 - HtmlTimelessView Answer on Stackoverflow
Solution 9 - HtmlManoj AlwisView Answer on Stackoverflow
Solution 10 - HtmlSwapnil GodambeView Answer on Stackoverflow
Solution 11 - HtmlJuraj GunišView Answer on Stackoverflow
Solution 12 - HtmlWesson2View Answer on Stackoverflow
Solution 13 - HtmlVladimir SalgueroView Answer on Stackoverflow
Solution 14 - HtmlRahul WasnikView Answer on Stackoverflow
Solution 15 - HtmlRashid IqbalView Answer on Stackoverflow
Solution 16 - HtmldeshbanksView Answer on Stackoverflow
Solution 17 - HtmlMichael MulikitaView Answer on Stackoverflow
Solution 18 - HtmlEden SharvitView Answer on Stackoverflow
Solution 19 - HtmlaksView Answer on Stackoverflow