# 字型

字型家族、大小與字重，統一使用 font-* 形式。

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

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

## 類別

| 類別 | 屬性 | 宣告 |
| --- | --- | --- |
| [`font-<family>`](/tw/reference/tokens/families#family-font) | `font-family` | `font-family: var(--font-family-<family>)` |
| [`font-<size>`](/tw/reference/tokens/families#family-font) | `font-size` | `font-size: var(--font-size-<size>)` |
| [`font-<weight>`](/tw/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 開始，結果只替換指定屬性對應的 class。實際呈現的字型取決於可用字型。

- 基準文字使用 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 class 本身不會設定 line-height。
- 範例使用真正的預設 CSS 與可用的系統字型，不表示字型家族 class 會下載字型檔。

字型大小 class 會讓行高與字距依各自的宣告或繼承決定。如果三個度量都應來自同一組文字樣式，請使用 `text-<name>`。無論哪種形式，字型家族與字重都仍然獨立。

字型家族 token 只會指定字型名稱，不會下載字型。預設的 sans、mono 與 serif 字型堆疊，分別以選用的 `--font-sans`、`--font-mono` 與 `--font-serif` 掛鉤開頭。載入字型與選擇全站字型堆疊的做法，請見文字排版指南。

自訂鍵必須在三個命名空間之間保持不同。如果 `--font-family-brand` 與 `--font-size-brand` 同時存在，`font-brand` 就會有歧義。`(font-size:var(--font-size-brand))` 這類明確的原生宣告，可以直接指定要使用的屬性與變數。

另見 [文字樣式](/tw/reference/tokens/text)與[命名空間解析](/tw/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>

## 自訂

每個命名空間接受自己的 token：`--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)}}
```

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

## 相關參考

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