Skip to content

Font Size 字体大小

控制元素字体大小的实用工具。

ClassProperties

基本用法

设置字体大小

使用 text-{size} 实用工具来控制元素的字体大小。

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.

html
<p class="font-sans ...">The quick brown fox ...</p>
<p class="font-serif ...">The quick brown fox ...</p>
<p class="font-mono ...">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.

html
<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>

您可以使用您的行高比例尺中定义的任何值,或者如果需要偏离设计令牌,也可以使用任意值。

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

有条件地应用

悬停、焦点和其他状态

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

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

有关所有可用状态修改器的完整列表,请查看悬停、焦点和其他状态文档。

断点和媒体查询

您还可以使用变体修饰符来针对响应式断点、深色模式、prefers-reduced-motion 等媒体查询进行定位。例如,使用 md:text-base 仅在中等屏幕尺寸及以上应用 text-base 实用程序。

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

要了解更多信息,请查看有关响应式设计深色模式其他媒体查询修饰符的文档。

使用自定义值

定制您的主题

您可以使用 tailwind.config.js 文件中的 theme.fontSize 部分配置自定义的字体大小工具集。

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',	
    }						
  }
}

有关自定义默认主题的更多信息,请参阅主题自定义文档。

提供默认行高

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

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? }] 的元组来实现这一点。

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 值,可以使用方括号来根据任意值动态生成属性。

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

有关任意值支持的更多信息,请参阅任意值文档。

Released under the MIT License.