1. 排版
  2. 字体大小

Quick reference

Class
Properties
text-xsfont-size: 0.75rem; /* 12px */ line-height: 1rem; /* 16px */
text-smfont-size: 0.875rem; /* 14px */ line-height: 1.25rem; /* 20px */
text-basefont-size: 1rem; /* 16px */ line-height: 1.5rem; /* 24px */
text-lgfont-size: 1.125rem; /* 18px */ line-height: 1.75rem; /* 28px */
text-xlfont-size: 1.25rem; /* 20px */ line-height: 1.75rem; /* 28px */
text-2xlfont-size: 1.5rem; /* 24px */ line-height: 2rem; /* 32px */
text-3xlfont-size: 1.875rem; /* 30px */ line-height: 2.25rem; /* 36px */
text-4xlfont-size: 2.25rem; /* 36px */ line-height: 2.5rem; /* 40px */
text-5xlfont-size: 3rem; /* 48px */ line-height: 1;
text-6xlfont-size: 3.75rem; /* 60px */ line-height: 1;
text-7xlfont-size: 4.5rem; /* 72px */ line-height: 1;
text-8xlfont-size: 6rem; /* 96px */ line-height: 1;
text-9xlfont-size: 8rem; /* 128px */ line-height: 1;

基础用法

设置字体大小

使用 text-* 实用程序来控制元素的字体大小。

text-sm

The quick brown fox jumps over the lazy dog.

text-base

The quick brown fox jumps over the lazy dog.

text-lg

The quick brown fox jumps over the lazy dog.

text-xl

The quick brown fox jumps over the lazy dog.

text-2xl

The quick brown fox jumps over the lazy dog.

<p class="text-sm ...">The quick brown fox ...</p>
<p class="text-base ...">The quick brown fox ...</p>
<p class="text-lg ...">The quick brown fox ...</p>
<p class="text-xl ...">The quick brown fox ...</p>
<p class="text-2xl ...">The quick brown fox ...</p>

设置行高

通过向任何字体大小实用程序添加行高修饰符,可以在设置字体大小的同时设置元素的行高。例如,使用 text-xl/8 可将字体大小设置为 1.25rem,行高设置为 2rem

text-base/6

So I started to walk into the water. I won't lie to you boys, I was terrified. But I pressed on, and as I made my way past the breakers a strange calm came over me. I don't know if it was divine intervention or the kinship of all living things but I tell you Jerry at that moment, I was a marine biologist.

text-base/7

So I started to walk into the water. I won't lie to you boys, I was terrified. But I pressed on, and as I made my way past the breakers a strange calm came over me. I don't know if it was divine intervention or the kinship of all living things but I tell you Jerry at that moment, I was a marine biologist.

text-base/loose

So I started to walk into the water. I won't lie to you boys, I was terrified. But I pressed on, and as I made my way past the breakers a strange calm came over me. I don't know if it was divine intervention or the kinship of all living things but I tell you Jerry at that moment, I was a marine biologist.

<p class="text-base/6 ...">So I started to walk into the water...</p>
<p class="text-base/7 ...">So I started to walk into the water...</p>
<p class="text-base/loose ...">So I started to walk into the water...</p>

您可以使用 line-height scale 中定义的任何值,或者如果您需要偏离设计令牌,则可以使用任意值。

<p class="text-sm/[17px] ..."></p>

条件应用

悬停、聚焦和其他状态

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

<p class="text-sm hover:text-base">
  <!-- ... -->
</p>

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

断点和媒体查询

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

<p class="text-sm md:text-base">
  <!-- ... -->
</p>

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


使用自定义值

定制你的主题

您可以使用 tailwind.config.js 文件的 theme.fontSize 部分配置您自己的自定义字体大小实用程序集。

tailwind.config.js
module.exports = {
  theme: {
    fontSize: {
      sm: '0.8rem',
      base: '1rem',
      xl: '1.25rem',
      '2xl': '1.563rem',
      '3xl': '1.953rem',
      '4xl': '2.441rem',
      '5xl': '3.052rem',
    }
  }
}

theme customization 文档中了解有关自定义默认主题的更多信息。

提供默认行高

Tailwind 的默认主题为每个 font-size 实用程序配置了一个合理的默认 line-height。您可以在使用自定义字体大小时配置自己的默认行高,方法是在 tailwind.config.js 文件中使用 [fontSize, lineHeight] 形式的元组定义每个尺寸。

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    fontSize: {
      sm: ['14px', '20px'],
      base: ['16px', '24px'],
      lg: ['20px', '28px'],
      xl: ['24px', '32px'],
    }
  }
}

您还可以使用对象语法指定默认行高,这样您还可以提供默认的 letter-spacingfont-weight 值。您可以使用 [fontSize, { lineHeight?, letterSpacing?, fontWeight? }] 形式的元组来执行此操作。

tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
  theme: {
    fontSize: {
      '2xl': ['1.5rem', {
        lineHeight: '2rem',
        letterSpacing: '-0.01em',
        fontWeight: '500',
      }],
      '3xl': ['1.875rem', {
        lineHeight: '2.25rem',
        letterSpacing: '-0.02em',
        fontWeight: '700',
      }],
    }
  }
}

任意值

如果您需要使用一次性的 font-size值,而该值没有必要包含在主题中,请使用方括号动态生成属性,使用任意值。

<p class="text-[14px]">
  <!-- ... -->
</p>

进一步了解,请参考 任意值 文档.