To change the font of your Next.js app efficiently, you can use the following methods:
1. Use the Built-in next/font Module
Next.js provides the next/font module for optimizing and managing fonts. It allows you to include Google Fonts or local fonts with performance enhancements, like automatic font subset optimization.
Steps for Google Fonts
- Install the Font Package:
npm install @next/font
- Use a Google Font in Your App:
Modify yourlayout.jsorpages/_app.jsfile:
import { Inter } from 'next/font/google';
const inter = Inter({ subsets: ['latin'] });
export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={inter.className}>{children}</body>
</html>
);
}
- Apply the Font Globally:
- The
inter.classNameautomatically applies the font style globally.
Steps for Custom or Local Fonts
If you have a custom font file, you can use the next/font/local import:
- Store Your Font File:
Place your font files in thepublic/fontsfolder. - Use the Font in Your Code:
import localFont from 'next/font/local';
const myFont = localFont({
src: './fonts/MyFont-Regular.woff2',
variable: '--font-myfont',
});
export default function RootLayout({ children }) {
return (
<html lang="en">
<body className={myFont.className}>{children}</body>
</html>
);
}
- CSS Custom Properties for Further Styling:
You can define a variable for the font (e.g.,--font-myfont) and use it in your global CSS:
:root {
--font-myfont: 'MyFont', sans-serif;
}
body {
font-family: var(--font-myfont);
}
2. Use a CSS Framework (e.g., Tailwind CSS)
If you are using Tailwind CSS, you can configure your font in tailwind.config.js:
- Install the Font via Tailwind or Manually Add It:
npm install @tailwindcss/typography
- Modify the Tailwind Configuration:
module.exports = {
theme: {
extend: {
fontFamily: {
sans: ['Inter', 'sans-serif'],
},
},
},
plugins: [require('@tailwindcss/typography')],
};
- Apply the Font:
Usefont-sansin your components or globally in thehtmlorbodytags.
3. Use Global CSS
If you want to manage fonts directly in a CSS file:
- Add a Global CSS File:
Create astyles/globals.cssfile if it doesn’t exist. - Import the Font Using
@importin CSS:
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap');
body {
font-family: 'Inter', sans-serif;
}
- Import the CSS File in
_app.js:
import '../styles/globals.css';
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
Comparison of Methods
| Method | Performance | Ease of Use | Notes |
|---|---|---|---|
next/font | ✅ Best | ✅ Easy | Built-in optimization, font subsetting, ideal for most cases |
| Tailwind CSS Framework | ✅ Good | ✅ Easy | Works well if using Tailwind, but adds dependency |
| Global CSS with Google | ⚠️ Okay | ✅ Easy | No optimization; rely on Google Fonts’ CDN |
Recommendation
The next/font module is the most efficient way to manage fonts in Next.js, offering optimal performance and seamless integration. Use it unless you have a specific reason to manage fonts differently.

