word-wrap、word-break和white-space

1. word-wrap

CSS3屬性,允許\color{red}{長單詞}或者\color{red}{URL地址}換行到下一行。

1.1 基本屬性值

屬性值 說明
normal 只在允許的斷字點換行(瀏覽器保持默認(rèn)處理)
break-word 在長單詞或 URL 地址內(nèi)部進(jìn)行換行。

1.2 應(yīng)用示例

QQ圖片20190422161509.png
    <div class="p1 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p2 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
  </body>
  <style>
    .p {
      width: 90px;
      margin:  20px 100px;
      border: 1px solid #dedede;
      float: left;
    }

    .p1 {
      word-wrap: normal;
    }

    .p2 {
      word-wrap: break-word;
    }

1.3 其他

  1. word-wrap已經(jīng)被W3C規(guī)范為overflow-wrap了,各瀏覽器支持情況如下:


    F35035B0-6EC2-4315-8AB0-F218A597C489.png
  2. word-wrap必須在white-space屬性支持換行的前提下才能起到作用(默認(rèn)是換行的)

2. word-break

CSS3屬性,規(guī)定自動換行的處理辦法

2.1 基本屬性值

屬性值 說明
normal 使用瀏覽器默認(rèn)的換行規(guī)則。
break-all 允許在單詞內(nèi)換行。
keep-all 只能在半角空格或連字符處換行。不是別中文自動換行,所以要慎重使用。

2.2 應(yīng)用示例

F415EF48-5620-49c4-9421-81CA71F8FDD3.png
    <div class="p1 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p2 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p3 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
   .p {
      width: 90px;
      margin:  20px 60px;
      border: 1px solid #dedede;
      float: left;
    }

    .p1 {
      word-break: normal;
    }

    .p2 {
      word-break: break-all;
    }

    .p3 {
      word-break: keep-all;
    }

2.3 其他

  1. word-break必須在white-space屬性支持換行的前提下才能起到作用(默認(rèn)是換行的)

3. white-space

設(shè)置如何處理元素內(nèi)的空白

3.1 基本屬性值

屬性值 說明
normal 默認(rèn)??瞻讜粸g覽器忽略。
nowrap 文本不會換行,文本會在在同一行上繼續(xù),直到遇到 br標(biāo)簽為止。
pre 空白會被瀏覽器保留。其行為方式類似 HTML 中的 <pre> 標(biāo)簽。
pre-wrap 保留空白符序列,但是正常地進(jìn)行換行。
pre-line 合并空白符序列,但是保留換行符。
inherit 規(guī)定應(yīng)該從父元素繼承 white-space 屬性的值。

3.2 應(yīng)用示例

34D850CA-A8A4-49ed-912C-BB139CF8E9A2.png
    <div class="p1 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p2 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p3 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p4 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    <div class="p5 p">
      the only Chinese-speaking tour guide at the visitor center of the UN headquarters
      聯(lián)合國總部游客中心唯一一位說中文的導(dǎo)游。
    </div>
    .p {
      width: 90px;
      margin:  20px 100px;
      border: 1px solid #dedede;
    }

    .p1 {
      white-space: normal;
    }

    .p2 {
      white-space: nowrap;
    }

    .p3 {
      white-space: pre;
    }

    .p4 {
      white-space: pre-wrap;
    }

    .p5 {
      white-space: pre-line;
    }

3.3 其他

  1. white-space其實針對自動換行、換行、空白符進(jìn)行了設(shè)置,通過設(shè)置不同的屬性值,這三者支持會有所不同:
屬性值 換行符 自動換行 空格符
normal no yes no(合并)
nowrap no no no(合并)
pre yes no yes
pre-wrap yes yes yes
pre-line yes yes no(合并)
  1. IE瀏覽器下(包括IE8)都不支持inherit屬性,建議慎用。

總結(jié)

word-wrap(overflow-wrap): 定義過長單詞是否換行。
word-break: 定義自動換行的時候是否可以拆分單詞。
white-space: 設(shè)置如何處理自動換行、換行符、空白符。

示例源碼地址:

  1. http://www.freerf.top/study/html5/css3/word-wrap/index.html
  2. http://www.freerf.top/study/html5/css3/word-break/index.html
  3. http://www.freerf.top/study/html5/css3/white-space/index.html

參考文獻(xiàn)

https://www.quackit.com/css/css3/properties/css_overflow-wrap.cfm
https://www.cnblogs.com/dfyg-xiaoxiao/p/9640422.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容