Difference between overflow-wrap and word-break?

后端 未结 3 1532
后悔当初
后悔当初 2020-11-28 23:50

What´s the exact difference between overflow-wrap/word-wrap and word-break? And can anybody tell me what´s the better one for breaking

3条回答
  •  一个人的身影
    2020-11-29 00:14

    Here are the exact differences: (based on testing in Chrome v81, and confirming my observations by referencing the spec)

    white-space

    normal (default): collapses whitespace-chains and line-breaks; adds line-breaks where needed
    nowrap: collapses whitespace-chains and line-breaks; doesn't add line-breaks
    pre-line: collapses whitespace-chains; adds line-breaks where needed
    pre-wrap: no collapsing; adds line-breaks where needed
    break-spaces: same as pre-wrap, except with spaces able to trigger line-break-adding
    pre: no collapsing; doesn't add line-breaks

    Note: If the selected white-space value lists "doesn't add line-breaks", the line-break behavior of the following properties is unable to be applied (ie. ignored).

    word-break

    normal (default): breaks line at end of last word fitting within container [if one exists], else line left unbroken
    break-word: breaks line at end of last word fitting within container [if one exists], else at end of container
    break-all: breaks line at end of container [can split a word, even with nearby whitespace]

    overflow-wrap (legacy name: word-wrap)

    normal (default): breaks line at end of last word fitting within container [if one exists], else line left unbroken
    break-word: breaks line at end of last word fitting within container [if one exists], else at end of container [if in non-flex container], else line left unbroken
    anywhere: breaks line at end of last word fitting within container [if one exists], else at end of container [so same as word-break: break-word]

    Note that for overflow-wrap: break-word (as for any combination that leaves lines too long for the container), the unbroken line can cause a flex container to expand beyond the flex ratio specified (forcing other flex containers to shrink to account for the too-long content).

提交回复
热议问题