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

瀏覽器相容性

另見