# 字体

字体族、字号和字重，统一使用 font-* 形式。

- ID: tokens/font
- Type: tokens
- Canonical: /cn/reference/tokens/font
- Version: 2.1.8
- Source revision: 09fc2350ea80ed137ba7ca62c8e5926744b5a5dc (revision)
- Source: packages/preset/src/theme.css
- Content digest: 1c7322a99e9f90e9d24f609401c63f639e4bceb25c478327d275190e1fe1467c
- Semantic digest: 83962260d6d56817dd052d2beebf7999a75804215dac050c47b26353145c099a
- Language: zh-CN

<a id="consumers"></a>

## 类

| 类 | 属性 | 声明 |
| --- | --- | --- |
| [`font-<family>`](/cn/reference/tokens/families#family-font) | `font-family` | `font-family: var(--font-family-<family>)` |
| [`font-<size>`](/cn/reference/tokens/families#family-font) | `font-size` | `font-size: var(--font-size-<size>)` |
| [`font-<weight>`](/cn/reference/tokens/families#family-font) | `font-weight` | `font-weight: var(--font-weight-<weight>)` |

三个命名空间共享 `font-` 前缀，因此名称必须在三者之间保持唯一。同名定义出现在多个命名空间时，会报告 `AMBIGUOUS_TOKEN`，并且不生成 CSS。

<a id="values"></a>

## 值

字体族是按顺序尝试的回退列表，字号是 rem 长度，字重则是数值请求。像素换算假设根元素字号为 16px。实际呈现的字体和可用字重取决于页面加载的字体。

<a id="font-family"></a>

### 字体族

| 类 | 变量 | 值 |
| --- | --- | --- |
| `font-sans` | `--font-family-sans` | `var(--font-sans, ui-sans-serif), system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"` |
| `font-mono` | `--font-family-mono` | `var(--font-mono, ui-monospace), SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace` |
| `font-serif` | `--font-family-serif` | `var(--font-serif, ui-serif), Georgia, Cambria, "Times New Roman", Times, serif` |

<a id="font-size"></a>

### 字号

| 类 | 变量 | 值 |
| --- | --- | --- |
| `font-3xs` | `--font-size-3xs` | `.5rem` |
| `font-2xs` | `--font-size-2xs` | `.625rem` |
| `font-xs` | `--font-size-xs` | `.75rem` |
| `font-sm` | `--font-size-sm` | `.875rem` |
| `font-md` | `--font-size-md` | `1rem` |
| `font-lg` | `--font-size-lg` | `1.125rem` |
| `font-xl` | `--font-size-xl` | `1.25rem` |
| `font-2xl` | `--font-size-2xl` | `1.5rem` |
| `font-3xl` | `--font-size-3xl` | `2rem` |
| `font-4xl` | `--font-size-4xl` | `2.25rem` |
| `font-5xl` | `--font-size-5xl` | `2.5rem` |
| `font-6xl` | `--font-size-6xl` | `3rem` |
| `font-7xl` | `--font-size-7xl` | `3.75rem` |
| `font-8xl` | `--font-size-8xl` | `4.5rem` |
| `font-9xl` | `--font-size-9xl` | `6rem` |
| `font-10xl` | `--font-size-10xl` | `8rem` |

<a id="font-weight"></a>

### 字重

| 类 | 变量 | 值 |
| --- | --- | --- |
| `font-thin` | `--font-weight-thin` | `100` |
| `font-extralight` | `--font-weight-extralight` | `200` |
| `font-light` | `--font-weight-light` | `300` |
| `font-regular` | `--font-weight-regular` | `400` |
| `font-medium` | `--font-weight-medium` | `500` |
| `font-semibold` | `--font-weight-semibold` | `600` |
| `font-bold` | `--font-weight-bold` | `700` |
| `font-extrabold` | `--font-weight-extrabold` | `800` |
| `font-heavy` | `--font-weight-heavy` | `900` |

<a id="independent-properties"></a>

## 共享一个前缀，属性各自独立

`font-serif`、`font-4xl` 和 `font-semibold` 分别设置 `font-family`、`font-size` 和 `font-weight`。由于设置不同属性，因此可以组合使用，但其中任何一个都不是完整的文本样式。

**font 前缀可改变三个独立属性**

每行都从 font-sans、font-2xl 和 font-regular 开始，结果只替换指定属性对应的类。实际呈现的字体取决于可用字体。

- 基准文本使用 sans 字体族、1.5rem 大小和 400 字重，根元素字号为 16px。
- font-serif 行只改变 font-family。font-4xl 行只将 font-size 改为 2.25rem。font-semibold 行则只将 font-weight 改为 600。
- 每个结果都使用相同文本，并共享无单位的 line-height 1.5 以便阅读；这些 font 类本身不会设置 line-height。
- 示例使用实际的预设 CSS 和可用系统字体，并不表示字体族类会下载字体文件。

字号类让行高和字距由各自的声明或继承决定。如果三个度量都应来自同一组文本样式，请使用 `text-<name>`。无论哪种形式，字体族和字重都仍然独立。

字体族令牌只指定字体名称，不会下载字体。预设的 sans、mono 和 serif 字体栈，分别以可选的 `--font-sans`、`--font-mono` 和 `--font-serif` 钩子开头。加载字体和选择全站字体栈的方法，见文字排版指南。

自定义键必须在三个命名空间之间保持不同。如果 `--font-family-brand` 和 `--font-size-brand` 同时存在，`font-brand` 就会有歧义。`(font-size:var(--font-size-brand))` 这类显式原生声明可以直接指定要使用的属性与变量。

另见 [文本样式](/cn/reference/tokens/text)与[命名空间解析](/cn/reference/rules/modes#namespace-resolution)与[原生字体选择](https://www.w3.org/TR/css-fonts-4/#font-family-prop).

<a id="usage"></a>

## 用法

为标题设置较大的字号和中等字重，并为代码示例设置等宽字体族。

```html
<h3 class="… font-2xl font-medium">Release notes</h3>
<p class="… font-sm">Install the new version with <code class="… font-mono font-xs">npm i @master/css</code>.</p>
```

```css disclosure=generated-css
@layer theme{:root,:host{--font-family-mono:var(--font-mono, ui-monospace), SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;--font-weight-medium:500;--font-size-xs:.75rem;--font-size-sm:.875rem;--font-size-2xl:1.5rem}}@layer utilities{.font-mono{font-family:var(--font-family-mono)}.font-2xl{font-size:var(--font-size-2xl)}.font-sm{font-size:var(--font-size-sm)}.font-xs{font-size:var(--font-size-xs)}.font-medium{font-weight:var(--font-weight-medium)}}
```

<a id="customize"></a>

## 自定义

每个命名空间接受自己的令牌：`--font-family-display` 创建 `font-display`，`--font-size-hero` 创建 `font-hero`。

```css
@theme {
  --font-family-display: "Inter Display", var(--font-family-sans);
  --font-size-hero: 4rem;
}
```

```css disclosure=generated-css
@layer theme{:root,:host{--font-family-sans:var(--font-sans, ui-sans-serif), system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"}:root,:host{--font-family-display:"Inter Display", var(--font-family-sans);--font-size-hero:4rem}}@layer utilities{.font-display{font-family:var(--font-family-display)}.font-hero{font-size:var(--font-size-hero)}}
```

另见 [主题指令](/cn/reference/directives/theme)与[文字排版](/cn/guide/typography).

## 相关参考

- [rules/modes](/cn/reference/rules/modes.md)
- [directives/theme](/cn/reference/directives/theme.md)
