1. 排版
  2. 文本换行

Quick reference

Class
Properties
text-wraptext-wrap: wrap;
text-nowraptext-wrap: nowrap;
text-balancetext-wrap: balance;
text-prettytext-wrap: pretty;

基础用法

换行

使用 text-wrap 将溢出的文本在文本中的逻辑点处换行到多行。

Beloved Manhattan soup stand closes

New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.

<article class="text-wrap ...">
  <h3>Beloved Manhattan soup stand closes</h3>
  <p>New Yorkers are facing the winter chill...</p>
</article>

不换行

使用 text-nowrap 来防止文本换行,必要时允许其溢出。

Beloved Manhattan soup stand closes

New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.

<article class="text-nowrap ...">
  <h3>Beloved Manhattan soup stand closes</h3>
  <p>New Yorkers are facing the winter chill...</p>
</article>

平衡

使用 text-balance 将文本均匀分布在每一行。

Beloved Manhattan soup stand closes

New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.

<article>
  <h3 class="text-balance ...">Beloved Manhattan soup stand closes</h3>
  <p>New Yorkers are facing the winter chill...</p>
</article>

出于性能原因,浏览器将文本平衡限制至约 6 行或更少的块,使其最适合标题。

漂亮的

使用 text-pretty 来防止文本块末尾出现孤儿词(单个单词占据一行)。

Beloved Manhattan soup stand closes

New Yorkers are facing the winter chill with less warmth this year as the city's most revered soup stand unexpectedly shutters, following a series of events that have left the community puzzled.

<article class="text-pretty ...">
  <h3>Beloved Manhattan soup stand closes</h3>
  <p>New Yorkers are facing the winter chill...</p>
</article>

条件应用

悬停、聚焦和其他状态

Tailwind 允许您使用变体修饰符有条件地在不同状态下应用实用程序类。例如,使用 hover:text-balance 来仅 在 hover应用 text-balance.

<h1 class="text-wrap hover:text-balance">
  <!-- ... -->
</h1>

详细了解, 请参考 Hover, Focus, & Other States 文档.

断点和媒体查询

您还可以使用变体修饰符来定位媒体查询,例如响应式断点、暗模式、首选减少运动等。例如, 使用 md:text-balance 来应用 text-balance 程序,适用于中等屏幕尺寸及以上。

<h1 class="text-wrap md:text-balance">
  <!-- ... -->
</h1>

进一步了解,请参考 响应式设计, 暗黑模式 媒体查询.