📖 Scenario: You are building a simple Next.js website that needs to use a custom Google font efficiently.
🎯 Goal: Learn how to import and use a Google font with the next/font package to optimize font loading and improve performance.
📋 What You'll Learn
Import a Google font using next/font/google
Configure the font with specific weights and subsets
Apply the font style to a React component
Use the font's className in the component's root element
💡 Why This Matters
🌍 Real World
Optimizing fonts in Next.js apps improves page load speed and user experience by loading only needed font styles.
💼 Career
Knowing how to use next/font for font optimization is important for frontend developers working with Next.js to build fast, accessible websites.
Progress0 / 4 steps
1
Import the Roboto font from next/font/google
Write an import statement to import Roboto from next/font/google.
NextJS
Hint
Use import { Roboto } from 'next/font/google' to import the font.
2
Configure the Roboto font with weights and subsets
Create a constant called roboto by calling Roboto with an object that sets weight to ["400", "700"] and subsets to ["latin"].
NextJS
Hint
Use Roboto({ weight: ["400", "700"], subsets: ["latin"] }) to configure the font.
3
Create a React component that uses the Roboto font
Write a functional React component called HomePage that returns a <main> element with the className set to roboto.className. Inside <main>, add an <h1> with the text Welcome to Next.js.
NextJS
Hint
Use className={roboto.className} on the <main> element to apply the font.
4
Export the HomePage component as default
Add export default HomePage at the end of the file to export the component.
NextJS
Hint
Use export default HomePage to export the component.
Practice
(1/5)
1. What is the main purpose of using next/font in a Next.js application?
easy
A. To convert fonts into images for display
B. To create custom font files from scratch
C. To optimize font loading for better performance and user experience
D. To disable all fonts and use system defaults only
Solution
Step 1: Understand the role of next/font
next/font is designed to load fonts efficiently in Next.js apps, improving speed and user experience.
Step 2: Compare options with this purpose
Only To optimize font loading for better performance and user experience describes optimization of font loading, which matches the purpose of next/font.
Final Answer:
To optimize font loading for better performance and user experience -> Option C
Quick Check:
Font optimization = To optimize font loading for better performance and user experience [OK]
Hint: Remember: next/font improves font loading speed [OK]
Common Mistakes:
Thinking next/font creates fonts
Confusing font optimization with image conversion
Assuming next/font disables fonts
2. Which of the following is the correct way to import the Inter font from Google Fonts using next/font/google?
easy
A. import Inter from 'next/font/google'; const inter = new Inter();
B. import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] });
C. const inter = require('next/font/google').Inter();
D. import { Inter } from 'next/font/local'; const inter = Inter();
Solution
Step 1: Check the import syntax for Google Fonts
The correct import uses named import from 'next/font/google' and calls the font function with options.
Step 2: Validate the usage
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); matches the correct pattern: import with braces, call with options object.
Final Answer:
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); -> Option B
Quick Check:
Correct import and call = import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); [OK]
Hint: Use named import and call font as a function with options [OK]
Common Mistakes:
Using default import instead of named import
Using require instead of import
Importing from 'next/font/local' for Google Fonts
3. Given this code snippet in a Next.js component:
import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'] });
export default function Home() {
return <main className={inter.className}>Hello World</main>;
}
What will be the effect of using inter.className in the <main> element?
medium
A. It adds a random CSS class unrelated to fonts
B. It causes a runtime error because className is undefined
C. It applies default system fonts ignoring Inter
D. It applies the Inter font styles optimized and loaded by next/font
Solution
Step 1: Understand what inter.className provides
The Inter font function returns an object with a className property that applies the optimized font styles.
Step 2: Check usage in the component
Using className={inter.className} applies the Inter font styles to the <main> element.
Final Answer:
It applies the Inter font styles optimized and loaded by next/font -> Option D
Quick Check:
inter.className applies optimized font styles = It applies the Inter font styles optimized and loaded by next/font [OK]
Hint: className from next/font applies the font styles [OK]
Common Mistakes:
Assuming className is undefined
Thinking it applies system fonts instead
Confusing className with inline styles
4. Identify the error in this code snippet that tries to load a local font with next/font/local:
B. The weight property should be a number, not a string
C. The import should use braces: { localFont }
D. The style property is not supported in local fonts
Solution
Step 1: Check the src property format for localFont
Local fonts require src to be an array of objects with path and weight, not a single string.
Step 2: Validate other properties
Weight can be string or number, import default is correct, style is supported.
Final Answer:
The src path must be an array, not a string -> Option A
Quick Check:
Local font src requires array = The src path must be an array, not a string [OK]
Hint: Local font src must be an array of objects [OK]
Common Mistakes:
Using string instead of array for src
Misusing import syntax with braces
Assuming style is unsupported
5. You want to load two Google Fonts, Roboto and Open Sans, both with latin subsets, and apply them to different parts of your Next.js page. Which approach correctly uses next/font/google to optimize both fonts and apply them separately?