# 文字

同時設定字型大小、行高與字距的文字樣式。

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

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

## 類別

| 類別 | 屬性 | 宣告 |
| --- | --- | --- |
| [`text-<name>`](/tw/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>` token 會建立 class；其 `--line-height` 與 `--letter-spacing` 配套 token 為選用，未提供時使用 `normal`。字型家族、字重與色彩仍由獨立 class 設定。

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

## 值

每列顯示一個 class 讀取的三個值。預設大小會參照對應的 font-size token，表格則解析參照以利閱讀。所有預設文字樣式都包含兩個配套 token。

| 類別 | 變數 | 大小 | 行高 | 字距 |
| --- | --- | --- | --- | --- |
| `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>

## 一個主要 token，三個宣告

`--text-card` 註冊 `text-card`。這個 class 一律輸出字型大小、行高與字距。如果沒有 `--text-card--line-height` 或 `--text-card--letter-spacing`，對應宣告就會使用 `normal`，不會保留繼承的值。

**文字樣式一律設定三個屬性**

這裡只定義主要的 --text-card token。兩個配套宣告仍會存在並使用 normal，取代任何繼承的行高或字距。

- 自訂主題將 --text-card 定義為 1.5rem，未定義任何配套 token。
- text-card 會輸出 font-size: var(--text-card)、line-height: var(--text-card--line-height, normal) 與 letter-spacing: var(--text-card--letter-spacing, normal)。
- 三條實線分支都代表會輸出的宣告。缺少的配套值會使用 normal 備援值，而不是繼承其他行高或字距。
- 單獨的配套 token 不會註冊 text-card。字型家族、字重與色彩都不屬於這個組合樣式。

只有配套 token 不會註冊文字樣式。即使只想自訂行高或字距，也必須加入主要的大小 token。

配套 token 是這個組合樣式使用的值，不是另一個 class 名稱。若要獨立調整，請使用 `font-<size>`、`leading-<name>` 或 `tracking-<name>`。條件相同時，這些單一屬性 utilities 會排在文字組合樣式之後，可覆寫對應的宣告；class 屬性中的順序不會決定結果。

這個組合樣式不會載入字型、選擇字型家族或字重、加入外距，或決定段落寬度。如何選擇並組合文字樣式建立層級，請見文字排版指南。

另見 [字型](/tw/reference/tokens/font)與[行高](/tw/reference/tokens/leading)與[字距](/tw/reference/tokens/tracking)與[生成規則的順序](/tw/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>

## 自訂

加入文字樣式及其配套 token：`--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)}}
```

另見 [主題指令](/tw/reference/directives/theme)與[文字排版](/tw/guide/typography).

## 相關參考

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