14

Is there an alternative for word-break: break-word that works in firefox ?

In firefox[Version 57.0]

enter image description here

In Chrome[Version 62.0.3202.94]

enter image description here

Is there a way or an alternative to use break-word attribute in firefox also.? [if it works in older versions it would be much better.]

Sample Code

table {
  width: 300px;
  display: inline-block;
  overflow: hidden;
  word-break: break-word;
}

.text-right {
  width: 30%;
}
<table>
  <tr>
    <td class="text-right">Sample text </td>
    <td>Sample texttexttexttexttexttexttexttext 000000000000000000000000000000</td>
  </tr>
</table>
Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69

5 Answers5

7

The valid values of word-break are as follows:

/* Keyword values */
word-break: normal; 
word-break: break-all; 
word-break: keep-all;

/* Global values */
word-break: inherit;
word-break: initial;
word-break: unset;

The word-break CSS property specifies whether or not the browser should insert line breaks wherever the text would otherwise overflow its content box.

Chrome, Safari and other WebKit/Blink browsers also support the unofficial break-word value which is treated like word-wrap: break-word.

So could you use word-wrap: break-word instead?

Yvonne Aburrow
  • 2,602
  • 1
  • 17
  • 47
  • 1
    But `word-wrap: break-word` will only break the line if there are words if its a line without a word then it will overflow the div. But `word-break: break-word` in chrome it will break the line if its overflow the div and it will also respect the word also.[ am looking for a solution like this] – Jithin Raj P R Nov 21 '17 at 13:32
  • thanks for the clarification. Could you add a code snippet in your OP? – Yvonne Aburrow Nov 21 '17 at 13:42
  • I have put a sample code for you guyz[it's a quick one], I hope you get the point. – Jithin Raj P R Nov 21 '17 at 14:07
  • I just found this on CodePen - any help? https://codepen.io/katydecorah/pen/ywmBE – Yvonne Aburrow Nov 21 '17 at 14:24
  • also this on GitHub which fixes a lot of issues: https://github.com/ftlabs/ftcolumnflow – Yvonne Aburrow Nov 21 '17 at 14:28
  • and this 2014 blogpost: https://miketaylr.com/posts/2014/01/word-break-break-word.html – Yvonne Aburrow Nov 21 '17 at 14:43
  • 1
    Thankyou for your references and you time you spend for that. But I am wondering if there any new post or hack regarding this issue. without the help of `script`. For your effort, I will give you +1, but am not looking for an answer like this sorry. – Jithin Raj P R Nov 22 '17 at 05:01
  • 2
    thanks for the upvote :) I think you will need to use some sort of polyfill script, I'm afraid. I found `overflow-wrap: break-word` but that didn't work in Firefox either :( – Yvonne Aburrow Nov 22 '17 at 10:48
7

I'm a bit late to the party, but I think I found the answer.

I've always used word-break: break-word because it's perfect in most cases, if there is enough space it wraps and keeps the words, and if there's not enough space it breaks the word. MDN Web Docs say that word-break: break-word is deprecated but will probably still works, but the real reason I started looking at an alternative is that visual studio doesn't auto-complete break-word anymore :)

Solution ?

MDN - overflow-wrap

overflow-wrap: anywhere;

Wraps the words to new line, and only breaks the word if it need to.

"The property was originally a nonstandard and unprefixed Microsoft extension called word-wrap, and was implemented by most browsers with the same name. It has since been renamed to overflow-wrap, with word-wrap being an alias."

So overflow-wrap should be used instead of word-wrap.

var s = document.querySelector('div');

function a(){
  s.removeAttribute('class');
  s.classList.add('a');

}

function b(){
  s.removeAttribute('class');
  s.classList.add('b');

}
#class{
  border: 1px solid rgb(255, 120, 120);
  width: 100px;
  margin-top: 10px;
  margin-bottom: 10px;
  border-radius: 2px;
}

btn{
  border: 1px solid gray;
  padding: 2px;
  border-radius: 5px;
  box-shadow: 0 0 2px gray;
  cursor: pointer;
}

.a{
  overflow-wrap: normal;
}

.b{
  overflow-wrap: anywhere;
}
<span>overflow-wrap:</span>
<btn onClick="a()">Normal</btn>
<btn onClick="b()">Anywhere</btn>

<div id='class'>
  We need a very long word like Supercalifragilisticexpialidocious 
</div>

<i>There's also overflow-wrap: break-word;</i>
RDU
  • 812
  • 1
  • 9
  • 22
  • 4
    This works fine everywhere but not on Safari in early 2021 https://caniuse.com/?search=overflow-wrap%3A%20anywhere%3B. Is there any easy way to "polyfill" or patch Safari, but not Chrome, Firefox or Edge? – andersnylund Mar 12 '21 at 08:04
2

According to this thread on Bugzilla, word-break: break-word now works in Firefox as of version 67. It got implemented in this changeset.

break-word does come with this note in the MDN docs:

This deprecated API should no longer be used, but will probably still work.

G-Ram
  • 33
  • 3
  • 6
  • It must have been updated. It seems to have been updated to just have a comment in the syntax indicating that break-word is deprecated. – G-Ram Apr 16 '20 at 14:31
0

To manipulate words in your case you have two CSS properties: word-break and word-wrap:

mickaelw
  • 1,453
  • 2
  • 11
  • 28
  • 1
    This I know bro. my question is there any solution for creating the `break-word` in firefox – Jithin Raj P R Nov 21 '17 at 13:35
  • @weBer the value break-word doesn't exist for the property word-break, so I think it's normal that firefox returns an error. What do you expect? – mickaelw Nov 21 '17 at 13:40
  • "Chrome, Safari and other WebKit/Blink browsers also support the unofficial `break-word` value which is treated like `word-wrap: break-word`." https://developer.mozilla.org/en-US/docs/Web/CSS/word-break – Yvonne Aburrow Nov 21 '17 at 13:43
  • @mickaelw I was just asking if there is an alternative to the same behaviour. – Jithin Raj P R Nov 21 '17 at 13:46
  • @weBer I just read your comment on yvonne answer. Can you add a sample into your question? – mickaelw Nov 21 '17 at 13:55
  • @mickaelw I have put a sample code for you guyz[it's a quick one], I hope you get the point. – Jithin Raj P R Nov 21 '17 at 14:07
0

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. source here It worked perfectly on my websites.

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33