Using next/font helps your website load fonts faster and look better on all devices. It automatically optimizes fonts for speed and quality.
Font optimization with next/font in NextJS
import { fontName } from 'next/font/google'; const myFont = fontName({ subsets: ['latin'], weight: ['400', '700'], style: ['normal', 'italic'], variable: '--my-font-variable', });
Replace fontName with the actual font you want, like Roboto or Inter.
You can specify subsets, weights, and styles to load only what you need.
import { Roboto } from 'next/font/google'; const roboto = Roboto({ subsets: ['latin'], weight: ['400'], });
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'], weight: ['400', '700'], style: ['normal', 'italic'], });
import localFont from 'next/font/local'; const myLocalFont = localFont({ src: './fonts/MyFont.woff2', variable: '--my-local-font', });
This Next.js component loads the Inter font with normal and bold weights. It applies the font style to the main content using the className provided by next/font. The font is optimized and loaded efficiently.
import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'], weight: ['400', '700'], }); export default function Home() { return ( <main className={inter.className} style={{ padding: '2rem' }}> <h1>Hello, Next.js with optimized fonts!</h1> <p>This text uses the Inter font loaded and optimized by next/font.</p> </main> ); }
Using next/font automatically preloads fonts for faster loading.
Only include the font weights and styles you need to keep your site fast.
Fonts loaded with next/font avoid layout shifts caused by late font loading.
next/font helps load fonts faster and better in Next.js apps.
You can load Google Fonts or local fonts with simple code.
Optimizing fonts improves user experience and website speed.
Practice
next/font in a Next.js application?Solution
Step 1: Understand the role of
next/fontnext/fontis 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 ofnext/font.Final Answer:
To optimize font loading for better performance and user experience -> Option CQuick Check:
Font optimization = To optimize font loading for better performance and user experience [OK]
- Thinking next/font creates fonts
- Confusing font optimization with image conversion
- Assuming next/font disables fonts
Inter font from Google Fonts using next/font/google?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 BQuick Check:
Correct import and call = import { Inter } from 'next/font/google'; const inter = Inter({ subsets: ['latin'] }); [OK]
- Using default import instead of named import
- Using require instead of import
- Importing from 'next/font/local' for Google Fonts
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?Solution
Step 1: Understand what
Theinter.classNameprovidesInterfont function returns an object with aclassNameproperty that applies the optimized font styles.Step 2: Check usage in the component
UsingclassName={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 DQuick Check:
inter.className applies optimized font styles = It applies the Inter font styles optimized and loaded by next/font [OK]
- Assuming className is undefined
- Thinking it applies system fonts instead
- Confusing className with inline styles
next/font/local:import localFont from 'next/font/local';
const myFont = localFont({
src: './fonts/MyFont.ttf',
weight: '400',
style: 'normal'
});
export default function Page() {
return <div className={myFont.className}>Sample Text</div>;
}Solution
Step 1: Check the
Local fonts requiresrcproperty format for localFontsrcto 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:
Thesrcpath must be an array, not a string -> Option AQuick Check:
Local font src requires array = Thesrcpath must be an array, not a string [OK]
- Using string instead of array for src
- Misusing import syntax with braces
- Assuming style is unsupported
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?Solution
Step 1: Check correct font names and import syntax
Font names must match exact exported names from next/font/google. 'OpenSans' is correct, 'Open_Sans' is not.Step 2: Verify usage of separate classNames
Applying separate classNames to different elements is correct for different fonts.Step 3: Review other options
import { Roboto, Open_Sans } from 'next/font/google'; const roboto = Roboto({ subsets: ['latin'] }); const openSans = Open_Sans({ subsets: ['latin'] }); export default function Page() { return <> <header className={roboto.className}>Header Text</header> <main className={openSans.className}>Main Text</main> </>; } uses wrong font name 'Open_Sans'. import Roboto from 'next/font/google'; import OpenSans from 'next/font/google'; const roboto = Roboto({ subsets: ['latin'] }); const openSans = OpenSans({ subsets: ['latin'] }); export default function Page() { return <> <header className={roboto.className}>Header Text</header> <main className={openSans.className}>Main Text</main> </>; } uses default imports incorrectly. import { Roboto, OpenSans } from 'next/font/google'; const roboto = Roboto({ subsets: ['latin'] }); const openSans = OpenSans({ subsets: ['latin'] }); export default function Page() { return <div className={`${roboto.className} ${openSans.className}`}>Combined Text</div>; } combines both fonts on one element, not separately.Final Answer:
Use named imports with correct font names and apply classNames separately -> Option AQuick Check:
Correct font names and separate usage = import { Roboto, OpenSans } from 'next/font/google'; const roboto = Roboto({ subsets: ['latin'] }); const openSans = OpenSans({ subsets: ['latin'] }); export default function Page() { return <> <header className={roboto.className}>Header Text</header> <main className={openSans.className}>Main Text</main> </>; } [OK]
- Using incorrect font names with underscores
- Using default imports instead of named imports
- Combining multiple fonts on one element incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
