Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

How can I solve the problem of soft hyphens on my web pages? In text, there can be long words which I might want to line break with a hyphen, but I do not want the hyphen to show if the whole word is on the same line. I have heard of &shy; and of <wbr> .

According to comments on this page <wbr> is a non-standard "tag soup invented by Netscape". It seems like &shy; has its problems with standard compliance as well .

How can I use soft hyphens in HTML in a way that works in all browsers?

For testing browsers: jsfiddle.net/k2hbrjz8/2 Either &#173; or &shy; seems to work as desired for display and copy/paste. <wbr> only selects half of word when double clicked. Search finds all in current Firefox (as of this comment date). If &#173; indexes better, use it. karmakaze Sep 7, 2018 at 3:08 Unfortunately, it doesn't. Try searching for a word containing a soft hyphen in your browser. Most browsers treat it as two separate words, instead of symply ignoring the soft hyphen. gclj5 Oct 18, 2009 at 17:08 @gclj5 I just tested find on a &shy; word in Chrome v21, and it correctly ignores the soft hyphen. Not sure about IE, FF and other browsers though. evanrmurphy Oct 23, 2012 at 21:01 But copy text with &shy; in Chrome return text with dash. Example: "uni&shy;later&shy;al." copied as "uni-­later­-al." Enyby May 2, 2014 at 11:10 Today we had to drop &shy; usage due to specific bugs in Webkit (affecting Safari, Safari iOS and Chrome latest versions) causing weird characters rendering with a significant number of custom fonts (both free and commercial) Giulia Nicole Pernice Nov 6, 2015 at 18:24

They all perform pretty well, &#173; edges it as Google can still index of words containing it.

  • In browsers: &shy; and &#173; both display as expected in major browsers (even old IE!). <wbr> isn't supported in recent versions of IE (10 or 11) and doesn't work properly in Edge.
  • When copied and pasted from browsers: (tested 2015) as expected for &shy; and &#173; for Chrome and Firefox on Mac, on Windows (10), it keeps the characters and pastes hard hyphens into Notepad and invisible soft hyphens into applications that support them. IE (win7) always pastes with hyphens, even in IE10, and Safari (Mac) copies in a way which pastes as hyphens in some applications (e.g. MS Word), but not others
  • Find on page works for &shy; and &#173; on all browsers except IE which only matches exact copied-and-pasted matches (even up to IE11)
  • Search engines: Google matches words containing &#173; with words typed normally. As of 2017 it appears to no longer match words containing &shy; . Yandex appers to be the same. Bing and Baidu seem to not match either.
  • Test it

    For up-to-date live testing, here are some examples of unique words with soft hyphens.

  • &shy; - confumbabbl&shy;ication&shy;ism - confumbabbl­ication­ism
  • ..............................................................................................................confumbabbl­ication­ism
  • ..................................................................................................................confumbabbl­ication­ism
  • <wbr> - donfounbabbl<wbr>ication<wbr>ism . This site removes <wbr/> from output. Here's a jsbin.com snippet for testing .

  • &#173; - eonfulbabbl&#173;ication&#173;ism - eonfulbabbl­ication­ism
  • .................................................................................................................eonfulbabbl­ication­ism
  • ....................................................................................................................eonfulbabbl­ication­ism
  • Here they are with no shy hyphens (this is for copying and pasting into find-on-page testing; written in a way which won't break the search engine tests):

    ZZZconfumbabblicationismZZZdonfounbabblicationismZZZeonfulbabblicationismZZZ

    Display across browsers

    Success: displaying as a normal word, except where it should break, when it breaks and hyphenates in the specified place.

    Failure: displaying unusually, or failing to break in the intended place.

  • Chrome (40.0.2214.115, Mac): &shy; success, <wbr> success, &#173; success
  • Firefox (35.0.1, Mac): &shy; success, <wbr> success, &#173; success
  • Safari (6.1.2, Mac): &shy; success, <wbr> not tested yet , &#173; success
  • Edge (Windows 10): &shy; success, <wbr> fail (break but no hyphen), &#173; success
  • IE11 (Windows 10): &shy; success, <wbr> fail (no break), &#173; success
  • IE10 (Windows 10): &shy; success, <wbr> fail (no break), &#173; success
  • IE8 (Windows 7): erratic - sometimes, none of them work at all and they all just follow css word-wrap . Sometimes, they seem to all work. Not yet found any clear pattern as to why.
  • IE7 (Windows 7): &shy; success, <wbr> success, &#173; success
  • Copy-paste across browsers

    Success: copying and pasting the whole word, unhyphenated. (tested on Mac pasting into browser search, MS Word 2011, and Sublime Text)

    Failure: pasting with a hyphen, space, line break, or with junk characters.

  • Chrome (40.0.2214.115, Mac): &shy; success, <wbr> success, &#173; success
  • Firefox (35.0.1, Mac): &shy; success, <wbr> success, &#173; success
  • Safari (6.1.2, Mac): &shy; fail into MS Word (pastes all as hyphens), success in other applications <wbr> fail , &#173; fail into MS Word (pastes all as hyphens), success in other applications
  • IE10 (Win7): &shy; fail pastes all as hyphens, <wbr> fail , &#173; fail pastes all as hyphens
  • IE8 (Win7): &shy; fail pastes all as hyphens, <wbr> fail , &#173; fail pastes all as hyphens
  • IE7 (Win7): &shy; fail pastes all as hyphens, <wbr> fail , &#173; fail pastes all as hyphens
  • Search engine matching

    Updated in November 2017. <wbr> not tested because StackOverflow's CMS stripped it out.

    Success: searches on the whole, non-hyphenated word find this page.

    Failure: search engines only find this page on searches for the broken segments of the words, or a word with hyphens.

  • Google: &shy; fails, &#173; succeeds
  • Bing: &shy; fails, &#173; fails
  • Baidu: &shy; fails, &#173; fails (can match fragments within longer strings but not the words on their own containing a &#173; or &shy; )
  • Yandex: &shy; fails, &#173; succeeds (though it's possible it's matching a string fragment like Baidu, not 100% sure)
  • Find on page across browsers

    Success and failure as search engine matching.

  • Chrome (40.0.2214.115, Mac): &shy; success, <wbr> success, &#173; success
  • Firefox (35.0.1, Mac): &shy; success, <wbr> success, &#173; success
  • Safari (6.1.2, Mac): &shy; success, <wbr> success, &#173; success
  • IE10 (Win7): &shy; fail only matches when both contain shy hyphens, <wbr> success, &#173; fail only matches when both contain shy hyphens
  • IE8 (Win7): &shy; fail only matches when both contain shy hyphens, <wbr> success, &#173; fail only matches when both contain shy hyphens
  • IE7 (Win7): &shy; fail only matches when both contain shy hyphens, <wbr> success, &#173; fail only matches when both contain shy hyphens
  • Today, <wbr/> works in Firefox and Chrome. (And, to be honest, I suspect it did even in February, at least on Windows.) Andreas Rejbrand Apr 20, 2015 at 18:03 I am using the latest versions of Chrome and Firefox on Windows 7, and <wbr/> works in both. Please notice that the <wbr> element is not supposed to add a hyphen when a break occurs. In other words, <wbr/> and &shy; are not supposed to do the same thing . Andreas Rejbrand Apr 24, 2015 at 10:14 Ah, looking at it, it looks like SE's CMS has removed the <wbr/> s from the final markup, while keeping them in the source code. Damn you SE! Will edit.. user56reinstatemonica8 May 5, 2015 at 11:21 Is there any point in testing &shy; and &#173; ? Once they reach the DOM they are literally the same ; only in the HTML tokenizer are they at all different. gsnedders Sep 30, 2015 at 14:04 Just for further info: According to caniuse IE dropped <wbr> support since version 8: caniuse.com/#feat=wbr-element Some second testing in practice would be nice though, especially for IE 11. Dennis98 Aug 25, 2017 at 7:39

    There is an ongoing effort to standardize hyphenation in CSS3 .

    Some modern browsers, notably Safari and Firefox, already support this. Here is a good and up to date reference on browser support .

    Once the CSS hyphenation gets implemented universally, that would be the best solution. In the meantime, I can recommend Hyphenator - a JS script that figures out how to hyphenate your text in the way most appropriate for a particular browser.

    Hyphenator:

  • relies on Franklin M. Liangs hyphenation algorithm , commonly known from LaTeX and OpenOffice.
  • uses CSS3 hyphenation where it is available,
  • automatically inserts &shy; on most other browsers,
  • supports multiple languages,
  • is highly configurable,
  • gracefully falls back in case javascript is not enabled.
  • I've used it and it works great!

    The Hyphenator script seems no longer maintained, but there is a new version from the same author available here: github.com/mnater/Hyphenopoly MattFisch Jul 15, 2020 at 19:55

    I use &shy; , inserted manually where necessary.

    I always find it a pity that people don’t use techniques because there is some—maybe old or strange—browser around which doesn’t handle them the way they were specified. I found that &shy; is working properly in both recent Internet Explorer and Firefox browsers, that should be enough. You may include a browser check telling people to use something mature or continue at their own risk if they come around with some strange browser.

    Syllabification isn’t that easy and I cannot recommend leaving it to some Javascript . It’s a language specific topic and may need to be carefully revised by the deskman if you don’t want it to turn your text irritating. Some languages, such as German, form compound words and are likely to lead to decomposition problems. E.g. Spargelder ( germ. saved money, pl.) may, by syllabification rules, be wrapped in two places ( Spar-gel-der ). However, wrapping it in the second position, turns the first part to show up as Spargel- ( germ. asparagus), activating a completely misleading concept in the head of the reader and therefore shoud be avoided.

    And what about the string Wachstube ? It could either mean ‘guardroom’ ( Wach-stu-be ) or ‘tube of wax’ ( Wachs-tu-be ). You may probably find other examples in other languages as well. You should aim to provide an environment in which the deskman can be supported in creating a well-syllabified text, proof-reading every critical word.

    The common example in English of Spar-gelder vs. Spargel-der is "re-cord" vs "rec-ord" (homographs but not homophones). The former is a verb, the latter is a noun. Algorithms are usually not smart enough for this, even in English (one of the best supported languages in IT). Nicholas Shanks Nov 14, 2013 at 11:29

    It is very important to notice that, as of HTML5, <wbr> and &shy; are not supposed to do the same thing !

    Soft hyphens

    &shy; is a soft hyphen, i.e., U+00AD: SOFT HYPHEN. For example,

    innehålls&shy;förteckning
    

    might be rendered as

    innehållsförteckning
    

    or as

    innehålls-
    förteckning
    

    As of today, soft hyphens work in Firefox, Chrome, and Internet Explorer.

    The wbr element

    The wbr element is a word-break opportunity, which will not display a hyphen if a line break occurs. For example,

    ABCDEFG<wbr/>abcdefg
    

    might be rendered as

    ABCDEFGabcdefg
    

    or as

    ABCDEFG
    abcdefg
    

    As of today, this element works in Firefox and Chrome.

    The zero-width space entity can be used in place of <wbr> tag reliably on virtually every platform.

    &#8203;
    

    Also useful is the word joiner entity, that can be used to prohibit a break. (Insert between each character of a word, except where you want the break.)

    &#8288;
    

    With the two of these, you can do anything.

    This is a crossbrowser solution that I was looking at a little while ago that runs on the client and using jQuery:

    (function($) { 
      $.fn.breakWords = function() { 
        this.each(function() { 
          if(this.nodeType !== 1) { return; } 
          if(this.currentStyle && typeof this.currentStyle.wordBreak === 'string') { 
            //Lazy Function Definition Pattern, Peter's Blog 
            //From http://peter.michaux.ca/article/3556 
            this.runtimeStyle.wordBreak = 'break-all'; 
          else if(document.createTreeWalker) { 
            //Faster Trim in Javascript, Flagrant Badassery 
            //http://blog.stevenlevithan.com/archives/faster-trim-javascript 
            var trim = function(str) { 
              str = str.replace(/^\s\s*/, ''); 
              var ws = /\s/, 
              i = str.length; 
              while (ws.test(str.charAt(--i))); 
              return str.slice(0, i + 1); 
            //Lazy Function Definition Pattern, Peter's Blog 
            //From http://peter.michaux.ca/article/3556 
            //For Opera, Safari, and Firefox 
            var dWalker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false); 
            var node,s,c = String.fromCharCode('8203'); 
            while (dWalker.nextNode()) { 
              node = dWalker.currentNode; 
              //we need to trim String otherwise Firefox will display 
              //incorect text-indent with space characters 
              s = trim( node.nodeValue ).split('').join(c); 
              node.nodeValue = s; 
        return this; 
    })(jQuery); 
    

    I used soft hyphen unicode character successfully in few desktop and mobile browsers to solve the issue.

    The unicode symbol is \u00AD and is pretty easy to insert into Python unicode string like s = u'Языки и методы програм\u00ADми\u00ADро\u00ADва\u00ADния'.

    Other solution is to insert the unicode char itself, and the source string will look perfectly ordinary in editors like Sublime Text, Kate, Geany, etc (cursor will feel the invisible symbol though).

    Hex editors of in-house tools can automate this task easily.

    An easy kludge is to use rare and visible character, like ¦, which is easy to copy and paste, and replace it on soft hyphen using, e.g. frontend script in $(document).ready(...). Source code like s = u'Языки и методы про¦гра¦м¦ми¦ро¦ва¦ния'.replace('¦', u'\u00AD') is easier to read than s = u'Языки и методы про\u00ADг\u00ADра\u00ADм\u00ADми\u00ADро\u00ADва\u00ADния'.

    Keep it simple. A soft hyphen is just a character. Like A or B or 🦊. You don't need a special character to include it, you can just type it (if your computer is set up for that), or copy/paste it from elsewhere. Like here: Soft Hyphen on unicode explorer

    Of course, the character you should copy is invisible, so that makes it a little difficult I guess :) But it still works. Right-click and copy.

    You also don't need to render text containing a soft hyphen in any special way (like "DangerouslySetInnerHTML" with React). It is just a character; it works like it should in all relevant browsers worth any amount of salt.

    As an example, in the next paragraph I will write the phrase "a really long word with soft hyphens instead of whitespace that should span at least two lines ow text no matter how big your screen is". Except I'll put in soft hyphens instead of whitespace. Here goes:

    a­really­long­word­with­soft­hyphens­instead­of­whitespace­that­should­span­at­least­two­lines­of­text­no­matter­how­big­your­screen­is

    I think this question predates widespread use of Unicode in text editors. In modern times, you are correct: there is no longer any need to use ASCII and HTML entities. – miken32 Oct 10 at 21:14