---
title: "字体优化"
description: "了解如何使用 Nuxt Fonts 优化 Nuxt 应用中的 Web 字体。"
canonical_url: "https://nuxt.zhcndoc.com/docs/4.x/getting-started/fonts"
---
# 字体优化

> 了解如何使用 Nuxt Fonts 优化 Nuxt 应用中的 Web 字体。

Nuxt 提供了一个核心模块（[`@nuxt/fonts`](https://nuxt.zhcndoc.com/modules/fonts)），用于优化 Web 字体并移除外部网络请求，从而提升隐私保护和性能：

- **自托管：** Nuxt Fonts 会在构建时下载字体，并通过你自己的域名和长期缓存标头提供字体。浏览器不会为这些字体向 Google 或其他字体提供商发送请求。请移除用于从提供商加载字体的 `<link>` 标签或 `@import` 规则，因为 Nuxt Fonts 不会处理它们。
- **减少布局偏移：** Nuxt Fonts 会调整本地系统字体，使其度量指标与 Web 字体匹配。Web 字体加载时，文本几乎不会移动。
- **无需编写代码：** 在 CSS 的 `font-family` 声明中指定字体名称，Nuxt Fonts 会自动查找字体并生成 `@font-face` 规则。

## 安装

将 Nuxt Fonts 模块添加到项目中：

```bash [Terminal]
npx nuxt add fonts
```

This command installs `@nuxt/fonts` and adds it to the `modules` of your `nuxt.config`.

## 用法

若要使用字体，请在 CSS 中添加 `font-family` 声明：

```vue [app/app.vue]
<template>
  <h1>Hello Nuxt Fonts!</h1>
</template>

<style scoped>
h1 {
  font-family: 'Roboto', sans-serif;
}
</style>
```

Nuxt Fonts 会检测到 `Roboto` 并从字体提供商解析该字体，然后将 `@font-face` 规则注入同一个样式表。

字体会在 CSS 使用它们的位置加载。若要将字体应用于整个应用，请在全局样式表中声明字体，并将该样式表添加到 `nuxt.config` 的 [`css` 属性](https://nuxt.zhcndoc.com/docs/4.x/getting-started/styling#the-css-property)中：

::code-group
```css [app/assets/css/main.css]
body {
  font-family: 'Inter', sans-serif;
}
```

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/fonts'],
  css: ['~/assets/css/main.css'],
})
```
::

Nuxt Fonts 只会解析 `font-family` 列表中的第一个字体。它会使用列表中的其他字体（例如 `sans-serif`）生成后备字体度量指标。

::warning
Nuxt Fonts 只会处理 CSS 样式表和 `<style>` 块，不会检测内联 `style` 属性中的 `font-family`。请参阅[未检测到的字体](#fonts-that-arent-detected)。
::

## Web 字体 {#web-fonts}

Nuxt Fonts 可以从多个 Web 字体提供商解析字体，例如 [Google Fonts](https://fonts.google.com)、[Bunny Fonts](https://fonts.bunny.net)、[Fontshare](https://www.fontshare.com) 和 [Fontsource](https://fontsource.org)。你无需进行配置，Nuxt Fonts 会使用第一个提供该字体的服务商。

默认情况下，Nuxt Fonts 会加载 `normal` 和 `italic` 样式下 `400 700` 的字重范围。如果字体系列提供[可变字体](https://fonts.google.com/knowledge/introducing_type/introducing_variable_fonts)，Google Fonts 和 Fontsource 会使用可变字体。一个可变字体文件即可覆盖一系列字重，无需每个字重使用一个文件。

若要加载其他字重或为字体选择提供商，请在 `nuxt.config` 的 `fonts.families` 选项中配置字体：

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/fonts'],
  fonts: {
    families: [
      // Load every weight of the variable Inter font
      { name: 'Inter', weights: ['100 900'] },
      // Only load these weights of Roboto, from Google Fonts
      { name: 'Roboto', weights: [400, 700], provider: 'google' },
    ],
  },
})
```

若要为所有字体使用相同选项，请改为在 `fonts.defaults` 中设置。你也可以更改 Nuxt Fonts 加载的字符子集，例如支持西里尔文或阿拉伯文。所有选项请参阅[字体选项](https://nuxt.zhcndoc.com/modules/fonts/configuration#font-options)。

## 本地字体 {#local-fonts}

若要使用自己的字体文件，请将它们存放在 [`public/`](https://nuxt.zhcndoc.com/docs/4.x/directory-structure/public) 目录中。Nuxt Fonts 会扫描该目录，查找与 CSS 中 `font-family` 名称匹配的 `woff2`、`woff`、`ttf`、`otf` 和 `eot` 文件。

文件名会告知 Nuxt Fonts 每个文件对应的字重和样式。文件名未标明字重或样式时，默认使用 `normal` 样式下的 `400` 字重：

```bash [Directory structure]
-| public/
---| fonts/
-----| my-font.woff2              # 400, normal
-----| my-font-bold.woff2         # 700, normal
-----| my-font-700-italic.woff2   # 700, italic
```

然后即可通过字体名称使用它：

```css [app/assets/css/main.css]
body {
  font-family: 'My Font', sans-serif;
}
```

Nuxt Fonts 会将 `My Font` 转换为 `my-font`，以查找对应文件。

对于可变字体，请在文件名中标明字重范围，例如 `public/fonts/my-variable-font-100-900.woff2`。文件名包含 `variable` 或 `VF` 但未指定范围时，默认覆盖 `100 900`。即使使用可变字体文件，Nuxt Fonts 也只会声明[默认字重](#web-fonts) `400 700`，因此其他字重会以该范围内最近的字重显示。若要使用完整字重范围，请配置字体字重：

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/fonts'],
  fonts: {
    families: [
      { name: 'My Variable Font', weights: ['100 900'] },
    ],
  },
})
```

然后即可通过字体名称使用它：

```css [app/assets/css/main.css]
h1 {
  font-family: 'My Variable Font', sans-serif;
  font-weight: 900;
}
```

若要使用名称不同的字体文件或托管在其他位置的文件，请在 `fonts.families` 中设置其 `src`。你可以重复指定字体名称，为每种字重添加一个文件：

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/fonts'],
  fonts: {
    families: [
      { name: 'Brand Font', src: '/brand-regular.woff2', weight: 400 },
      { name: 'Brand Font', src: '/brand-black.woff2', weight: 900 },
    ],
  },
})
```

## 未检测到的字体 {#fonts-that-arent-detected}

Nuxt Fonts 只会解析在 CSS 中找到的字体。它不会检测内联样式、内联 SVG、第三方样式表中的字体，也不会检测仅在运行时应用的字体。

对于这些字体，请在 `fonts.families` 中设置 `global: true`。Nuxt Fonts 随后会将该字体的 `@font-face` 规则添加到每个页面：

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@nuxt/fonts'],
  fonts: {
    families: [
      { name: 'Anton', global: true },
    ],
  },
})
```

每位访客都会下载每种全局字体对应的 CSS，因此请仅在确有需要时使用此选项。

此选项不适用于通过 `<img>` 标签加载的 SVG 文件，因为它们无法使用页面中的字体。请改为将字体嵌入这些 SVG 文件。

## Tailwind CSS

Nuxt Fonts 会读取 Tailwind CSS 生成的 CSS，因此你可以将字体声明为主题变量：

```css [app/assets/css/main.css]
@import "tailwindcss";

@theme {
  --font-display: 'Inter', sans-serif;
}
```

然后使用 `font-display` 工具类应用该字体。更多详情以及 UnoCSS 的用法，请参阅 [Nuxt Fonts 用法指南](https://nuxt.zhcndoc.com/modules/fonts/usage#tailwind-css)。

## 后续步骤

Nuxt Fonts 的完整功能说明请参阅：

- [配置](https://nuxt.zhcndoc.com/modules/fonts/configuration)：设置字体的字重、样式、字符子集和预加载选项。
- [提供商](https://nuxt.zhcndoc.com/modules/fonts/providers)：配置 Adobe Fonts 等内置提供商，或编写自己的提供商。
- [工作原理](https://nuxt.zhcndoc.com/modules/fonts/advanced#how-it-works)：了解 Nuxt Fonts 如何解析字体并生成后备字体度量指标。


## Sitemap

See the full [sitemap](https://nuxt.zhcndoc.com/sitemap.md) for all pages.
