overflow-wrap

Baseline 已廣泛支援

此特性已成熟穩定,適用於多種裝置和瀏覽器版本。自 2018 年 10 月起,它已在各瀏覽器中可用。

overflow-wrap CSS 屬性適用於文字,用於設定瀏覽器是否應在不可中斷的字串內部插入換行符,以防止文字溢位其行框。

注意: 該屬性最初是一個非標準的、無字首的 Microsoft 擴充套件,名為 word-wrap,並被大多數瀏覽器以相同的名稱實現。後來它被更名為 overflow-wrap,而 word-wrap 則作為其別名。

試一試

overflow-wrap: normal;
overflow-wrap: anywhere;
overflow-wrap: break-word;
<section class="default-example" id="default-example">
  <div class="example-container">
    Most words are short &amp; don't need to break. But
    <strong class="transition-all" id="example-element"
      >Antidisestablishmentarianism</strong
    >
    is long. The width is set to min-content, with a max-width of 11em.
  </div>
</section>
.example-container {
  background-color: rgb(255 0 200 / 0.2);
  border: 3px solid rebeccapurple;
  padding: 0.75em;
  width: min-content;
  max-width: 11em;
  height: 200px;
}

注意:word-break 不同,overflow-wrap 僅在整個單詞無法單獨放置在一行中而不溢位時才會建立換行。

語法

css
/* Keyword values */
overflow-wrap: normal;
overflow-wrap: break-word;
overflow-wrap: anywhere;

/* Global values */
overflow-wrap: inherit;
overflow-wrap: initial;
overflow-wrap: revert;
overflow-wrap: revert-layer;
overflow-wrap: unset;

overflow-wrap 屬性被指定為從下面值列表中選擇的單個關鍵字。

normal

行只能在正常的單詞斷點處(例如兩個單詞之間的空格)斷開。

anywhere

為了防止溢位,一個原本不可中斷的字元字串(例如長單詞或 URL)可以在任何點處斷開,如果行中沒有其他可接受的斷點。斷點處不插入連字元。計算最小內容固有尺寸時,會考慮由單詞斷裂引入的軟換行機會。

break-word

anywhere 值相同,允許原本不可中斷的單詞在行中沒有其他可接受的斷點時在任意點斷開,但計算最小內容固有尺寸時,不考慮由單詞斷裂引入的軟換行機會。

正式定義

初始值normal
應用於文字元素
繼承性
計算值同指定值
動畫型別離散

正式語法

overflow-wrap = 
normal |
break-word |
anywhere

示例

比較 overflow-wrap、word-break 和 hyphens

此示例比較了 overflow-wrapword-breakhyphens 在拆分長單詞時的結果。

HTML

html
<p>
  They say the fishing is excellent at Lake
  <em class="normal">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
  I've never been there myself. (<code>normal</code>)
</p>
<p>
  They say the fishing is excellent at Lake
  <em class="ow-anywhere">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
  though I've never been there myself. (<code>overflow-wrap: anywhere</code>)
</p>
<p>
  They say the fishing is excellent at Lake
  <em class="ow-break-word">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
  though I've never been there myself. (<code>overflow-wrap: break-word</code>)
</p>
<p>
  They say the fishing is excellent at Lake
  <em class="word-break">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
  though I've never been there myself. (<code>word-break</code>)
</p>
<p>
  They say the fishing is excellent at Lake
  <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
  I've never been there myself. (<code>hyphens</code>, without
  <code>lang</code> attribute)
</p>
<p lang="en">
  They say the fishing is excellent at Lake
  <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
  I've never been there myself. (<code>hyphens</code>, English rules)
</p>
<p class="hyphens" lang="de">
  They say the fishing is excellent at Lake
  <em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
  I've never been there myself. (<code>hyphens</code>, German rules)
</p>

CSS

css
p {
  width: 13em;
  margin: 2px;
  background: gold;
}

.ow-anywhere {
  overflow-wrap: anywhere;
}

.ow-break-word {
  overflow-wrap: break-word;
}

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

.hyphens {
  hyphens: auto;
}

結果

規範

規範
CSS Text Module Level 3
# overflow-wrap 屬性

瀏覽器相容性

另見