# 文本

同时设置字号、行高和字距的文本样式。

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

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

## 类

| 类 | 属性 | 声明 |
| --- | --- | --- |
| [`text-<name>`](/cn/reference/tokens/families#family-text) | `font-size, line-height, letter-spacing` | `font-size: var(--text-<name>);` `line-height: var(--text-<name>--line-height, normal);` `letter-spacing: var(--text-<name>--letter-spacing, normal);` |

主要的 `--text-<name>` 令牌创建类；其 `--line-height` 和 `--letter-spacing` 配套令牌可选，未提供时使用 `normal`。字体族、字重和颜色仍由独立类设置。

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

## 值

每行显示一个类读取的三个值。预设字号引用对应的 font-size 令牌，表格则解析该引用以便阅读。所有预设文本样式都包含两个配套令牌。

| 类 | 变量 | 大小 | 行高 | 字距 |
| --- | --- | --- | --- | --- |
| `text-3xs` | `--text-3xs` | `var(--font-size-3xs)` | `1.5` | `0` |
| `text-2xs` | `--text-2xs` | `var(--font-size-2xs)` | `1.5` | `0` |
| `text-xs` | `--text-xs` | `var(--font-size-xs)` | `1.5` | `0` |
| `text-sm` | `--text-sm` | `var(--font-size-sm)` | `1.5` | `0` |
| `text-md` | `--text-md` | `var(--font-size-md)` | `1.6` | `0` |
| `text-lg` | `--text-lg` | `var(--font-size-lg)` | `1.5` | `0` |
| `text-xl` | `--text-xl` | `var(--font-size-xl)` | `1.4` | `-.01em` |
| `text-2xl` | `--text-2xl` | `var(--font-size-2xl)` | `1.35` | `-.015em` |
| `text-3xl` | `--text-3xl` | `var(--font-size-3xl)` | `1.25` | `-.02em` |
| `text-4xl` | `--text-4xl` | `var(--font-size-4xl)` | `1.2` | `-.025em` |
| `text-5xl` | `--text-5xl` | `var(--font-size-5xl)` | `1.15` | `-.03em` |
| `text-6xl` | `--text-6xl` | `var(--font-size-6xl)` | `1.1` | `-.035em` |
| `text-7xl` | `--text-7xl` | `var(--font-size-7xl)` | `1.05` | `-.04em` |
| `text-8xl` | `--text-8xl` | `var(--font-size-8xl)` | `1.05` | `-.04em` |
| `text-9xl` | `--text-9xl` | `var(--font-size-9xl)` | `1` | `-.045em` |
| `text-10xl` | `--text-10xl` | `var(--font-size-10xl)` | `1` | `-.05em` |

<a id="primary-and-companions"></a>

## 一个主要令牌，三个声明

`--text-card` 注册 `text-card`。这个类始终输出字号、行高和字距。如果没有 `--text-card--line-height` 或 `--text-card--letter-spacing`，对应声明就使用 `normal`，不会保留继承的值。

**文本样式始终设置三个属性**

这里只定义主要的 --text-card 令牌。两个配套声明仍然存在并使用 normal，替换任何继承的行高或字距。

- 自定义主题将 --text-card 定义为 1.5rem，未定义任何配套令牌。
- text-card 输出 font-size: var(--text-card)、line-height: var(--text-card--line-height, normal) 和 letter-spacing: var(--text-card--letter-spacing, normal)。
- 三条实线分支都代表会输出的声明。缺失的配套值使用 normal 回退值，而不是继承其他行高或字距。
- 单独的配套令牌不会注册 text-card。字体族、字重和颜色都不属于这个组合样式。

只有配套令牌不会注册文本样式。即使只想自定义行高或字距，也必须添加主要的字号令牌。

配套令牌是这个组合样式使用的值，不是另一个类名。需要独立调整时，请使用 `font-<size>`、`leading-<name>` 或 `tracking-<name>`。条件相同时，这些单属性工具类排在文本组合样式之后，可覆盖对应声明；class 属性中的顺序不会决定结果。

这个组合样式不会加载字体、选择字体族或字重、添加外边距，或决定段落宽度。如何选择并组合文本样式来建立层次，见文字排版指南。

另见 [字体](/cn/reference/tokens/font)与[行高](/cn/reference/tokens/leading)与[字距](/cn/reference/tokens/tracking)与[生成规则的顺序](/cn/reference/rules/layers).

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

## 用法

为段落应用一组文本样式，字体族、字重和颜色仍可独立设置。

```html
<p class="… text-lg">A paragraph keeps its size,…</p>
```

```css disclosure=generated-css
@layer theme{:root,:host{--font-size-lg:1.125rem;--text-lg:var(--font-size-lg);--text-lg--line-height:1.5;--text-lg--letter-spacing:0}}@layer utilities{.text-lg{font-size:var(--text-lg);line-height:var(--text-lg--line-height, normal);letter-spacing:var(--text-lg--letter-spacing, normal)}}
```

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

## 自定义

添加文本样式及其配套令牌：`--text-display` 会创建 `text-display`。

```css
@theme {
  --text-display: 3.5rem;
  --text-display--line-height: 1.1;
  --text-display--letter-spacing: -.03em;
}
```

```css disclosure=generated-css
@layer theme{:root,:host{--text-display:3.5rem;--text-display--line-height:1.1;--text-display--letter-spacing:-.03em}}@layer utilities{.text-display{font-size:var(--text-display);line-height:var(--text-display--line-height, normal);letter-spacing:var(--text-display--letter-spacing, normal)}}
```

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

## 相关参考

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