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 属性

浏览器兼容性

另见