--- title: Typography 排版 description: 面向标题、正文与行内代码的语义化排版原语。 component: typography group: 组件 --- ## 用法 ```vue ``` > 示例中的 `pnpm add @heroui/react` 沿用固定基线文案;本 Vue 项目的实际包名与安装命令以[安装指南](../guide)为准。 ## 组件结构 默认情况下,`Typography` 会将视觉上的 `type` 映射到对应的语义化元素。 ```vue ``` | `type` | 默认元素 | | ---------------------------- | ----------- | | `h1` – `h6` | `h1` – `h6` | | `body`、`body-sm`、`body-xs` | `p` | | `code` | `code` | 需要保留视觉类型但更换语义元素时,使用 `as`。它对应上游的 `render` 自定义渲染能力。 ```vue H1 视觉样式,h2 语义元素 ``` ## 示例 ### 层级 ### 原语 - `TypographyHeading` 将 `level`(1–6)映射为 `type="h1"` 至 `type="h6"`。 - `TypographyParagraph` 将 `size="base" | "sm" | "xs"` 映射为正文样式。 - `TypographyCode` 映射为行内代码样式。 - `TypographyProse` 为以常规 HTML 子节点传入的富文本内容提供排版样式。 上游以 `Typography.Heading`、`Typography.Paragraph`、`Typography.Code` 和 `Typography.Prose` 对象属性提供这些原语;Vue 版按本库约定改为独立具名导出。 ### 富文本排版 ### 渲染属性 React 的 `render` 由 Vue 的 `as` 属性替代,只更换实际渲染元素,不改变 `type` 对应的视觉样式与 `data-type`。 ## 自定义样式 ### Tailwind CSS ### 全局 CSS 若要自定义 Typography 组件类,可使用 `@layer components` 指令。 ```css @layer components { .hv-typography--h1 { @apply font-extrabold tracking-tight; } .hv-typography--code { @apply rounded-md bg-default-100 font-mono dark:bg-default-50/20; } } ``` ## 样式参考 HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。 ### CSS 类 Typography 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/typography.css)): #### 基础类 [!toc] - `.hv-typography` - 排版基础原语 - `.hv-typography-prose` - 富文本文章体容器 #### 类型类 [!toc] - `.hv-typography--h1` 至 `.hv-typography--h6` - `.hv-typography--body`、`.hv-typography--body-sm`、`.hv-typography--body-xs` - `.hv-typography--code` #### 修饰类 [!toc] - `.hv-typography--align-start`、`.hv-typography--align-center`、`.hv-typography--align-end`、`.hv-typography--align-justify` - `.hv-typography--color-default`、`.hv-typography--color-muted` - `.hv-typography--truncate` - `.hv-typography--weight-normal`、`.hv-typography--weight-medium`、`.hv-typography--weight-semibold`、`.hv-typography--weight-bold` ## API 参考 ### Typography | 属性 | 类型 | 默认值 | 说明 | | ---------- | -------------------------------------------------------------------------------------------- | -------------- | ----------------------------------------- | | `type` | `'h1' \| 'h2' \| 'h3' \| 'h4' \| 'h5' \| 'h6' \| 'body' \| 'body-sm' \| 'body-xs' \| 'code'` | `'body'` | 语义化排版样式与默认元素。 | | `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 | | `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 | | `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 | | `truncate` | `boolean` | `false` | 将文本截断为单行并显示省略号。 | | `as` | `string` | 由 `type` 决定 | 自定义实际渲染的元素;替代上游 `render`。 | | `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定)。 | | 默认插槽 | - | - | 文本内容。 | `$attrs`(`aria-*`、`data-*`、`id`、`style`、事件等)透传至实际根元素。组件不修改传入内容,也没有内部值或事件。 ### TypographyHeading | 属性 | 类型 | 默认值 | 说明 | | ---------- | ---------------------------------------------- | --------------- | ----------------------------------- | | `level` | `1 \| 2 \| 3 \| 4 \| 5 \| 6` | `1` | 映射到 `h1` – `h6` 与对应视觉样式。 | | `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 | | `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 | | `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 | | `truncate` | `boolean` | `false` | 单行截断。 | | `as` | `string` | 由 `level` 决定 | 自定义实际渲染的元素。 | | `class` | `string \| Array \| Object` | - | 附加 CSS 类。 | | 默认插槽 | - | - | 标题内容。 | ### TypographyParagraph | 属性 | 类型 | 默认值 | 说明 | | ---------- | ---------------------------------------------- | ----------- | ------------------------------------- | | `size` | `'base' \| 'sm' \| 'xs'` | `'base'` | 映射到 `body`、`body-sm`、`body-xs`。 | | `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 | | `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 | | `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 | | `truncate` | `boolean` | `false` | 单行截断。 | | `as` | `string` | `'p'` | 自定义实际渲染的元素。 | | `class` | `string \| Array \| Object` | - | 附加 CSS 类。 | | 默认插槽 | - | - | 正文内容。 | ### TypographyCode | 属性 | 类型 | 默认值 | 说明 | | ---------- | ---------------------------------------------- | ----------- | ---------------------- | | `align` | `'start' \| 'center' \| 'end' \| 'justify'` | `'start'` | 文本对齐。 | | `color` | `'default' \| 'muted'` | `'default'` | 文本颜色。 | | `weight` | `'normal' \| 'medium' \| 'semibold' \| 'bold'` | - | 字重覆盖。 | | `truncate` | `boolean` | `false` | 单行截断。 | | `as` | `string` | `'code'` | 自定义实际渲染的元素。 | | `class` | `string \| Array \| Object` | - | 附加 CSS 类。 | | 默认插槽 | - | - | 行内代码内容。 | ### TypographyProse | 属性 | 类型 | 默认值 | 说明 | | -------- | --------------------------- | ------ | ---------------------- | | `class` | `string \| Array \| Object` | - | 附加 CSS 类。 | | 默认插槽 | - | - | 常规 HTML 富文本内容。 | `TypographyProse` 固定渲染 `div`,内部标题、段落、列表、引用、代码块、链接、图片等使用基线排版规则。`$attrs` 透传至该 `div`。 ## 相关组件 [Card 卡片](./card) · [Kbd 键盘按键](./kbd)