CSS word-wrap 与 overflow-wrap:长文本如何换行

CSS 的 word-wrap 是 overflow-wrap 的旧别名。当长 URL 或没有空格的标识符需要换行才能放下时,可以用 overflow-wrap: anywhere。它保留正常换行点,也可能减小 Flexbox 或 Grid 为文本保留的最小宽度。

word-wrap 与 overflow-wrap 的关系

如果想将 word-wrap: break-word 改为新名称并保留行为,可以使用 overflow-wrap: break-word。下面的两条声明可以让支持旧名称但还不识别 anywhere 的浏览器使用备用规则。

.message {
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

两个名称指向同一个属性。层叠优先级相同时,后面有效的声明生效。改用 anywhere 也可能改变 min-content 宽度的计算。

CSS Text 中 overflow-wrap 及其别名的规范

三种取值与 min-content 宽度

初始值为 normal,该属性会继承,在文本允许换行的地方生效。anywhere 和 break-word 都允许在即将溢出的字符串内部应急换行。

取值 换行行为 应急换行点是否计入 min-content
normal 只用常规换行点 无应急换行点
break-word 可断开会溢出的字符串 不计入
anywhere 可断开会溢出的字符串 计入

min-content 是根据可用换行点计算出的最小内容宽度。固定宽度的段落里,break-word 和 anywhere 可能看起来一样。使用内容最小宽度的布局可以根据 anywhere 提供的新换行点进一步缩小项目。

试用长 URL 与 Flexbox 示例

将完整代码保存为 word-wrap-example.html,再用浏览器打开。上方两个盒子使用同一 URL。下方三行 Flexbox 使用同一个标识符。

<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>比较 CSS word-wrap 与 overflow-wrap</title>
  <style>
    body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; }
    main { max-inline-size: 46rem; margin-inline: auto; }
    .comparison {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(min(100%, 17rem), 1fr));
      gap: 1rem;
    }
    figure { margin: 0 0 0.75rem; min-width: 0; }
    figcaption { margin-block-end: 0.35rem; overflow-wrap: anywhere; }
    .address {
      box-sizing: border-box;
      inline-size: min(100%, 15rem);
      padding: 0.5rem;
      overflow-x: auto;
      background: #edf5fa;
    }
    .address--wrap { overflow-wrap: anywhere; }
    .viewport { overflow-x: auto; }
    .row {
      display: flex;
      box-sizing: border-box;
      inline-size: min(100%, 21rem);
      border: 1px solid #8baeca;
    }
    .label {
      flex: 0 0 2.5rem;
      padding: 0.5rem;
      background: #dbeaf4;
    }
    .value {
      flex: 1 1 0;
      padding: 0.5rem;
      background: #edf5fa;
      font-family: ui-monospace, monospace;
    }
    .old .value { word-wrap: break-word; }
    .new .value { overflow-wrap: anywhere; }
    .unlocked .value {
      overflow-wrap: break-word;
      min-inline-size: 0;
    }
  </style>
</head>
<body>
  <main>
    <h1>比较 CSS word-wrap 与 overflow-wrap</h1>
    <div class="comparison">
      <figure>
        <figcaption>overflow-wrap: normal</figcaption>
        <div class="address">https://example.com/verylongidentifierwithoutspaces123456789</div>
      </figure>
      <figure>
        <figcaption>overflow-wrap: anywhere</figcaption>
        <div class="address address--wrap">https://example.com/verylongidentifierwithoutspaces123456789</div>
      </figure>
    </div>
    <h2>比较 Flexbox 的最小宽度</h2>
    <figure>
      <figcaption>word-wrap: break-word</figcaption>
      <div class="viewport">
        <div class="row old">
          <span class="label">ID</span>
          <span class="value">BuildConfigurationIdentifierWithoutSpaces2026</span>
        </div>
      </div>
    </figure>
    <figure>
      <figcaption>overflow-wrap: anywhere</figcaption>
      <div class="viewport">
        <div class="row new">
          <span class="label">ID</span>
          <span class="value">BuildConfigurationIdentifierWithoutSpaces2026</span>
        </div>
      </div>
    </figure>
    <figure>
      <figcaption>break-word 与 min-inline-size: 0</figcaption>
      <div class="viewport">
        <div class="row unlocked">
          <span class="label">ID</span>
          <span class="value">BuildConfigurationIdentifierWithoutSpaces2026</span>
        </div>
      </div>
    </figure>
  </main>
</body>
</html>

上面的 normal 使用斜杠等已有换行点。可以在盒子内滚动,查看放不下的 URL 部分。使用 anywhere 时,过长的部分也能换行。

第一行中,标识符让右侧项目的最小宽度超出了边框。第二行用 anywhere 让它换行。第三行保留 break-word,并通过 min-inline-size: 0 让项目能够缩小。

检查 Flexbox 和 Grid 的最小尺寸

自动最小尺寸可能阻止 Flexbox 文本项目缩小。如果改变换行方式后盒子依然很宽,先找到真正的 Flexbox 项目。必要时像第三行一样,在该项目上降低最小行内尺寸。

左图的长标识符溢出 Flexbox 行。右图的项目缩小,同一标识符在项目内换行。

如果 Grid 的弹性列一直过宽,可以用 grid-template-columns: auto minmax(0, 1fr) 把该列的最小值设为 0。同时检查文本能否在所得宽度内换行。

Flexbox 自动最小尺寸规范

Grid 轨道尺寸与 minmax() 规范

排查意外溢出

  • 文本一直不换行时,检查 white-space: nowrap,并确认盒子确实有宽度限制。
  • 普通单词过于频繁地断开时,检查是否继承了 word-break: break-all。处理超长字符串的应急换行时可以用 overflow-wrap。
  • overflow-wrap 改变换行位置并保留原字符串。需要单行省略号时,可将 text-overflow 与合适的宽度及溢出设置配合。