What is the difference between overflow-wrap: break-word and word-break: break-word?
As you see from the following example, there is no visual difference between option-1 and option-2. (You need to uncomment either one.)
body {
width: 300px;
}
.dont-break-out {
/* Option 1 */
/* overflow-wrap: break-word; */
/* Option 2 */
/* word-break: break-word; */
}
<p class="dont-break-out" lang="en-US">For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.</p>
<p class="dont-break-out" lang="en-US">According to Wikipedia, The longest word in any of the major English language dictionary is pneumonoultramicroscopicsilicovolcanoconiosis, a word that refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano; medically, it is the same as silicosis.</p>
Not dupes:
Difference between overflow-wrap and word-break? - The accepted answer is just few small quotes from MDN and there are no examples. To be honest, I'm not sure that the person who posted it really understands the difference himself.
Do `overflow-wrap: break-word` and `word-break: break-word` ever behave differently? - Again, no example, and so it is very hard to understand what is really assumed.
Please, could you provide an example to show a difference between them?
And yes, I know that word-wrap is an alias to overflow-wrap. My question is not about it.
edit
An interesting remark by Louis Lazaris on CSS Tricks:
overflow-wrap and word-break behave very similarly and can be used to solve similar problems. A basic summary of the difference, as explained in the CSS specification is:
overflow-wrap is generally used to avoid problems with long strings causing broken layouts due to text flowing outside a container.
word-break specifies soft wrap opportunities between letters commonly associated with languages like Chinese, Japanese, and Korean (CJK).
After describing examples of how word-break can be used in CJK content, the spec says: "To enable additional break opportunities only in the case of overflow, see overflow-wrap".
From this, we can surmise that word-break is best used with non-English content that requires specific word-breaking rules, and that might be interspersed with English content, while overflow-wrap should be used to avoid broken layouts due to long strings, regardless of the language used.
But Louis haven't provided any examples. I performed the same test as above with the following text from the work-break page by MDN:
Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu 次の単語グレートブリテンおよび北アイルランド連合王国で本当に大きな言葉
... and there is still no difference between overflow-wrap: break-word and word-break: break-word.
The difference lies in the way min content intrinsic sizes are calculated. Soft wrap opportunities introduced by the break are taken into account for word-break:break-word but not for overflow-wrap:break-word. So for an example of the difference between them, we need to choose something that sizes according the min content intrinsic size, such as a float:
body {
width:160px;
}
p {
float:left;
border:1px solid;
}
.overflow-wrap {
overflow-wrap: break-word;
}
.word-break {
word-break: break-word;
}
<p>A popular long word in Engish is
<i class="overflow-wrap">antidisestablishmentarianism</i>,
although longer more contrived words exist</p>
<p>A popular long word in Engish is
<i class="word-break">antidisestablishmentarianism</i>,
although longer more contrived words exist</p>
word-break:break-word has the same effect as overflow-wrap:anywhere.
Looks like overflow-wrap provides more opportunities for the text to wrap.
I modified your code to show both cases, one after the other, for easier comparison.
Edit: good catch on the missing { - after fixing that I agree there appears to be no difference.
I'll leave this answer here as it is still a good code sample for testing alternatives.
body {
width: 300px;
}
.break-1 {
overflow-wrap: break-word;
}
.break-2 {
word-break: break-word;
}
<p class="break-1" lang="en-US">For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.</p>
<p class="break-2" lang="en-US">For more information, please visit: http://csstricks.com/thisisanonexistentandreallylongurltoaddtoanytextinfactwhyareyoustillreadingit.</p>
<p class="break-1" lang="en-US">According to Wikipedia, The longest word in any of the major English language dictionary is pneumonoultramicroscopicsilicovolcanoconiosis, a word that refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano; medically, it is the same as silicosis.</p>
<p class="break-2" lang="en-US">According to Wikipedia, The longest word in any of the major English language dictionary is pneumonoultramicroscopicsilicovolcanoconiosis, a word that refers to a lung disease contracted from the inhalation of very fine silica particles, specifically from a volcano; medically, it is the same as silicosis.</p>
<p class="break-1">Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu 次の単語グレートブリテンおよび北アイルランド連合王国で本当に大きな言葉</p>
<p class="break-2">Honorificabilitudinitatibus califragilisticexpialidocious Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu 次の単語グレートブリテンおよび北アイルランド連合王国で本当に大きな言葉</p>
OK, it seems I have found the answer, but I'm not really sure.
As per a comment at GitHub, it seems that overflow-wrap: break-word is now catches all cases of word-break: break-word, and so the latter one is redundant except if used for a backward compatibility.
Note that the behavior of this feature—allowing breaks anywhere if the word cannot fit its container (per word-wrap/overflow-wrap: break-word) and also having these break opportunities affect the min-content size—is now specified for word-wrap/overflow-wrap: break-word. See #2682
This means that once implementations catch up, there is no use case requiring word-break: break-word to be added (since overflow-wrap: break-word now has this behavior), and the only reason to add this second syntax would be if it's needed for Web-compat.
Related
Can anyone explain why the following snippet won't correctly hyphenate the word "Sustainability"? I tested this in Google Chrome, FireFox and Safari, none of them hyphenates the word, but all of them correctly hyphenate the word "hyphenated".
My observations so far:
If the word is not capitalized, hyphenation starts working
If I change the lang attribute from en to de (german), it also starts working, although the word is an english word
:root {
font-size: 20px;
font-family: system-ui;
padding: 2rem;
}
body {
margin: 0;
}
p {
border: 1px solid;
width: 100px;
hyphens: auto;
}
<html lang="en">
<body>
<p>The word Sustainability can't be hyphenated, apparently</p>
<p>Only if written without a capital "S", it starts working: sustainability 🤔</p>
</body>
</html>
It's defined in the specs: https://www.w3.org/TR/css-text-3/#hyphenation
"The UA may use language-tailored heuristics to exclude certain words from automatic hyphenation. For example, a UA might try to avoid hyphenation in proper nouns by excluding words matching certain capitalization and punctuation patterns. Such heuristics are not defined by this specification. (Note that such heuristics will need to vary by language: English and German, for example, have very different capitalization conventions.)"
This must be an intended behavior due to language rules. At this point, there aren't many hyphenation control mechanisms and browsers are free to decide how to do the hyphenation when hyphens: auto.
As a workaround, you could use soft wrap character like U+00AD or ­ HTML entity.
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.
What is the difference between "word-break: break-all" versus "word-wrap: break-word" in CSS (That question is about word-break: break-all but my question here is about word-break: break-word)
Difference between overflow-wrap and word-break? (That question asks about overflow-wrap and word-break attributes but my question here is about the break-word value for this attributes in particular. Also, that question is mysteriously marked as a duplicate of the previous question even though it is unrelated.)
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:
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap#Browser_compatibility shows that overflow-wrap: break-word is supported since Chrome 23, Edge 18, Firefox 49, Safari 6.1. It isn't supported in IE. (I am ignoring the non-standard name word-wrap here. I care only about the standard name.)
https://developer.mozilla.org/en-US/docs/Web/CSS/word-break#Browser_compatibility shows that word-break: break-word is supported since Chrome 1, Firefox 67, Safari 3. It isn't supported in IE and Edge.
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?
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.
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:
And here's the same page, using overflow-wrap: break-word:
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?
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>
I'm using the hyphens property of CSS: https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens Mozillas documentation says:
Suggested line break opportunities, as covered in Suggesting line break opportunities, should be preferred over automatically selecting break points whenever possible.
I thought that means the browser places hyphens as needed unless there is something like a . If that's the case the will be prioritised.
But I have this code:
.box {
width: 130px;
border: 1px solid red;
margin-bottom: 5px;
}
.box > a {
text-transform: uppercase;
font-size: 12px;
-moz-hyphens: auto;
-webkit-hyphens: auto;
-ms-hyphens: auto;
hyphens: auto;
}
<div class="box">
subdermatoglyphic
</div>
<div class="box">
subdermatoglyphic
</div>
Now I would assume the word breaks after "subdermat". But it doesn't. It breaks after "subdermato". At least in Firefox 33. This result may be different in other browsers. I know that this is "experimental technology" but maybe I'm missing something. Is something wrong with my implementation?
There is nothing wrong with your code from the HTML and CSS perspective. Whether the (preferred) hyphenation is correct is of course a different issue.
But Firefox does not actually implement the logic that the MDN page describes. The page just echoes an old version of the CSS3 Text draft. The current editor’s draft puts things even much stronger (and rather questionably), for hyphens: auto (the default): “Automatic hyphenation opportunities within a word must be ignored if the word contains a conditional hyphen ( or U+00AD), in favor of the conditional hyphen(s). However, if, even after breaking at such opportunities, a portion of that word is is still too long to fit on one line, an automatic hyphenation opportunity may be used.”
What Firefox actually does, as far as we can judge from its behavior, is that it treats as suggesting a hyphenation opportunity, equal to the opportunities it has determined on the basis of its hyphenation algorithms, if used (this depends on the declared language and on installed support to various languages in the browser).
This means that when used together with automatic hyphenation, is not (currently) useful in suggesting preferred hyphenation points (i.e. points to be preferred over others that might have been determined by a browser’s hyphenator). However, it is useful for suggesting hyphenation points that would not otherwise be used.
If you actually wanted to have “subdermatoglyphic” hyphenated so that hyphenation is possible between “t” and “o” but not between “o” and “g” (where most authorities allow hyphenation), you could write subdermat<span style="white-space: nowrap">og</span>lyphic. But it’s probably not worth it.
My client has requested to enable auto-hyphenation on this page: http://carlosdinizart.com/biography/ , and I realized I've never actually seen it done on a web-page.
Is it possible to set up auto-hyphenation in an HTML document with just HTML/CSS? If not - what are the options?
CSS3 provides some support for this. Source: http://drublic.de/blog/css3-auto-hyphenation-for-text-elements/
You can check the w3c documentation here: http://www.w3.org/TR/2011/WD-css3-text-20110901/#hyphenation
CSS3 adds six properties to the list of useful thing. These are:
The most important one is hyphens.
You can add dictionary-files with hyphenate-resource so the browser has a better chance to render your text with the right hyphenation.
hyphenate-before sets a minimum number of characters before the hyphenation.
hyphenate-after does the same as hyphenate-before but for characters after the hyphenation.
hyphenate-lines defines about how many lines a hyphenated word is written at a maximum.
with hyphenate-character you can specify which HTML-entity should be used, e.g. \2010.
The main property of this stack is hyphens. It accepts one of three values: none, manual or auto. The default one is manual, where you can set hyphens via . auto it the better one for continuous text while words get split if possible and available. And none does not hyphenate at all even if there is a character set for a possible line break in a certain word.
Update:
Browser support information here: http://caniuse.com/css-hyphens
An option is to insert soft hyphens into the text in places where it may be broken. The soft hyphen is represented by the entity in HTML. You may find libraries/tools that can prepare text automatically with s in the right places, otherwise you'll have to do it manually.
To deal with one page that has fixed width for text, the practical move would be to add a couple of SOFT HYPHEN characters (U+00AD), using the entity reference if you find it more comfortable than entering the (invisible) character itself. You can rather quickly find out which words need to be hyphenated to produce a good result.
In a more complex case (several pages, flexible width), use a preprocessor, or server-side code, or client-side code that adds soft hyphens. The client-side approach is simplest and can be applied independently of server-side technologies and authoring tools. Beware that automatic hyphenation may go wrong and needs some help: the language(s) of the text need to be indicated in markup (or otherwise, depending on the library used).
At the minimum, you could just put the attributes lang=en class=hyphenate into the <body> tag and the following code in the head part:
<script
src="http://hyphenator.googlecode.com/svn/tags/Version%204.0.0/Hyphenator.js">
</script>
<script>Hyphenator.run();</script>
Demo: http://bytelevelbooks.com/code/javascript/hyphenation.html (flexible-width text, with just maximum width set, so you can test it varying the browser window width).
At present my css for <p> is
p {
font-style: normal;
padding: 0;
margin-top: 0;
margin-left: 0px ;
margin-right: .5em ;
margin-bottom: 0;
text-indent: 1em;
text-align: justify;
-webkit-hyphens: auto;
-moz-hyphens: auto;
-ms-hyphens: auto;
-o-hyphens: auto;
word-break:break-word;
hyphens: auto;
}
This doesn't work for Chrome 39 on Mac. Known not to work on Opera.
Works for Firefox, iOS Safari.
This is NOT foolproof: Narrow columns (under 6 words) are ugly, but overall it makes the layout look far more like properly set type.
I have an HTML table 360px wide, which works great. The challenge is that sometimes a url appears http://this/is/a/really-really-really-really-really/long/url in the text. This causes the table to expand horizontally and a scroll bar appears on the bottom.
I don't think overflow:hidden will work because half of the text would be hidden.
What is the best way to force breaking the line on slashes (/) and dashes (-) in CSS (hopefully)?
It should work with IE7+, Chrome, Firefox and Safari.
Working in Rails 3 and jQuery.
tl;dr; (edited Apr 2022)
Use <wbr> word-break-opportunity element before each /. See first link in further reading below.
Details (original from 2014)
While the css word-wrap: break-word; does work, its implementation is different across browsers.
If you have control of the content and want exact breakpoints, you can insert
a <wbr> word break (supported in all major browsers except IE8 CanIUse.com);
zero-width space (U+200B) - ugly in IE<=6
soft hyphen - though of course this adds a hyphen when breaking which is not always what is desired.
I have a large corporate user base who still have to use IE8, so when I hit this problem I used the C# someString.Replace("/", "/") in the server-side code.
Gotcha: If you insert a zero-width space in an email address, when a user copies and pastes into their email client, the space gets copied too and the email will fail with no way for a user to see why (the space is zero width ...)
References
Stack Overflow
http://www.quirksmode.org/oddsandends/wbr.html - with examples
Further Reading
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr#example
https://developer.mozilla.org/en-US/docs/Web/CSS/word-break
https://developer.mozilla.org/en-US/docs/Web/CSS/overflow-wrap
https://kenneth.io/blog/2012/03/04/word-wrapping-hypernation-using-css/ (March 2012)
https://css-tricks.com/almanac/properties/w/word-break/ (Sep 2012)
https://css-tricks.com/almanac/properties/h/hyphenate/ (Sep 2011)
You can use word-wrap : break-word; like so:
div {
width : 50px;
border : 1px solid #000;
word-wrap : break-word;
}
<div>http://www.aaa.com/bbb/ccc/ddd/eee/fff/ggg</div>
I tested this in: I.E. 6/7/8, Firefox 7, Opera 11, Safari 5, Chrome 15
Here is a jsfiddle: https://jsfiddle.net/p4SxG/
If you don’t really care where the breaks happen, the simplest method is to add the style overflow-wrap: break-word;. This will allow long words to break without affecting the breaking of other words.
But if you want to break at specific characters, such as the slash character, you can’t do that with CSS, you have to do it in HTML. If you have access to the HTML code you can choose any of these solutions:
<wbr> word break opportunity tag
zero width space
​ zero width space
But you don’t always have access to the HTML code. Some web applications won’t allow you to enter code into certain fields; for example, WordPress will filter out any code you enter into a post title. In these situations you may be able to insert a zero-width-space character directly. One way to do this is to use Character Viewer (Mac) or Character Map (Windows), although of course they are a bit tricky to use when it comes to spaces because spaces are invisible. In the case of Character Viewer, when you search for arrow, lots of matches appear, but when you search for zero width space, it appears that no characters were found. But if you click where the blue square is in the second image below, you’ll discover that the character was found, it’s just invisible.
A snippet to demonstrate that these various methods all work:
h1 {
width: 15rem;
border: 1px solid black;
}
.b {
overflow-wrap: break-word;
}
A title which is too long
<h1>Seminars/Workshops</h1>
Breaking with CSS
<h1 class="b">Seminars/Workshops</h1>
Breaking with HTML: code-based solutions
<h1>Seminars<wbr>/<wbr>Workshops</h1>
<h1>Seminars/Workshops</h1>
<h1>Seminars​/​Workshops</h1>
Breaking with HTML: character-based solution
<h1>Seminars/Workshops</h1>