#Fonts with Tailwind 4

1 messages · Page 1 of 1 (latest)

dapper peak
#

What is the proper way to be importing fonts? All the tutorials I've seen have been for Tailwind 3 and since everything is supposed to be inside global.css now with the removal of tailwind.config.js. I can't seem to figure out how to use the next font package.

formal oliveBOT
#

🔎 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)

dapper peak
frail turtle
#

... 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>
  );
}
dapper peak
#

Okay so there's basically no way to use tailwind for nextjs fonts anymore?

frail turtle
#
@import "tailwindcss";
@theme {
  /* Fonts */
  --font-playfair-display: var(--font-playfair-display);
  --font-montserrat: var(--font-montserrat);
}
frail turtle
#

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

dapper peak
#

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

frail turtle
#

Okey

dapper peak
#

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?
frail turtle
#

No, it's okay

dapper peak
#

It's still not working thinkies

frail turtle
#

Is the result as expected or is there some strange behavior?

frail turtle
#

Check if the font is downloading.

dapper peak
#

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

frail turtle
frail turtle
dapper peak
#

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);
}```
frail turtle
#

Can you show me your package.json? (only the vital stuff please)

frail turtle
#

in global.css

#

AH

#

AH

#

AH

#

AH

#

AH

#

AH

dapper peak
#

AH

frail turtle
dapper peak
#

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"
  }```
frail turtle
frail turtle
#

(I've had problems with the migration client)

dapper peak
#

This is my first time messing with next so idk for sure but is this importing the variable as intended?

dapper peak
#

@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?
frail turtle
#

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.

frail turtle
#

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.)

dapper peak
#

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?

frail turtle
dapper peak
#

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))]">

dapper peak
#

Yeah very

#

I guess I'll just go with this for now

frail turtle
#

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.

dapper peak
#

Alright I'll do that whenever I find the time. Thanks for the help I appreciate it