536

Okay, this is really confusing me. I have some content inside of a div like so:

<div style="background-color: green; width: 200px; height: 300px;">

Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.Thisisatest.

</div>

However, the content overflows the DIV (as expected) because the 'word' is too long.

How can I force the browser to 'break' the word where necessary to fit all of the content inside?

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361

19 Answers19

778

Use word-wrap:break-word;

It even works in IE6, which is a pleasant surprise.


word-wrap: break-word has been replaced with overflow-wrap: break-word; which works in every modern browser. IE, being a dead browser, will forever rely on the deprecated and non-standard word-wrap instead.

Existing uses of word-wrap today still work as it is an alias for overflow-wrap per the specification.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Chi
  • 22,624
  • 6
  • 36
  • 37
165

I am not sure about the browser compatibility

word-break: break-all;

Also you can use the <wbr> tag

<wbr> (word break) means: "The browser may insert a line break here, if it wishes." It the browser does not think a line break necessary nothing happens.

TylerH
  • 20,799
  • 66
  • 75
  • 101
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 33
    Be aware of that word-break: break-all; will break words in the middle if it still can fit any characters on a line with other words before, which might not be desired outcome, while word-wrap: break-word; moves words to a new line and only breaks the word if it is too long to at all fit on it's own line in the container. Example: http://jsfiddle.net/4AKhn/1/ – alexteg Sep 25 '13 at 16:31
  • 1
    break-all breaks everything. Okay to use with specific class which have long URL etc only, but not with BODY or P – Upendra May 12 '14 at 08:56
  • This is definitely perfectly (and desired) acceptable when you are trying to display something like a request URI in a log table cell, and need it to stop breaking the whole table with a giant long cell. Breaking in the middle of a 'word' is wanted ;) While the accepted answer doesn't do squat in this regard. – IncredibleHat May 06 '18 at 13:07
  • @Skelly1983 there's no such thing as IE12 – TylerH Oct 29 '19 at 13:58
  • @TylerH I am aware of this, thanks for pointing this out to me after 3 years. This is listed on w3schools browser support chart for the wbr tag, which lists the support as "Internet Explorer/Edge" and has the version number listed as 12.0, which is in fact the first version of Edge. – Skelly1983 Oct 29 '19 at 17:52
  • @Skelly1983 So, no "IE12 and above" then... just Edge. BTW, Edge's first (public) version # was 20. EdgeHTML (now abandoned for Chromium) started at 12, however. – TylerH Oct 29 '19 at 18:12
117

This could be added to the accepted answer for a 'cross-browser' solution.

Sources:

.your_element{
    -ms-word-break: break-all;
    word-break: break-all;

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

    -webkit-hyphens: auto;
       -moz-hyphens: auto;
        -ms-hyphens: auto;
            hyphens: auto;
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Milche Patern
  • 19,632
  • 6
  • 35
  • 52
  • I got it on Chrome/Opera and Safari, but still didn't got it on Firefox and IE. With this I solved Firefox, but IE is still going over the container (I have a long filename of just alpha-numeric characters). – Kamafeather Nov 05 '14 at 17:44
  • 1
    Hi Kamafeather, i am not sure, but ifitisareallylongfilename you are attemting to break, the whole context (browser,html-css code) might be helpful to help you further more. Perhaps, you could create another question with your situation. – Milche Patern Dec 03 '14 at 19:13
  • Please note that this breaks words in ungrammatical places. –  May 05 '15 at 11:25
  • @user1322720 what is an "ungrammatical" place? – TylerH Oct 29 '19 at 13:59
  • This seems to be the best answer here. Don't break words when not needed. – John T Oct 12 '20 at 05:02
36

I was just Googling the same issue, and posted my final solution HERE. It's relevant to this question too.

You can do this easily with a div by giving it the style word-wrap: break-word (and you may need to set its width, too).

div {
    word-wrap: break-word;         /* All browsers since IE 5.5+ */
    overflow-wrap: break-word;     /* Renamed property in CSS3 draft spec */
    width: 100%;
}

However, for tables, you also need to apply: table-layout: fixed. This means the columns widths are no longer fluid, but are defined based on the widths of the columns in the first row only (or via specified widths). Read more here.

Sample code:

table {
    table-layout: fixed;
    width: 100%;
}

table td {
    word-wrap: break-word;         /* All browsers since IE 5.5+ */
    overflow-wrap: break-word;     /* Renamed property in CSS3 draft spec */
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Simon East
  • 55,742
  • 17
  • 139
  • 133
  • 1
    Thank you for tables solution ;) However, I don't quite understand why break-word can't be used with auto layout. Any suggestion? – ondObno Oct 01 '14 at 09:02
  • @ondObno Yeah I'm not sure of the technical reason, but several browsers appear to behave this way. Perhaps it's because super long words mess with all the calculations required to determine the widths of each column. – Simon East Oct 01 '14 at 13:03
  • Setting `width: 100%;` was the only way to get this working for me on FF and chrome! – Putzi San Jul 06 '18 at 14:42
30

&#8203; is the HTML entity for a unicode character called the zero-width space (ZWSP) which is an invisible character which specifies a line-break opportunity. Similarly the hyphen's purpose is to specify a line-break opportunity within a word boundary.

davidcondrey
  • 34,416
  • 17
  • 114
  • 136
  • 1
    Very nice tip! Just for complementing: `​` has lower priority than a white-space (i.e. if there's one nearby, the line will break in a white-space instead). – CPHPython Sep 26 '16 at 12:24
  • Seems to be the `` unicode equivalent I was looking for – Xenos Mar 27 '19 at 13:09
25

Found that using the following worked across most major browsers (Chrome, IE, Safari iOS/OSX) except Firefox (v50.0.2) when using flexbox and relying on width: auto.

.your_element {
    word-wrap: break-word;   
    overflow-wrap: break-word;
    word-break: break-word;
}

Note: you may need to add browser vendor prefixes if you are not using an autoprefixer.

Another thing to watch out for is text using &nbsp; for spacing can cause line breaks mid-word.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Andrew
  • 5,525
  • 5
  • 36
  • 40
  • 5
    This is exactly what I needed. The other solutions didn't work with width: 100%. One thing to note: **word-break: break-word;** should be **word-break: break-all;** – jscul Oct 14 '17 at 22:30
25

Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks

.pre-wrap {
    white-space: pre-wrap;
    word-break: break-word;
}

DEMO

td {
   word-break: break-word;
   white-space: pre-wrap;
   -moz-white-space: pre-wrap;      
}

table {
    width: 100px;
    border: 1px solid black;
    display: block;
}
<table>
<tr><th>list</th>
<td>
1.longtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtextlongtext
2.breaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreaklinebreakline
</td>
</tr>
</table>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33
8

CSS word-wrap:break-word;, tested in FireFox 3.6.3

Babiker
  • 18,300
  • 28
  • 78
  • 125
8

Remove white-space: nowrap, if there is any.

Implement:

white-space: inherit;
word-break: break-word;
TylerH
  • 20,799
  • 66
  • 75
  • 101
4

I solved my problem with code below.

display: table-caption;
3

First you should identify the width of your element. E.g:

#sampleDiv{
  width: 80%;
  word-wrap:break-word;
}
<div id="sampleDiv">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>

so that when the text reaches the element width, it will be broken down into lines.

TylerH
  • 20,799
  • 66
  • 75
  • 101
3

From MDN:

The overflow-wrap CSS property specifies whether or not the browser should insert line breaks within words to prevent text from overflowing its content box.

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.

So you can use:

overflow-wrap: break-word;

Can I use?

Community
  • 1
  • 1
Shiva127
  • 2,413
  • 1
  • 23
  • 27
3

As mentioned in @davidcondrey's reply, there is not just the ZWSP, but also the SHY &#173; &shy; that can be used in very long, constructed words (think German or Dutch) that have to be broken on the spot you want it to be. Invisible, but it gives a hyphen the moment it's needed, thus keeping both word connected and line filled to the utmost.

That way the word luchthavenpolitieagent might be noted as lucht&shy;haven&shy;politie&shy;agent which gives longer parts than the syllables of the word.
Though I never read anything official about it, these soft hyphens manage to get higher priority in browsers than the official hyphens in the single words of the construct (if they have some extension for it at all).
In practice, no browser is capable of breaking such a long, constructed word by itself; on smaller screens resulting in a new line for it, or in some cases even a one-word-line (like when two of those constructed words follow up).

FYI: it's Dutch for airport police officer

3

word-break: normal seems better to use than word-break: break-word because break-word breaks initials such as EN

word-break: normal
Matoeil
  • 6,851
  • 11
  • 54
  • 77
2
.word-break {
   word-break: keep-all;
   word-wrap: break-word;
}
0

I checked in Chrome, Firefox: word-wrap: break-word, overflow-wrap: break-word did not work for me. What works is either:

    overflow-wrap: anywhere;

or

    word-break: break-all;
Roman
  • 19,236
  • 15
  • 93
  • 97
-1

You can use a grid system for this.

<div class="container" style="background-color: green; width: 200px; height: 300px;">
   <div class="row">Thisisatest.Thisisatest.Thisisatest.</div>
   <div class="row">Thisisatest.Thisisatest.Thisisatest.</div>
 </div>
Shamsul Haque
  • 359
  • 2
  • 5
-2

Do this:

<div id="sampleDiv">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>

#sampleDiv{
   overflow-wrap: break-word;
}
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
amit bende
  • 264
  • 3
  • 4
-4

just try this in our style

white-space: normal;
Benjamin Fuentes
  • 674
  • 9
  • 22
  • 1
    Duplicating answers across multiple questions is frowned-upon in *most* cases. In this case, it seems to be an incorrect answer to both questions. `normal` is the default `white-space` value. I think that adding it would have no effect on the example in the question. Please correct me if I'm mistaken. – Jeremy May 15 '15 at 00:51
  • It depends on the override made by the CSS. On my case, i had to redeclare it on "style" in my JSF component to override the default CSS style that was not "white-space: normal;" – Benjamin Fuentes May 19 '15 at 07:46