---
title: "尺寸与密度"
description: "一组五级高度供控件的各个尺寸使用，任何可按下的东西都不会画得比最小尺寸更小，容器还会把尺寸传给其中的内容。"
url: "https://tanrei.chnotchy.com/zh-cn/foundations/size-and-density/"
last_updated: 2026-11-21T03:27:29.000Z
---

> 本页是 Tanrei 文档的一部分。索引 [llms.txt](https://tanrei.chnotchy.com/llms.txt)（英文）列出了所有页面；默认系统中各组件的规格汇总在一个文件 [llms-full.txt](https://tanrei.chnotchy.com/llms-full.txt) 中。

# 尺寸与密度

> 一组五级高度供控件的各个尺寸使用，任何可按下的东西都不会画得比最小尺寸更小，容器还会把尺寸传给其中的内容。

控件或行有多高，由三个值决定。**控件高度**和**最小目标**属于整个设计系统：产品在自己的层里各设一次。**尺寸**属于一个组件或一组组件：按钮在它所在的位置是 small、medium 或 large。

## 尺寸阶梯

阶梯是 `tanrei.sys.control.height.*` 这五个高度，控件的尺寸从这里读取：`medium` 就是控件高度 `tanrei.dial.control.height`（默认 40），其余各级在它下方和上方相差整数个尺寸单位（`dial.size.unit`，默认 4px）：`small` 低两个、`extra-small` 低四个，`large` 高四个、`extra-large` 高八个。下表是几个控件高度下的高度，由令牌自身的规则计算得出：

| 令牌 | dial.control.height 36px | dial.control.height 40px | dial.control.height 44px | dial.control.height 48px | dial.control.height 56px |
| --- | --- | --- | --- | --- | --- |
| `--tanrei-sys-control-height-extra-small` | 20px | 24px | 28px | 32px | 40px |
| `--tanrei-sys-control-height-small` | 28px | 32px | 36px | 40px | 48px |
| `--tanrei-sys-control-height-medium` | 36px | 40px | 44px | 48px | 56px |
| `--tanrei-sys-control-height-large` | 52px | 56px | 60px | 64px | 72px |
| `--tanrei-sys-control-height-extra-large` | 68px | 72px | 76px | 80px | 88px |

生成自 `design/tokens/tanrei.light.tokens.json`, `tokens/sys/measure.tokens.json`

许多组件的高度都读取这些级别，读取同一级的组件在任何控件高度下都一样高：Button 的 `small`、`medium` 和 `large` 分别读取 small、medium 和 large 这三级。阶梯本身没有下限，因为 `extra-small` 用于没有人会按的东西，例如标签或键盘快捷键。

## 最小目标

`tanrei.sys.target.minimum` 是任何供人按下的东西的最小绘制高度，对只有图标的控件来说也是最小宽度。每个可按下的盒子或行的高度都以它为下限推导，所以无论尺寸如何，都不会画得更低。它是开发者自己定的值：没有调节量会缩放它，因为一个随着被限制的东西一起缩小的下限，就不再是下限。

在默认系统中，最小目标为 36，鼠标和手指都一样。Button 的高度能说明它的作用：括号里的值是尺寸和控件高度给出的高度，前面的值是最小目标让按钮保持的高度。

| 令牌 | dial.control.height 36px | dial.control.height 40px | dial.control.height 44px | dial.control.height 48px | dial.control.height 56px |
| --- | --- | --- | --- | --- | --- |
| `--tanrei-comp-button-container-height-small` | 36px (28px) | 36px (32px) | 36px | 40px | 48px |
| `--tanrei-comp-button-container-height-medium` | 36px | 40px | 44px | 48px | 56px |
| `--tanrei-comp-button-container-height-large` | 52px | 56px | 60px | 64px | 72px |

生成自 `design/tokens/tanrei.light.tokens.json`, `tokens/comp/button.tokens.json`

另外两个值决定从多大的区域接收按下：手指用 `sys.target.touch`，精确指针用 `sys.target.pointer`。两者默认都等于最小目标，所以一个值就能同时设定三个。遵循某个平台指南的系统会把它们设得更大；这时，单独放置的控件会在绘制范围之外，从这个更大的区域接收按下，而与相邻元素相接的行会画到这个高度。画得比目标小的标记，例如复选框的方框或小号纸片（Chip），保持自身尺寸，把点击区域扩大到目标大小。各个内置系统的设定如下：

| 设计系统 | `minimum` | `touch` | `pointer` |
| --- | --- | --- | --- |
| `tanrei` | 36px | 36px | 36px |
| `cupertino` | 28px | 44px | 28px |
| `m3-like` | 32px | 48px | 44px |

生成自 `tokens/systems/*.system.json`, `design/tokens/<system>/tanrei.light.tokens.json`

构建会检查每个系统：三个值都不低于 24px（WCAG 2.5.8），`touch` 和 `pointer` 不低于最小目标，最小目标不高于控件高度，以及带下限的高度不低于最小目标。产品自己的层若把目标设得低于 24px，会以警告的形式告知；接收按下的区域小于最小目标，或最小目标高于控件高度，在任何系统中都会被拒绝。

## 在层中设定

两者都是层里附上理由来设定的令牌：

```json
"tokens": {
  "tanrei.dial.control.height": { "$value": { "value": 32, "unit": "px" }, "reason": "A tool used all day with a mouse: controls and rows lower." },
  "tanrei.sys.target.minimum": { "$value": { "value": 32, "unit": "px" }, "reason": "Used with a mouse; 32 is the least we draw." }
}
```

在 Web 上，高度是由调节量构成的 CSS 表达式，所以在根元素上设置 `--tanrei-dial-control-height`，无需重新构建，跟随它的所有高度就会一起变化。在 [Theme Studio](https://tanrei.chnotchy.com/zh-cn/foundations/theme-studio.md) 中，“尺寸”一行包含控件高度、尺寸单位和最小目标。

## 在容器上设置的尺寸

尺寸在组件所在的位置设置。在 Web 上，按钮组的 `size` 一旦设置，就成为其中每个没有自己尺寸的 Button 的尺寸：

```html
<tanrei-button-group size="small">
  <tanrei-button variant="outlined">Cancel</tanrei-button>
  <tanrei-button variant="filled">Publish</tanrei-button>
  <tanrei-button variant="text" size="medium">Details</tanrei-button>
</tanrei-button-group>
```

- **组件自己的尺寸优先。** 上面的第三个 Button 是 medium。
- **只有设置了的尺寸才会向下传递。** 没有 `size` 的按钮组不传递任何尺寸，所以其中每个 Button 按自己的默认尺寸 medium 绘制，或按包围该组的容器所设的尺寸绘制；容器嵌套时，由最近的、设置了尺寸的容器决定。
- **组件没有的尺寸会变成离它最近的尺寸**，按 extra-small、small、medium、large、extra-large 的阶梯顺序计算，两个同样近时取较大的一个。
- **按钮组和工具栏不会因为尺寸而改变自己绘制的任何东西。** 它们的间距、圆角和分隔线保持其变体的样子，围绕着按实际高度绘制的各项。

按容器给的尺寸绘制的组件，和按自己的尺寸绘制时一样，跟随尺寸阶梯和最小目标。改变容器的尺寸，或把组件移入、移出容器，都会立即按新的尺寸绘制。

### 哪些组件传递尺寸，哪些接收尺寸

有三种容器会把设在自己身上的尺寸传给其中的内容：

- [按钮组](https://tanrei.chnotchy.com/zh-cn/components/button-group.md)传给其中的 Button。
- [工具栏](https://tanrei.chnotchy.com/zh-cn/components/toolbar.md)传给其中的 Button 和带菜单的按钮，不论它们是否在分组里，也传给溢出按钮。
- [列表](https://tanrei.chnotchy.com/zh-cn/components/list.md)传给其中的列表行。

下面这些组件在没有设置自己的尺寸时，取上述容器中最近的那个的尺寸；没有容器设置尺寸时按 medium 绘制：[Button](https://tanrei.chnotchy.com/zh-cn/components/button.md)、[带菜单的按钮](https://tanrei.chnotchy.com/zh-cn/components/button-with-menu.md)、[复制按钮](https://tanrei.chnotchy.com/zh-cn/components/copy-button.md)和[列表行](https://tanrei.chnotchy.com/zh-cn/components/list-row.md)；还有按这个尺寸绘制自己所有行或部件的[列表](https://tanrei.chnotchy.com/zh-cn/components/list.md)、[树视图](https://tanrei.chnotchy.com/zh-cn/components/tree-view.md)、[折叠面板](https://tanrei.chnotchy.com/zh-cn/components/accordion.md)、[目录](https://tanrei.chnotchy.com/zh-cn/components/table-of-contents.md)、[描述列表](https://tanrei.chnotchy.com/zh-cn/components/description-list.md)和[表格](https://tanrei.chnotchy.com/zh-cn/components/table.md)。

尺寸只传到这里为止。折叠面板、描述列表和表格不会把尺寸传给你放在其中的内容：表格单元格里的 Button 保持自己的尺寸，或取表格外层容器的尺寸。还有一些组件完全不从容器接收尺寸：

- [菜单](https://tanrei.chnotchy.com/zh-cn/components/menu.md)（包括上下文菜单和[菜单栏](https://tanrei.chnotchy.com/zh-cn/components/menubar.md)中的菜单）、[搜索](https://tanrei.chnotchy.com/zh-cn/components/search.md)的视图，以及[下拉选择框](https://tanrei.chnotchy.com/zh-cn/components/select.md)和[组合框](https://tanrei.chnotchy.com/zh-cn/components/combobox.md)的列表。它们都在页面上方打开，与周围的控件分开，所以把工具栏设为 small，并不会让其中 Button 打开的菜单变小。菜单和搜索在没有设置自己的 `size` 时是 medium；下拉选择框和组合框的选项跟随其输入框的尺寸。
- 工具栏中的下拉选择框、数字输入框、文本输入框和滑块，各自保持你给它的尺寸。
- [时间线](https://tanrei.chnotchy.com/zh-cn/components/timeline.md)，它的尺寸指的是文字的大小。
