What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS

前端 未结 10 1201
离开以前
离开以前 2020-11-22 07:39

I am currently wondering what is the difference between the two. When I used both they seem to break the word if it is not fitting the container. But why did W3C made two wa

10条回答
  •  失恋的感觉
    2020-11-22 07:54

    From the respective W3 specifications —which happen to be pretty unclear due to a lack of context— one can deduce the following:

    • word-break: break-all is for breaking up foreign, non-CJK (say Western) words in CJK (Chinese, Japanese or Korean) character writings.
    • word-wrap: break-word is for word breaking in a non-mixed (let us say solely Western) language.

    At least, these were W3's intentions. What actually happened was a major cock-up with browser incompatibilities as a result. Here is an excellent write-up of the various problems involved.

    The following code snippet may serve as a summary of how to achieve word wrapping using CSS in a cross browser environment:

    -ms-word-break: break-all;
     word-break: break-all;
    
     /* Non standard for webkit */
     word-break: break-word;
    
    -webkit-hyphens: auto;
       -moz-hyphens: auto;
        -ms-hyphens: auto;
            hyphens: auto;
    

提交回复
热议问题