跳至內容

字體模組

此 API 參考將幫助您了解如何使用 next/font/googlenext/font/local。如需功能和用法,請參閱字體最佳化頁面。

字體函式引數

如需使用說明,請參閱 Google FontsLocal Fonts

鍵值font/googlefont/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/googlenext/font/local

  • 如果使用的字體不是可變字體,則為必要項。

範例

  • weight: '400':字體 Inter 的單一字重值字串,可用的值有 '100''200''300''400''500''600''700''800''900''variable''variable' 為預設值)。
  • weight: '100 900':變數字體的字重範圍字串,範圍介於 100900 之間。
  • weight: ['100','400','900']:非變數字體的三個可用字重值陣列。

style

字體 style 具有以下可能性:

  • 字串 ,預設值為 'normal'
  • 如果字體不是 Google 變數字體,則為樣式值陣列。此僅適用於 next/font/google

用於 next/font/googlenext/font/local

  • 選用。

範例

  • style: 'italic':字串,對於 next/font/google 來說,可以是 normalitalic
  • style: 'oblique':字串,對於 next/font/local 來說可以是任何值,但預期來自 標準字體樣式
  • style: ['italic','normal']next/font/google 的兩個值陣列,值來自 normalitalic

subsets

字體 subsets 由一個字串值陣列定義,其中包含您希望預先載入的每個子集的名稱。當 preload 選項為 true(預設值)時,透過 subsets 指定的字體將會在 head 中插入一個連結預載標籤。

用於 next/font/google

  • 選用。

範例

  • subsets: ['latin']:一個包含 latin 子集的陣列

您可以在 Google Fonts 頁面上找到您字體的所有子集列表。

axes

某些可變字體具有額外的 axes 可以包含。預設情況下,僅包含字體粗細以減小檔案大小。axes 的可能值取決於特定的字體。

用於 next/font/google

  • 選用。

範例

  • axes: ['slnt']:一個包含 Inter 可變字體 slnt 值的陣列,如這裡所示,其中 slnt 作為額外的 axes。您可以透過在Google 可變字體頁面上使用篩選器並尋找 wght 以外的軸來找到字體的可能 axes 值。

display

字體 display,其可能的字串'auto''block''swap''fallback''optional',預設值為 'swap'

用於 next/font/googlenext/font/local

  • 選用。

範例

  • display: 'optional':指定為 optional 值的字串

preload

一個布林值,指定字體是否應該被預先載入。預設值為 true

用於 next/font/googlenext/font/local

  • 選用。

範例

  • preload: false

fallback

如果字體無法載入,則使用的後備字體。一個字串陣列,包含後備字體,沒有預設值。

  • 選用。

用於 next/font/googlenext/font/local

範例

  • fallback: ['system-ui', 'arial']:一個陣列,將後備字體設定為 system-uiarial

adjustFontFallback 累積版面配置偏移。預設值為 true
  • 對於 next/font/local:一個字串或布林值 false,設定是否應使用自動後備字體來減少累積版面配置偏移。可能的值為 'Arial''Times New Roman'false。預設值為 'Arial'
  • 用於 next/font/googlenext/font/local

    • 選用。

    範例

    • adjustFontFallback: false:適用於 next/font/google
    • adjustFontFallback: 'Times New Roman':適用於 next/font/local

    variable

    一個字串值,用於定義如果使用CSS 變數方法套用樣式時要使用的 CSS 變數名稱。

    用於 next/font/googlenext/font/local

    • 選用。

    範例

    • variable: '--my-font':宣告 CSS 變數 --my-font

    declarations

    一個字型描述符(descriptor)鍵值對的陣列,用於進一步定義生成的 @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 檔案,並如下設定字型載入器物件的變數選項

    app/page.tsx
    import { Inter } from 'next/font/google'
    import styles from '../styles/component.module.css'
     
    const inter = Inter({
      variable: '--font-inter',
    })

    要使用字型,請將您要套用樣式的文字的父容器的 className 設定為字型載入器的 variable 值,並將文字的 className 設定為外部 CSS 檔案中的 styles 屬性。

    app/page.tsx
    <main className={inter.variable}>
      <p className={styles.text}>Hello World</p>
    </main>

    component.module.css CSS 檔案中定義 text 選擇器類別,如下所示

    styles/component.module.css
    .text {
      font-family: var(--font-inter);
      font-weight: 200;
      font-style: italic;
    }

    在上面的範例中,文字 Hello World 使用 Inter 字型和生成的字型後備字型進行樣式設定,並使用 font-weight: 200font-style: italic

    使用字體定義檔案

    每次呼叫 localFont 或 Google 字體函式時,該字體都會在您的應用程式中作為一個實例託管。因此,如果您需要在多個位置使用相同的字體,應該將其載入到一個位置,然後在需要的位置導入相關的字體物件。這可以使用字體定義檔案來完成。

    例如,在應用程式目錄根目錄的 styles 資料夾中建立一個 fonts.ts 檔案。

    然後,如下指定您的字體定義:

    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 }

    現在,您可以在程式碼中如下使用這些定義:

    app/page.tsx
    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.jsonjsconfig.json 檔案中定義路徑別名,如下所示:

    tsconfig.json
    {
      "compilerOptions": {
        "paths": {
          "@/fonts": ["./styles/fonts"]
        }
      }
    }

    現在您可以如下導入任何字體定義:

    app/about/page.tsx
    import { greatVibes, sourceCodePro400 } from '@/fonts'

    版本變更

    版本變更
    v13.2.0@next/font 更名為 next/font。不再需要安裝。
    v13.0.0新增 @next/font