Font Module
next/font
会自动优化你的字体(包括自定义字体),并移除外部网络请求,从而提升隐私和性能。
它为任意字体文件提供内置自动自托管。这意味着你可以无布局偏移地(layout shift)最佳加载 Web 字体。
你还可以便捷地使用所有 Google Fonts。CSS 和字体文件会在构建时下载,并与其他静态资源一起自托管。浏览器不会向 Google 发送任何请求。
- TypeScript
- JavaScript
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
要在所有页面中使用字体,请将其添加到 /pages
下的 _app.js
文件:
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.className}>
<Component {...pageProps} />
</main>
)
}
🎥 视频讲解: 了解更多关于
next/font
的用法 → YouTube(6 分钟)。
参考
键 | font/google | font/local | 类型 | 是否必填 |
---|---|---|---|---|
src | ❌ | ✅ | 字符串或对象数组 | 是 |
weight | ✅ | ✅ | 字符串或数组 | 必填/可选 |
style | ✅ | ✅ | 字符串或数组 | - |
subsets | ✅ | ❌ | 字符串数组 | - |
axes | ✅ | ❌ | 字符串数组 | - |
display | ✅ | ✅ | 字符串 | - |
preload | ✅ | ✅ | 布尔值 | - |
fallback | ✅ | ✅ | 字符串数组 | - |
adjustFontFallback | ✅ | ✅ | 布尔值或字符串 | - |
variable | ✅ | ✅ | 字符串 | - |
declarations | ❌ | ✅ | 对象数组 | - |
src
字体文件的路径,可以是字符串或对象数组(类型为 Array<{path: string, weight?: string, style?: string}>
),相对于调用字体加载函数的目录。
用于 next/font/local
- 必填
示例:
src:'./fonts/my-font.woff2'
,其中my-font.woff2
位于app/fonts
目录下src:[{path: './inter/Inter-Thin.ttf', weight: '100',},{path: './inter/Inter-Regular.ttf',weight: '400',},{path: './inter/Inter-Bold-Italic.ttf', weight: '700',style: 'italic',},]
- 如果在
app/page.tsx
中调用字体加载函数,使用src:'../styles/fonts/my-font.ttf'
,则my-font.ttf
位于项目根目录下的styles/fonts
weight
字体 weight
可选值如下:
- 单个权重字符串,或可变字体的权重范围字符串
- 非可变字体可用权重数组,仅适用于
next/font/google
用于 next/font/google
和 next/font/local
- 如果使用的字体不是可变字体,则必填
示例:
weight: '400'
:单个权重字符串——如Inter
可选'100'
、'200'
、'300'
、'400'
、'500'
、'600'
、'700'
、'800'
、'900'
或'variable'
(默认)weight: '100 900'
:可变字体的权重范围weight: ['100','400','900']
:非可变字体的权重数组
style
字体 style
可选值如下:
- 字符串值,默认
'normal'
- 非可变 Google 字体可用样式数组,仅适用于
next/font/google
用于 next/font/google
和 next/font/local
- 可选
示例:
style: 'italic'
:字符串,可为normal
或italic
(next/font/google
)style: 'oblique'
:字符串,可为任意标准字体样式(next/font/local
)style: ['italic','normal']
:Google 字体的样式数组
subsets
字体 subsets
由字符串数组定义,指定你希望预加载的子集。指定的子集会在 preload
为 true 时自动注入 preload 标签(默认)。
用于 next/font/google
- 可选
示例:
subsets: ['latin']
:包含latin
子集的数组
所有可用子集可在 Google Fonts 页面查看。
axes
部分可变字体有额外的 axes
可选。默认只包含字体权重以减小文件体积。可选值取决于具体字体。
用于 next/font/google
- 可选
示例:
axes: ['slnt']
:如Inter
可变字体的slnt
轴。可在 Google 可变字体页面 查看所有可用轴。
display
字体 display
可选值为 'auto'
、'block'
、'swap'
、'fallback'
或 'optional'
,默认 'swap'
。
用于 next/font/google
和 next/font/local
- 可选
示例:
display: 'optional'
:设置为optional
preload
布尔值,指定是否预加载字体。默认 true
。
用于 next/font/google
和 next/font/local
- 可选
示例:
preload: false
fallback
字体加载失败时的备用字体。为字符串数组,无默认值。
- 可选
用于 next/font/google
和 next/font/local
示例:
fallback: ['system-ui', 'arial']
:设置备用字体为system-ui
或arial
adjustFontFallback
- 对于
next/font/google
:布尔值,指定是否自动使用备用字体以减少布局偏移。默认true
。 - 对于
next/font/local
:字符串或布尔值false
,可选'Arial'
、'Times New Roman'
或false
,默认'Arial'
。
用于 next/font/google
和 next/font/local
- 可选
示例:
adjustFontFallback: false
:next/font/google
adjustFontFallback: 'Times New Roman'
:next/font/local
variable
字符串,用于定义 CSS 变量名(CSS 变量用法)。
用于 next/font/google
和 next/font/local
- 可选
示例:
variable: '--my-font'
:声明 CSS 变量--my-font
declarations
字体 face 描述符 的键值对数组,用于进一步定义生成的 @font-face
。
用于 next/font/local
- 可选
示例:
declarations: [{ prop: 'ascent-override', value: '90%' }]
示例
Google Fonts
要使用 Google 字体,从 next/font/google
以函数方式导入。推荐使用可变字体以获得最佳性能和灵活性。
- TypeScript
- JavaScript
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
如果无法使用可变字体,必须指定权重:
- TypeScript
- JavaScript
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
}
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({ children }) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
}
要在所有页面中使用字体,请将其添加到 /pages
下的 _app.js
文件:
import { Inter } from 'next/font/google'
// 加载可变字体时,无需指定 font weight
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.className}>
<Component {...pageProps} />
</main>
)
}
如果无法使用可变字体,必须指定权重:
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={roboto.className}>
<Component {...pageProps} />
</main>
)
}
你可以通过数组指定多个权重和/或样式:
const roboto = Roboto({
weight: ['400', '700'],
style: ['normal', 'italic'],
subsets: ['latin'],
display: 'swap',
})
提示: 多单词字体名请用下划线(_)连接。例如
Roboto Mono
应导入为Roboto_Mono
。
在 <head>
中应用字体
你也可以不使用 wrapper 和 className
,而是在 <head>
中注入样式:
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<>
<style jsx global>{`
html {
font-family: ${inter.style.fontFamily};
}
`}</style>
<Component {...pageProps} />
</>
)
}
单页面使用
要在单个页面中使用字体,直接在该页面引入:
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
return (
<div className={inter.className}>
<p>Hello World</p>
</div>
)
}