Frontend
Inside Tailwind v4: how the native CSS factory works
FrontendNov 28, 2026

Inside Tailwind v4: how the native CSS factory works

Learn how Tailwind v4 compiles @theme, @theme inline and @layer into native CSS, and avoid silent dark mode and font bugs in Next.js.

Reading: 8 min

Tailwind v4 is breaking interfaces quietly. As LLMs churn out code mixing v3 conventions with the new CSS-first architecture, developers are left with silent bugs that compile cleanly: dark modes that refuse to switch and custom fonts that silently fall back to system defaults.

When that happens, guessing tweaks in your JSX won't fix it. The fastest path is to look at what Tailwind v4 really is under the hood: a compiler that turns directives into native CSS variables, media queries and @layer cascade layers. Once you understand that output and have a practical way to inspect the compiled CSS, you stop guessing, both in the code you write and in the code the AI generates.

What the browser sees in DevTools#

The browser doesn't know Tailwind exists. The compiler resolves every class at build time and ships plain CSS selectors.

When you write md:block in a component, the compiler reads the value of the md breakpoint (48rem by default) and generates a native media query using range syntax:

css
@media (width >= 48rem) {
  .md\:block {
    display: block;
  }
}

The default breakpoints are sm (40rem), md (48rem), lg (64rem), xl (80rem) and 2xl (96rem). If you change 2xl to 86rem, the output becomes:

css
@media (width >= 86rem) {
  .\32 xl\:block {
    display: block;
  }
}

That \32 at the start of the selector isn't a Tailwind trick. Per the CSS spec, a class name can't start with a digit, so the 2 has to be escaped with its hexadecimal code, which is 32. The framework just writes the CSS you would have had to write by hand.

Why @theme exists when we already have var()#

If Tailwind v4 uses native CSS, why do breakpoints have to live in a @theme block instead of regular CSS variables?

Because CSS itself doesn't allow var() inside a media query. This code is invalid, and the browser simply ignores it:

css
/* Invalid: var() doesn't work inside @media */
@media (width >= var(--breakpoint-md)) {
  .custom-container {
    max-width: 100%;
  }
}

The compiler works around this at build time. It reads --breakpoint-2xl: 86rem; in @theme and copies the 86rem value straight into the media queries of the responsive variants. Where CSS requires a fixed value, Tailwind writes the number. Where CSS accepts a variable, it uses a variable.

One detail worth remembering: by default, Tailwind only emits the theme variables that something actually uses. If you need all of them, even the ones no class uses, there's @theme static.

@theme vs @theme inline: the dark mode bug#

In practice, this is where the default @theme tends to betray anyone expecting dynamic behavior.

You define your dark theme colors, put the .dark class on a child element such as <div class="dark">, and the colors don't change. The reason is how the browser resolves variables that point to other variables.

What the regular @theme generates#

When you bind a theme variable to another variable inside a regular @theme:

css
@theme {
  --color-background: var(--background);
}

Tailwind generates two separate things:

css
:root {
  --color-background: var(--background);
}

.bg-background {
  background-color: var(--color-background);
}

Look at where the problem is. var(--background) is resolved where --color-background was defined, which is :root, the <html> element. Up there, --background holds the light theme value. That already-resolved value flows down the tree. When a child with .dark redefines --background, the .bg-background class keeps reading --color-background, which was already computed at the top.

The Tailwind docs put it another way: a variable is resolved where it's defined, not where it's used.

What @theme inline generates#

That's what @theme inline is for:

css
@theme inline {
  --color-background: var(--background);
}

With inline, Tailwind skips the intermediate variable and puts var(--background) straight into the class:

css
.bg-background {
  background-color: var(--background);
}

Now the browser resolves --background on the element that has the class. If that element sits inside .dark, it picks up the dark value right away.

The rule of thumb:

  • @theme: Resolves the variable on :root, where the theme variable is created. Use it for fixed values such as hex colors, sizes and breakpoints.

  • @theme inline: Resolves the variable directly on the element that uses the class. Use it when a variable points to another variable: dark mode, next/font fonts and tokens that change by context.

Reserved prefixes: why not every variable becomes a class#

Another common mistake is assuming any variable inside @theme becomes a utility class. The compiler doesn't guess. It only generates classes for variables whose names start with one of the namespaces it knows. The most common ones:

  • --color-*: Generates color classes such as bg-*, text-* and border-*.

  • --font-*: Generates font families such as font-sans.

  • --text-*: Generates text sizes such as text-xl.

  • --breakpoint-*: Generates responsive variants such as sm: and md:.

  • --spacing: Drives the margin, padding, gap and sizing scale, such as m-4, p-4 and gap-4.

  • --radius-*: Generates border radius classes such as rounded-*.

  • --shadow-*: Generates shadows such as shadow-*.

The full list is much longer: it also covers font weight, letter spacing, line height, container queries, blur, aspect ratio, easing curves, animations and more. You'll find it on the theme variables page of the docs. A variable outside these namespaces still works as a regular CSS variable, but it doesn't generate any class.

A variable doesn't know what it's for#

Remember one CSS rule: a variable holds a value without knowing which property it will end up in. --radius: 0.5625rem; is just a number with a unit until you bind it to a property:

css
@theme inline {
  --radius-lg: var(--radius);
}

That generates .rounded-lg { border-radius: var(--radius); }. Now, if you accidentally bind a color token to border-radius, or a radius value to color, nothing breaks at build time and nothing shows up in the console. The browser treats the declaration as invalid when it computes the value (the spec term is invalid at computed-value time) and acts as if it weren't there. The result is a silent visual bug.

CSS cascade layers and the font bug#

The second bug has to do with CSS priority order. Tailwind v4 uses native CSS cascade layers (@layer) and declares this order:

css
@layer theme, base, components, utilities;

The layer declared first is the weakest. And any CSS outside a layer beats all of them. So the order, from weakest to strongest, is: theme, base, components, utilities and, last, loose CSS outside any @layer.

Tailwind's default fonts live in the theme, which goes into the theme layer, the weakest one:

css
@theme default {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, ...;
  --font-serif: ui-serif, Georgia, Cambria, ...;
  --font-mono: ui-monospace, SFMono-Regular, ...;
}

Now the Next.js scenario. You load the font in app/layout.tsx with next/font, which creates the --font-inter variable on the <html> tag. Many projects then write --font-sans: var(--font-inter); inside a :root in globals.css, with no @layer at all.

Since that :root sits outside the layers, it beats Tailwind's theme, and .font-sans starts using your font. The day someone deletes that line thinking it's leftover, the project falls back to the system font without any error.

The approach the Tailwind docs recommend is to declare the font in the theme itself, with inline:

tsx
// app/layout.tsx
import { Inter } from "next/font/google";

const inter = Inter({ subsets: ["latin"], variable: "--font-inter" });

export default function RootLayout({ children }) {
  return (
    <html lang="en" className={inter.variable}>
      <body>{children}</body>
    </html>
  );
}
css
/* app/globals.css */
@import "tailwindcss";

@theme inline {
  --font-sans: var(--font-inter);
}

With inline, .font-sans gets var(--font-inter) directly, and the font lives in a place anyone on the team recognizes as part of the theme.

Where each rule goes in the project#

To keep styles from leaking between pages in a Next.js app with Tailwind v4, the safest split puts the rules in three places.

The first is app/globals.css, the core of the design system. That's where @import "tailwindcss";, plugins via @plugin, raw variables on :root and .dark, and the @theme and @theme inline blocks go. Any reset on HTML tags, such as *, body or h1, goes inside @layer base. Outside the layer, it would beat the utility classes.

The second is your *.module.css files, for more complex component styles. For a utility class passed through className, such as <Button className="mt-4" />, to override the module without !important, put the module's classes inside @layer components:

css
/* Button.module.css */
@layer components {
  .primaryButton {
    display: inline-flex;
    align-items: center;
    padding: 0.75rem 1.5rem;
    background-color: var(--color-primary);
    border-radius: var(--radius-md);
  }
}

Two things to watch with CSS Modules. First, they're processed separately from the main file, so they can't see your theme at build time. If you want to use @apply inside them, you need @reference pointing to globals.css. Second, since Tailwind only emits the theme variables that are used, check in DevTools that --color-primary actually exists in the final CSS. If it doesn't, @theme static fixes it.

The third is tailwind.config.js, which is now legacy only. v4 no longer detects that file on its own. If an old plugin still needs it, load it with @config "../../tailwind.config.js"; in your CSS. And note that the corePlugins, safelist and separator options no longer work in v4. All design tokens now live in CSS.

Wrapping up#

Use @theme inline whenever a theme variable points to another variable, as with dark mode colors and next/font fonts. Remember that only known namespaces become classes and that CSS outside @layer beats everything, utilities included. And put your CSS Modules classes in @layer components so className can override them.

What silent layer, font or dark mode bug has shown up in your Tailwind v4 project? And how did you fix it?

Sources#

#CSS#Next.js#Frontend Architecture