css word-wrap: break-word won't work

前端 未结 6 1582
鱼传尺愫
鱼传尺愫 2021-02-02 17:11

I have two inline span. code sample:

    
6条回答
  •  旧时难觅i
    2021-02-02 17:55

    To properly break long-words it requires to combine several CSS properties, I would suggest defining and applying helper class like this:

    .break-long-words {
      overflow-wrap: break-word;
      word-wrap: break-word;
      word-break: break-all;
      word-break: break-word;
      hyphens: auto;
    }
    

    Properties explained:

    • overflow-wrap and word-wrap are aliases for same thing but some browsers support one and not the other so we need them both. They are offical "correct" way to break words but they don't have any effect on elements with dynamic width so we need more...
    • word-break is originally intended for requiring a particular behaviour with CJK (Chinese, Japanese, and Korean) text but works similar to word-wrap but in WebKit break-all value breaks everything and everywhere. For that reason we must use non-standard and poorly documented WebKit-only break-word value.
    • and optionally, if it makes sense for break words to have hypens (ie. for URLs it probably doesn't) we can use hypens in browsers that support them (at the moment Firefox, Safari, Edge and IE 10+). Also note that in Firefox hypens don't work if you have word-brake property so unless you have fixed-width container you must choose between hypens on FF or legacy support.

    Note that I omitted vendor prefixes but if you don't use something like Autoprefixer than this is full verison:

    .break-long-words {
      overflow-wrap: break-word;
      word-wrap: break-word;
      -ms-word-break: break-all;
      word-break: break-all;
      word-break: break-word;
      -ms-hyphens: auto;
      -moz-hyphens: auto;
      -webkit-hyphens: auto;
      hyphens: auto;
    }
    

提交回复
热议问题