Fonts with next/font
Load Google and local fonts with next/font so they are self-hosted at build time, cause no layout shift, and connect to Tailwind through CSS variables.
Fonts with next/font
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `next/font` downloads Google Fonts at build time and serves them from your own domain, so the browser sends no requests to Google when someone visits.
- You call the font as a function with options such as `subsets`, then apply it with `className`, `style`, or a CSS variable; apply it in the root layout to cover the whole app.
- A variable font needs no `weight`; a font that is not variable requires one.
- Next.js generates a fallback font adjusted to match your font's metrics by default, which is how it avoids layout shift when the real font loads.
- To use a font from Tailwind, give the font a `variable` name, put that variable's class on `<html>`, and map it in an `@theme inline` block.
Why fonts need help
A custom font is a file the browser has to download after it has already started drawing your page. Two things go wrong. Before the file arrives the browser shows either nothing or a fallback font, and when the real font lands, the text may be a different width and the page jumps. And if the font comes from a third party’s server, every visitor makes a request to that party before your page looks right.
next/font addresses both. The docs say it optimizes your fonts and removes external network requests, with built-in self-hosting for any font file, so you can load web fonts with no layout shift. The css roadmap covers the browser mechanics in font families and web fonts and variable fonts and font loading. Here is how Next.js packages them.
A Google font
Import the font by name from next/font/google, call it, and apply it.
// app/layout.tsx
import { Geist } from 'next/font/google'
const geist = Geist({
subsets: ['latin'],
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={geist.className}>
<body>{children}</body>
</html>
)
}
Fonts are scoped to the component they are used in. To cover the whole app, apply the font in the root layout, as above.
The self-hosting claim is concrete: the CSS and font files are downloaded at build time and served as static assets with the rest of your site, so the browser sends no requests to Google.
Three options you will meet:
subsets. Google Fonts are split into subsets such aslatin. You say which ones to preload. Ifpreloadistrue, which is the default, and you give no subsets, you get a warning.weight. The docs recommend variable fonts. A variable font needs no weight. A font that is not variable requires one:weight: '400'or an array like['400', '700'], andstylecan take['normal', 'italic'].display. Takes the standardfont-displayvalues and defaults to'swap'.
For a font with several words in its name, use an underscore: Roboto Mono is imported as Roboto_Mono.
A local font
For a file you own, use next/font/local and point src at it. The path is resolved relative to the file where you call the function, and the file can live anywhere in the project, including public or next to the layout in app.
import localFont from 'next/font/local'
const myFont = localFont({
src: './my-font.woff2',
})
If one family spans several files, src can be an array of { path, weight, style } objects.
No layout shift
The fallback font is the interesting part. By default, adjustFontFallback is on: Next.js produces an automatic fallback font to reduce Cumulative Layout Shift. For Google fonts it is a boolean, default true. For local fonts it is 'Arial' by default, or 'Times New Roman', or false. The idea is to show a system font adjusted to take up about the same space as your real font, so the swap does not move the text around.
Three ways to apply a font
classNamereturns a read-only class for the font family. Put it on any element.stylereturns a style object includingfontFamilywith the fallbacks, forstyle={inter.style}.- CSS variables are what you want with several fonts. Set the
variableoption, put the returnedvariableclass on a parent, and usevar(--font-inter)in your CSS.
const inter = Inter({ subsets: ['latin'], variable: '--font-inter' })
<main className={inter.variable}>
<p className={styles.text}>Hello World</p>
</main>
With Tailwind
The docs wire fonts to Tailwind v4 through the variable approach. Give each font a variable name, put the variable classes on <html>, then map them in your global CSS:
// app/layout.tsx
const inter = Inter({
subsets: ['latin'],
display: 'swap',
variable: '--font-inter',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
variable: '--font-roboto-mono',
})
// ...
<html lang="en" className={`${inter.variable} ${roboto_mono.variable} antialiased`}>
/* app/globals.css */
@import 'tailwindcss';
@theme inline {
--font-sans: var(--font-inter);
--font-mono: var(--font-roboto-mono);
}
Now font-sans and font-mono are utilities. If you are still on Tailwind v3, the docs show a fontFamily entry in tailwind.config.js for the same job. Setup for Tailwind itself is in the previous lesson.
Load each font once, preload where it is used
Each call to a font function hosts one instance of that font. If several files need the same font, load it in one shared file and import the object:
// app/fonts.ts
import { Inter, Roboto_Mono } from 'next/font/google'
export const inter = Inter({ subsets: ['latin'], display: 'swap' })
export const roboto_mono = Roboto_Mono({ subsets: ['latin'], display: 'swap' })
A font is not global just because you defined it. It is preloaded on the routes tied to the file where it is used: a page’s own route, all routes under a layout, or all routes for the root layout. So a heading font used on one page is not preloaded on the rest of the site. Since every extra font is another download, the docs advise using multiple fonts conservatively.
What the video covers
Tuomo Kankaanpää’s video is from December 2024 and runs about nine and a half minutes. Per its title and the chapters recorded when it was sourced, it covers Google fonts, local fonts, connecting a font to Tailwind through a variable, and how next/font self-hosts. We could not read a transcript, so the section above rests on the docs. Its description promotes a paid course. The runner-up, Lee Robinson’s 2022 intro, is a shorter original, and the current API reference links to it; but it predates the rename, so it shows the @next/font package name that next/font replaced in 13.2. Both channels are English-language by title and description; confirm Tuomo’s accent by ear.
Where this fits
Text is done. Images are the other big source of layout shift and wasted bytes. Next: optimizing images.
Check yourself
What does it mean that `next/font` self-hosts Google Fonts?
The CSS and font files are downloaded at build time and served as static assets from your own domain. The visitor's browser makes no request to Google.
You call `Roboto({ subsets: ['latin'] })` and get an error about weight. Why, and what is the fix?
Roboto is not a variable font on Google Fonts, so `weight` is required. Pass one, such as `weight: '400'`, or an array of weights.
What is the difference between applying a font with `className` and with the `variable` option?
`className` applies the font family directly to that element. `variable` declares a CSS custom property (for example `--font-inter`) and you reference it from CSS or a Tailwind theme, which lets you use several fonts and choose where each applies.
You use the same font in five components. How should you load it?
Call the font function once, in a shared fonts file, and import the resulting object where you need it. Each call to a font function hosts one instance of that font.
Where is a font preloaded when you call it in a page, a layout, or the root layout?
In a page it is preloaded on that page's route, in a layout on all routes the layout wraps, and in the root layout on all routes.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.