text-wrap-mode

Baseline 2024
新推出

自 2024 年 10 月以来,此功能已可在最新的设备和浏览器版本上使用。此功能可能不适用于旧设备或浏览器。

text-wrap-mode CSS 属性控制元素内部文本是否换行。不同的值提供了换行块元素内容的替代方式。它也可以使用 text-wrap 简写属性或 white-space 简写属性进行设置和重置。

注意:white-space-collapsetext-wrap-mode 属性可以使用 white-space 简写属性一起声明。

注意:此属性的名称是占位符,有待 CSS 工作组找到更好的名称。

试一试

text-wrap-mode: wrap;
text-wrap-mode: nowrap;
<section class="default-example" id="default-example">
  <div class="whole-content-wrapper">
    <p>Edit the text in the box:</p>
    <div class="transition-all" id="example-element">
      <p contenteditable="">
        Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptatem aut
        cum eum id quos est.
      </p>
    </div>
  </div>
</section>
.whole-content-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

#example-element {
  border: 1px solid #c5c5c5;
  width: 250px;
}

语法

css
/* Keyword values */
text-wrap-mode: wrap;
text-wrap-mode: nowrap;

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

此属性指定是否可以在非强制软换行处换行。可能的值:

wrap

文本在适当的字符处(例如,在英语等使用空格分隔符的语言中是空格)跨行换行,以最大程度地减少溢出。这是默认值。

nowrap

文本不跨行换行。它会溢出其包含元素,而不是换到新行。

正式定义

初始值wrap
应用于文本和块容器
继承性
计算值同指定值
动画类型离散

正式语法

text-wrap-mode = 
wrap |
nowrap

示例

换行内容

默认设置是换行内容,因此不需要 text-wrap-mode 属性。在此示例中,内容将流到下一行,以便它适合框中,最后一行比包含框长,因此溢出。

HTML

html
<div class="box">CSS IS AWESOME</div>

CSS

css
.box {
  font-family: "Arial", sans-serif;
  font-weight: bold;
  font-size: 64px;
  box-sizing: border-box;
  border: 4px solid black;
  padding: 0px 3px;
  width: 223px;
  text-wrap-mode: wrap;
}

结果

不换行内容

在此示例中,内容将流到下一行,以便它适合框中,因为内容已被明确告知不要使用 text-wrap-mode: nowrap; 换行,内容比包含框长,因此溢出。

HTML

html
<div class="box">CSS IS AWESOME</div>

CSS

css
.box {
  font-family: "Arial", sans-serif;
  font-weight: bold;
  font-size: 64px;
  box-sizing: border-box;
  border: 4px solid black;
  padding: 0px 3px;
  width: 223px;
  text-wrap-mode: nowrap;
}

结果

规范

规范
CSS 文本模块第 4 级
# text-wrap-mode

浏览器兼容性

另见