1. 排版
  2. 字体系列

Quick reference

Class
Properties
font-sansfont-family: ui-sans-serif, system-ui, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
font-seriffont-family: ui-serif, Georgia, Cambria, "Times New Roman", Times, serif;
font-monofont-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;

基本用法

¥Basic usage

设置字体系列

¥Setting the font family

你可以使用字体系列工具控制文本的字体。

¥You can control the typeface of text using the font family utilities.

font-sans

The quick brown fox jumps over the lazy dog.

font-serif

The quick brown fox jumps over the lazy dog.

font-mono

The quick brown fox jumps over the lazy dog.

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

有条件地应用

悬停、聚焦和其他状态

Tailwind lets you conditionally apply utility classes in different states using variant modifiers. For example, use hover:font-serif to only apply the font-serif utility on hover.

<p class="font-sans hover:font-serif">
  <!-- ... -->
</p>

For a complete list of all available state modifiers, check out the Hover, Focus, & Other States documentation.

断点和媒体查询

You can also use variant modifiers to target media queries like responsive breakpoints, dark mode, prefers-reduced-motion, and more. For example, use md:font-serif to apply the font-serif utility at only medium screen sizes and above.

<p class="font-sans md:font-serif">
  <!-- ... -->
</p>

To learn more, check out the documentation on Responsive Design, Dark Mode and other media query modifiers.


使用自定义值

¥Using custom values

自定义主题

¥Customizing your theme

默认情况下,Tailwind 提供三种字体系列工具:跨浏览器无衬线堆栈、跨浏览器衬线堆栈和跨浏览器等宽堆栈。你可以通过编辑 Tailwind 配置的 theme.fontFamily 部分来更改、添加或删除它们。

¥By default, Tailwind provides three font family utilities: a cross-browser sans-serif stack, a cross-browser serif stack, and a cross-browser monospaced stack. You can change, add, or remove these by editing the theme.fontFamily section of your Tailwind config.

tailwind.config.js
module.exports = {
  theme: {
    fontFamily: {
      'sans': ['ui-sans-serif', 'system-ui', ...],
      'serif': ['ui-serif', 'Georgia', ...],
      'mono': ['ui-monospace', 'SFMono-Regular', ...],
      'display': ['Oswald', ...],
      'body': ['"Open Sans"', ...],
    }
  }
}

字体系列可以指定为数组或简单的逗号分隔字符串:

¥Font families can be specified as an array or as a simple comma-delimited string:

{
  // Array format:
  'sans': ['Helvetica', 'Arial', 'sans-serif'],

  // Comma-delimited format:
  'sans': 'Helvetica, Arial, sans-serif',
}

请注意,Tailwind 不会自动为你转义字体名称。如果你使用的字体包含无效标识符,请将其用引号引起来或转义无效字符。

¥Note that Tailwind does not automatically escape font names for you. If you’re using a font that contains an invalid identifier, wrap it in quotes or escape the invalid characters.

{
  // Won't work:
  'sans': ['Exo 2', ...],

  // Add quotes:
  'sans': ['"Exo 2"', ...],

  // ...or escape the space:
  'sans': ['Exo\\ 2', ...],
}

与任何其他 Web 项目一样,请确保在 CSS 中为你使用的任何自定义字体包含必要的 @font-face@import 规则,以便浏览器为你的网站加载这些字体:

¥Like any other web project, make sure to include the necessary @font-face or @import rules in your CSS for any custom fonts you’re using so that those fonts are loaded by the browser for your site:

app.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  @font-face {
    font-family: 'Roboto';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url(/fonts/Roboto.woff2) format('woff2');
  }
}

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

¥Learn more about customizing the default theme in the theme customization documentation.

提供默认字体设置

¥Providing default font settings

在配置自定义字体时,你可以选择使用 [fontFamilies, { fontFeatureSettings, fontVariationSettings }] 形式的元组为项目中的每种字体提供默认的 font-feature-settingsfont-variation-settings

¥You can optionally provide default font-feature-settings and font-variation-settings for each font in your project using a tuple of the form [fontFamilies, { fontFeatureSettings, fontVariationSettings }] when configuring custom fonts.

tailwind.config.js
module.exports = {
  theme: {
    fontFamily: {
      sans: [
        '"Inter var", sans-serif',
        {
          fontFeatureSettings: '"cv11", "ss01"',
          fontVariationSettings: '"opsz" 32'
        },
      ],
    },
  },
}

任意值

¥Arbitrary values

If you need to use a one-off font-family value that doesn’t make sense to include in your theme, use square brackets to generate a property on the fly using any arbitrary value.

<p class="font-['Open_Sans']">
  <!-- ... -->
</p>

Learn more about arbitrary value support in the arbitrary values documentation.

自定义默认字体

¥Customizing the default font

为方便起见,预检html 元素上设置字体系列以匹配你配置的 sans 字体,因此更改项目默认字体的一种方法是在 fontFamily 配置中自定义 sans 键:

¥For convenience, Preflight sets the font family on the html element to match your configured sans font, so one way to change the default font for your project is to customize the sans key in your fontFamily configuration:

tailwind.config.js
const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  theme: {
    extend: {
      fontFamily: {
        'sans': ['"Proxima Nova"', ...defaultTheme.fontFamily.sans],
      },
    }
  }
}

你还可以通过明确设置 font-family 属性的 添加自定义基本样式 自定义项目中使用的默认字体:

¥You can also customize the default font used in your project by adding a custom base style that sets the font-family property explicitly:

main.css
@tailwind base;
@tailwind components;
@tailwind utilities;

@layer base {
  html {
    font-family: "Proxima Nova", system-ui, sans-serif;
  }
}

如果你已将字体系列工具自定义为具有不同的名称并且不希望在你的项目中有可用的 font-sans 工具,则这是最佳方法。

¥This is the best approach if you have customized your font family utilities to have different names and don’t want there to be font-sans utility available in your project.