字型模組
此 API 參考將幫助您了解如何使用 next/font/google
和 next/font/local
。如需功能和用法,請參閱最佳化字型頁面。
字型函數引數
鍵 | 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
,具有以下可能性
- 字串,包含特定字型可用的粗細值,或如果是可變字型,則為值範圍
- 如果字型不是可變 Google 字型,則為粗細值陣列。僅適用於
next/font/google
。
用於 next/font/google
和 next/font/local
- 如果使用的字型不是可變字型,則為必填
範例
weight: '400'
:單一粗細值的字串 - 對於字型Inter
,可能的值為'100'
、'200'
、'300'
、'400'
、'500'
、'600'
、'700'
、'800'
、'900'
或'variable'
,其中'variable'
為預設值)weight: '100 900'
:可變字型介於100
和900
之間的範圍字串weight: ['100','400','900']
:非可變字型的 3 個可能值陣列
style
字型的style
,具有以下可能性
- 字串值,預設值為
'normal'
- 如果字型不是可變 Google 字型,則為樣式值陣列。僅適用於
next/font/google
。
用於 next/font/google
和 next/font/local
- 選填
範例
style: 'italic'
:字串 - 對於next/font/google
,可以是normal
或italic
style: 'oblique'
:字串 - 對於next/font/local
,可以採用任何值,但預期來自標準字型樣式style: ['italic','normal']
:next/font/google
的 2 個值陣列 - 值來自normal
和italic
subsets
字型的subsets
,由字串值陣列定義,其中包含您想要預先載入的每個子集名稱。透過 subsets
指定的字型,當 preload
選項為 true 時(預設值),將在 head 中注入連結預先載入標籤。
用於 next/font/google
- 選填
範例
subsets: ['latin']
:包含子集latin
的陣列
您可以在 Google Fonts 頁面上找到字型的所有子集清單。
axes
某些可變字型具有可以包含的額外 axes
。預設情況下,僅包含字型粗細以縮減檔案大小。axes
的可能值取決於特定字型。
用於 next/font/google
- 選填
範例
axes: ['slnt']
:包含值slnt
的陣列,適用於Inter
可變字型,其具有slnt
作為額外axes
,如此處所示。您可以使用 Google 可變字型頁面上的篩選器,並尋找wght
以外的軸,來找到字型的可能axes
值
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
字型臉孔描述元鍵值對陣列,用於進一步定義產生的 @font-face
。
用於 next/font/local
- 選填
範例
declarations: [{ prop: 'ascent-override', value: '90%' }]
套用樣式
您可以使用三種方式套用字型樣式
className
傳回載入字型的唯讀 CSS className
,以傳遞至 HTML 元素。
<p className={inter.className}>Hello, Next.js!</p>
style
傳回載入字型的唯讀 CSS style
物件,以傳遞至 HTML 元素,包括 style.fontFamily
以存取字型系列名稱和後備字型。
<p style={inter.style}>Hello World</p>
CSS 變數
如果您想要在外部樣式表設定樣式,並在那裡指定其他選項,請使用 CSS 變數方法。
除了匯入字型外,也匯入定義 CSS 變數的 CSS 檔案,並將字型載入器物件的 variable 選項設定如下
import { Inter } from 'next/font/google'
import styles from '../styles/component.module.css'
const inter = Inter({
variable: '--font-inter',
})
若要使用字型,請將您想要設定樣式的文字的父容器的 className
設定為字型載入器的 variable
值,並將文字的 className
設定為來自外部 CSS 檔案的 styles
屬性。
<main className={inter.variable}>
<p className={styles.text}>Hello World</p>
</main>
在 component.module.css
CSS 檔案中定義 text
選取器類別,如下所示
.text {
font-family: var(--font-inter);
font-weight: 200;
font-style: italic;
}
在上面的範例中,文字 Hello World
使用 Inter
字型和產生的字型後備樣式(font-weight: 200
和 font-style: italic
)設定樣式。
使用字型定義檔
每次呼叫 localFont
或 Google 字型函數時,該字型都將作為應用程式中的一個實例託管。因此,如果您需要在多個位置使用相同的字型,您應該在一個位置載入它,並在您需要它的地方匯入相關的字型物件。這是使用字型定義檔完成的。
例如,在應用程式目錄根目錄的 styles
資料夾中建立 fonts.ts
檔案。
然後,指定您的字型定義,如下所示
import { Inter, Lora, Source_Sans_3 } from 'next/font/google'
import localFont from 'next/font/local'
// define your variable fonts
const inter = Inter()
const lora = Lora()
// define 2 weights of a non-variable font
const sourceCodePro400 = Source_Sans_3({ weight: '400' })
const sourceCodePro700 = Source_Sans_3({ weight: '700' })
// define a custom local font where GreatVibes-Regular.ttf is stored in the styles folder
const greatVibes = localFont({ src: './GreatVibes-Regular.ttf' })
export { inter, lora, sourceCodePro400, sourceCodePro700, greatVibes }
您現在可以在程式碼中使用這些定義,如下所示
import { inter, lora, sourceCodePro700, greatVibes } from '../styles/fonts'
export default function Page() {
return (
<div>
<p className={inter.className}>Hello world using Inter font</p>
<p style={lora.style}>Hello world using Lora font</p>
<p className={sourceCodePro700.className}>
Hello world using Source_Sans_3 font with weight 700
</p>
<p className={greatVibes.className}>My title in Great Vibes font</p>
</div>
)
}
為了更輕鬆地在程式碼中存取字型定義,您可以在 tsconfig.json
或 jsconfig.json
檔案中定義路徑別名,如下所示
{
"compilerOptions": {
"paths": {
"@/fonts": ["./styles/fonts"]
}
}
}
您現在可以匯入任何字型定義,如下所示
import { greatVibes, sourceCodePro400 } from '@/fonts'
版本變更
版本 | 變更 |
---|---|
v13.2.0 | @next/font 更名為 next/font 。不再需要安裝。 |
v13.0.0 | 新增 @next/font 。 |
這是否有幫助?