6

My question:

Is there any difference between overflow-wrap: break-word and word-break: break-word?

Non-duplicates:

Here are some existing questions that may appear to be duplicates at first sight but aren't.

Code:

I wrote this code snippet that appears to show that both overflow-wrap: break-word and word-break: break-word behave the same way. But I don't know for sure if I have accounted for all cases. Maybe there is a case in which they behave differently?

.ow {
  overflow-wrap: break-word;
  background: lightgreen;
}

.wb {
  word-break: break-word;
  background: lightblue;
}

div {
  width: 5em;
  display: inline-block;
}
<div class="ow">
Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
</div>

<div class="wb">
Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
</div>

<div class="ow">
Most words are short and don't need to break. But Antidisestablishmentarianism is long.
</div>

<div class="wb">
Most words are short and don't need to break. But Antidisestablishmentarianism is long.
</div>

Browser support:

Considering the browser support matrix, it looks like overflow-wrap: break-word works with all modern browsers.

What I want to know if you can imagine any type of text or HTML that would make overflow-wrap: break-word and word-break: break-word behave differently?

Lone Learner
  • 18,088
  • 20
  • 102
  • 200
  • from the speficition : *For compatibility with legacy content, the word-break property also supports a deprecated break-word keyword. When specified, this has the same effect as word-break: normal and overflow-wrap: anywhere, regardless of the actual value of the overflow-wrap property.* (https://drafts.csswg.org/css-text-3/#word-break-property) this can probably be a good starting point – Temani Afif Jun 16 '19 at 12:28
  • This seems like a ppk question: https://www.quirksmode.org/compatibility.html . (Not saying he's answered it.) –  Jun 20 '19 at 14:04
  • @D_N What does "ppk" mean? – Lone Learner Jun 21 '19 at 03:23
  • The initials of the guy who runs quirksmode. He does a ton of testing of edge case CSS things. –  Jun 21 '19 at 06:47

3 Answers3

3

To simplify it, word-wrap is an older version of overflow-wrap and has been replaced by overflow-wrap in the current CSS3 specification. This rule allows you to tell the browser is it allowed to break words when they are too long.

On the other hand, word-break allows you to tell the browser how to break the words.

As you've noted in your question, the break-word value for both of these rules will behave the same. See these definitions from the links I provided above:

word-break: break-word:

To prevent overflow, normally unbreakable words may be broken at arbitrary points if there are no otherwise acceptable break points in the line.

overflow-wrap: break-word:

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.

2

Yes, overflow-wrap: break-word and word-break: break-word can behave differently. I've run into that, though I'm finding it hard to reproduce!

It helps to understand that at this point, word-break: break-word is really an alias for overflow-wrap: anywhere.

word-break: break-word is officially deprecated; see the CSS Text Module Level 3 Working Draft:

For compatibility with legacy content, the word-break property also supports a deprecated break-word keyword. When specified, this has the same effect as word-break: normal and overflow-wrap: anywhere, regardless of the actual value of the overflow-wrap property.

The thing to note here is that word-break: break-word is an alias for overflow-wrap: anywhere, NOT an alias for overflow-wrap: break-word.

(word-break: normal is just the default value for word-break, so you can ignore it unless you're setting a different value for word-break.)

How do overflow-wrap: anywhere and overflow-wrap: break-word differ?

Well, here's a page from the wild, using overflow-wrap: anywhere:

enter image description here

And here's the same page, using overflow-wrap: break-word:

enter image description here

The only difference in the documentation between the two is that overflow-wrap: anywhere DOES "consider soft wrap opportunities introduced by the word break" when it is "calculating min-content intrinsic sizes", while overflow-wrap: break-word does NOT.

I guess widths might be more accurate in some cases if it is considering them?

Ben Wheeler
  • 6,788
  • 2
  • 45
  • 55
1

As written in documentation, word-break: break-word behaves as word-break: normal combined with overflow-wrap: anywhere, but overwrites actual overflow-wrap value.

Documentation for overflow-wrap states that anywhere allows soft wraps (for example, on spaces), introduced by hard wraps, while word-break doesn't.

This means, that if the container has with depending on content size, anywhere will calculate width as if there are no overflowing words and then cut them to fit the container, while word-break will consider overflowing words as a reason to make the container as wide as possible.

In the example, the width is set as the longest word in the first sentence for word-break: break-word and overflow-wrap: anywhere, while overflow-wrap: break-word forces width to reach the limit, as there is a long word in the second sentence.

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

.owaw {
  overflow-wrap: anywhere;
}

.wbbw {
  word-break: break-word;
}

div {
  width: min-content;
  max-width: 150px;
  display: inline-block;
  background: lightgreen;
}
<h3>overflow-wrap: break-word</h3>
<div>
This text has no reason to take the max width. <b  class="owbw">The word AVeryLongWordWithoutAnyPlacesToBreakIt might cause it in some cases.</b>
</div><br>

<h3>overflow-wrap: anywhere</h3>
<div>
This text has no reason to take the max width. <b  class="owaw">The word AVeryLongWordWithoutAnyPlacesToBreakIt might cause it in some cases.</b>
</div><br>

<h3>word-break: break-word</h3>
<div>
This text has no reason to take the max width. <b  class="wbbw">The word AVeryLongWordWithoutAnyPlacesToBreakIt might cause it in some cases.</b>
</div>
vladko312
  • 99
  • 6