#Fonts with Tailwind 4
1 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
I've read the docs
... Let me show you how I do it.
import { Playfair_Display, Montserrat } from "next/font/google";
export const playfair_display = Playfair_Display({
variable: "--font-playfair-display",
subsets: ["latin"],
weight: ["400", "600", "800"],
style: ["normal", "italic"],
display: "swap",
adjustFontFallback: true,
preload: true,
});
export const montserrat = Montserrat({
variable: "--font-montserrat",
subsets: ["latin"],
weight: ["200", "300", "400", "500", "600"],
style: ["normal", "italic"],
display: "swap",
adjustFontFallback: true,
preload: true,
});
import { playfair_display, montserrat } from "./fonts";
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html
suppressHydrationWarning
suppressContentEditableWarning
>
{env.NODE_ENV == "development" && (
<script
async
src="//unpkg.com/react-scan/dist/auto.global.js"
></script>
)}
<body
className={`${playfair_display.variable} ${montserrat.variable} antialiased`}
>
{children}
</body>
</html>
);
}
Okay so there's basically no way to use tailwind for nextjs fonts anymore?
@import "tailwindcss";
@theme {
/* Fonts */
--font-playfair-display: var(--font-playfair-display);
--font-montserrat: var(--font-montserrat);
}
If you can, you have to use CSS to import the fonts.
and
As you can see in the example of my global.css define them in theme as variables with the prefix font-<name-font-here>
that's all
I did exactly this last night and was not getting the same results let me double check to make sure I didn't mess it up anywhere
Okey
import "@/styles/globals.css";
import { Figtree } from "next/font/google";
import type { AppProps } from "next/app";
export const figtree = Figtree({
variable: "--font-figtree",
subsets: ["latin"],
weight: ["500"],
preload: true,
});
export default function App({ Component, pageProps }: AppProps) {
return (
<main className={`${figtree.variable}`}>
<Component {...pageProps} />;
</main>
)
}
@theme {
--font-figtree: var(--font-figtree);
}```
Am I doing something wrong here?
No, it's okay
It's still not working 
Is the result as expected or is there some strange behavior?
check the devtools (network side)
Check if the font is downloading.
Alright one second
Doesn't look like the font is downloading
wait no it might be i actually can't tell the woff2 it's downloading has some arbitrary name
Okay, try other fonts (you don't have to use a specific one). We'll see if the same behavior persists with others.
That should be handled by next.js by default, as far as I know.
import "@/styles/globals.css";
import { Figtree } from "next/font/google";
import type { AppProps } from "next/app";
import { Playfair_Display } from "next/font/google";
export const playfair_display = Playfair_Display({
variable: "--font-playfair-display",
subsets: ["latin"],
weight: ["400", "600", "800"],
style: ["normal", "italic"],
display: "swap",
adjustFontFallback: true,
preload: true,
});
export const figtree = Figtree({
variable: "--font-figtree",
subsets: ["latin"],
weight: ["500"],
preload: true,
});
export default function App({ Component, pageProps }: AppProps) {
return (
<main className={`${playfair_display.variable}`}>
<Component {...pageProps} />;
</main>
)
}
Same issue
This is all that's needed inside the global.css file right?
@theme {
--font-figtree: var(--font-figtree);
--font-playfair-display: var(--font-playfair-display);
}```
Can you show me your package.json? (only the vital stuff please)
ye
in global.css
AH
AH
AH
AH
AH
AH
AH
Don't tell me you're importing globals.css and you have global.css? XDDD
Oh no lmao
I wish that was the case though
You had me sweating for a second there
"dependencies": {
"react": "^19.0.0",
"react-dom": "^19.0.0",
"next": "15.2.2"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"@tailwindcss/postcss": "^4",
"tailwindcss": "^4",
"eslint": "^9",
"eslint-config-next": "15.2.2",
"@eslint/eslintrc": "^3"
}```
x2
Looks good, have you migrated to the tailwind client?
$ npx @tailwindcss/upgrade
Just to make sure it's not a tailwind migration error.
(I've had problems with the migration client)
This is my first time messing with next so idk for sure but is this importing the variable as intended?
ye
@tailwindcss/upgrade@4.0.13
Ok to proceed? (y) y
≈ tailwindcss v4.0.13
│ Git directory is not clean. Please stash or commit your changes before migrating.
│ You may use the `--force` flag to silence this warning and perform the migration.```
I should continue with this then?
It seems to work except it doesn't show the font family. This is because the variable is specified, but you haven't used it, right? You can put it inside your elements. In case you've already tried it and it doesn't work for testing, create a test project by just importing the fonts and seeing if the behavior is different.
If you already have Tailwind 4, there is no need to upgrade further, it's a strange error.
v4 is now the stable version so I would say stay there
(Unless you see something that prompts you to move to the latest version.)
Alright
I'll try and make a new project see if I have same error
I assume usig Turbopack and not having a AppRouter shouldn't be the cause to this issue right?
No problem at all unless you have a specific configuration on your part (own experience).
okey
Okay so seems like it's functioning the same. I kept everything as default:
import "@/styles/globals.css";
import type { AppProps } from "next/app";
import { Playfair_Display } from "next/font/google";
export const playfair_display = Playfair_Display({
variable: "--font-playfair-display",
subsets: ["latin"],
weight: ["400", "600", "800"],
style: ["normal", "italic"],
display: "swap",
adjustFontFallback: true,
preload: true,
});
export default function App({ Component, pageProps }: AppProps) {
return (
<main className={`${playfair_display.variable}`}>
<p>test</p>
<Component {...pageProps} />;
</main>
)
}
@import "tailwindcss";
:root {
--background: #ffffff;
--foreground: #171717;
}
@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}
@theme {
--font-playfair-display: var(--font-playfair-display);
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}
However when I used this format it worked: <div className="font-[family-name:var(--font-playfair-display))]">
it's very strange
I suggest you ask for help on the tailwind forum. So far, we've tested next.js and it hasn't had any issues. Tailwind just has a strange problem when declaring custom font variables.
Thank you for sharing the details calmly.
Alright I'll do that whenever I find the time. Thanks for the help I appreciate it