Fonts in Next.js: next/font Without Layout Shift
Load Google and local fonts with next/font in Next.js: self-hosted files, no layout shift, CSS variables, and how to use them with Tailwind CSS v4.
By UndieGravity, , 1 min read
Fonts are a common cause of layout shift: the page renders with a fallback font, then jumps when the web font arrives. next/font solves this by downloading the font at build time, serving it from your own domain, and adjusting the fallback font so both take the same space.
Load a Google font
// app/layout.tsx
import { Inter } from "next/font/google";
const inter = Inter({ subsets: ["latin"], display: "swap", variable: "--font-inter" });
export default function RootLayout({ children }: LayoutProps<"/">) {
return (
<html lang="en" className={inter.variable}>
<body>{children}</body>
</html>
);
}The font files are downloaded during the build and served with your site, so the browser never contacts Google's servers. That is faster, and it avoids sending visitor IP addresses to a third party.
Load your own font files
import localFont from "next/font/local";
const brand = localFont({
src: [
{ path: "./fonts/Brand-Regular.woff2", weight: "400" },
{ path: "./fonts/Brand-Bold.woff2", weight: "700" },
],
variable: "--font-brand",
});Use woff2 files: they are the smallest format and supported by every current browser.
Use the font with Tailwind CSS v4
Tailwind v4 is configured in CSS. Point a theme token at the CSS variable that next/font created, and the font-sans class uses it everywhere.
@import "tailwindcss";
@theme {
--font-sans: var(--font-inter), ui-sans-serif, system-ui, sans-serif;
}Keep the download small
- Load only the subsets you need, usually
latin. - Prefer a variable font: one file covers every weight.
- Limit yourself to two families, one for headings and one for text.
- Avoid loading fonts with a CSS
@importfrom Google; it blocks rendering and bypassesnext/font.
Plain HTML sites
Without Next.js you can get most of the benefit by self-hosting the woff2 files, adding font-display: swap to your @font-face rules, and preloading the main file with <link rel="preload" as="font" type="font/woff2" crossorigin>.