Tailwind CSS 4 theme tokens in Next.js 16: oklch, @theme inline, and 4 traps that build without errors
DEV Community

Tailwind CSS 4 theme tokens in Next.js 16: oklch, @theme inline, and 4 traps that build without errors

I moved a landing page's colors into Tailwind CSS 4 theme tokens: a brand scale in oklch, plus surface and ink tokens that flip in dark mode. Every build passed.

Then I read the computed colors with Playwright and found four bugs that no build step reported.

I tested everything in a fresh create-next-app project on Next.js 16.5.0, React 19.3.1 and Tailwind CSS 4.3.5 (create-next-app installed 4.3.3; I upgraded and re-ran every test). Each result below comes from next build, next start, and getComputedStyle in Chromium.

Where Tailwind lives in a new Next.js 16.5 project

There's no postcss.config.mjs anymore. create-next-app 16.5 wires Tailwind through a Turbopack loader in next.config.ts:

// next.config.ts (generated, trimmed)
const nextConfig : NextConfig = {
  turbopack : {
    rules : {
      "*.css" : {
        loaders : [ "@tailwindcss/turbopack" ] ,
        as : "*.css" ,
      } ,
    } ,
  } ,
} ;

The Next.js CSS docs only use @tailwindcss/postcss and a postcss.config.mjs when you build with webpack or Rspack. Everything below goes in app/globals.css, after @import "tailwindcss";.

Brand colors: oklch values inside @theme

@import "tailwindcss" ;
@theme {
  --color-brand-50 : oklch ( 0.97 0.02 265 ) ;
  --color-brand-500 : oklch ( 0.55 0.2 265 ) ;
  --color-brand-900 : oklch ( 0.3 0.12 265 ) ;
  --radius-card : 1rem ;
}

A variable in a known namespace creates utilities: --color-* gives you bg-brand-500, text-brand-900, border-brand-50 and the rest, and --radius-* gives you rounded-card. A variable in :root creates no utility. The Tailwind docs say to use @theme for tokens that should map to a class, and :root for plain variables.

Opacity modifiers work with oklch. bg-brand-500/50 compiled to:

.bg-brand-500 \/ 50 { background-color : #3665e480 ; }
@supports ( color : color-mix ( in lab , red , red ) ) {
  .bg-brand-500 \/ 50 { background-color : color-mix ( in oklab , var ( --color-brand-500 ) 50 % , transparent ) ; }
}

Keep that hex fallback in mind for section 7.

Trap 1: a dark section that stays light

Semantic tokens need to flip in dark mode, so they point at plain variables:

: root { --surface : oklch ( 0.99 0 0 ) ; --ink : oklch ( 0.21 0.03 265 ) ; }
.dark { --surface : oklch ( 0.21 0.03 265 ) ; --ink : oklch ( 0.97 0 0 ) ; }
@theme { --color-surface : var ( --surface ) ; --color-ink : var ( --ink ) ; }
< div className = "bg-surface text-ink" > Features </ div >
< section className = "dark" >
  < div className = "bg-surface text-ink" > Pricing </ div >
</ section >

The card inside the dark section rendered exactly like the light one:

light-card bg=lab(98.84 0.0000298023 -0.0000119209) color=lab(8.15628 0.58081 -10.86)

dark-card bg=lab(98.84 0.0000298023 -0.0000119209) color=lab(8.15628 0.58081 -10.86)

The compiled CSS shows why:

/* trimmed */
: root , : host { --color-surface : var ( --surface ) ; }
.bg-surface { background-color : var ( --color-surface ) ; }

The var(--surface) inside --color-surface is resolved on :root, where --surface is still the light value. Children inherit that finished value, so redefining --surface on .dark further down changes nothing. It only works when .dark sits on <html> itself, which is why the bug stays hidden until you add a dark footer or an inverted pricing block.

Fix: use @theme inline:

@theme inline { --color-surface : var ( --surface ) ; --color-ink : var ( --ink ) ; }

Now the utility points at the plain variable:

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

light-card bg=lab(98.84 0.0000298023 -0.0000119209) color=lab(8.15628 0.58081 -10.86)

dark-card bg=lab(8.15628 0.58081 -10.86) color=lab(96.52 -0.0000298023 0.0000119209)

The Tailwind docs recommend inline for any theme variable that references another variable. The create-next-app template already does this for --color-background and --color-foreground. Keep the keyword when you add your own.

Trap 2: @theme inside a media query applies everywhere

For system dark mode, I tried this:

@media ( prefers-color-scheme : dark ) { @theme { --color-surface : oklch ( 0.21 0.03 265 ) ; } }

The build passed with no warning. In the output, the media query was gone and the dark value sat in the theme layer:

/* trimmed */
: root , : host { --color-surface : #111826 ; }
@supports ( color : lab ( 0 % 0 0 ) ) {
  : root , : host { --color-surface : lab ( 8.15628 % 0.58081 -10 . 86 ) ; }
}

With the browser set to light mode, the light card got the dark background and kept its dark text:

light-card bg=lab(8.15628 0.58081 -10.86) color=lab(8.15628 0.58081 -10.86)

Dark text on a dark background, in light mode. The Tailwind docs say theme variables must be defined at the top level, not nested under selectors or media queries. In 4.3.5, breaking that rule produced no error. It just changed the output.

Fix: switch the plain variables inside the media query, and keep @theme inline at the top level:

: root { --surface : oklch ( 0.99 0 0 ) ; --ink : oklch ( 0.21 0.03 265 ) ; }
@media ( prefers-color-scheme : dark ) {
  : root { --surface : oklch ( 0.21 0.03 265 ) ; --ink : oklch ( 0.97 0 0 ) ; }
}
.dark { --surface : oklch ( 0.21 0.03 265 ) ; --ink : oklch ( 0.97 0 0 ) ; }
@theme inline { --color-surface : var ( --surface ) ; --color-ink : var ( --ink ) ; }

I ran Playwright with colorScheme: "light" and "dark". The light card followed the system setting, and the nested .dark section was dark in both.

Trap 3: a CSS variable missing from the output

Tailwind only outputs the theme variables your code uses. It scans your source files for full names, so this worked:

< div style = { { backgroundColor : "var(--color-brand-900)" } } />

A name built at runtime doesn't:

const shade = 50 ;
< div style = { { backgroundColor : `var(--color-brand-${shade})` } } />

The background came out as rgba(0, 0, 0, 0), and --color-brand-50 wasn't in the CSS at all.

Two fixes, both tested. Write every name out in full:

const brand = { 50 : "var(--color-brand-50)" , 500 : "var(--color-brand-500)" , 900 : "var(--color-brand-900)" , } as const ;
type Shade = keyof typeof brand ; // 50 | 500 | 900
function Swatch ( { shade } : { shade : Shade } ) { return < div style = { { backgroundColor : brand [ shade ] } } /> ; }

Or tell Tailwind to output every variable in a block with @theme static { ... }. That's useful for tokens you read from JavaScript, but it ships every variable in the block whether you use it or not.

Trap 4: --color-*: initial also removes white and black

To ship only your own colors, the docs show resetting the whole namespace:

@theme { --color- * : initial ; --color-brand-500 : oklch ( 0.55 0.2 265 ) ; }

That removes the default palette, and it also removes bg-white, text-white, bg-black and friends, because --color-white and --color-black live in the same namespace. The white text on my brand block turned black (rgb(0, 0, 0)), and bg-white became transparent. No error: those classes just generate nothing.

Add them back:

@theme { --color- * : initial ; --color-white : #fff ; --color-black : #000 ; --color-brand-500 : oklch ( 0.55 0.2 265 ) ; }

Why DevTools shows lab() and hex instead of your oklch

Every computed color above is lab(...), not oklch(...). The production CSS looked like this:

/* trimmed */
: root , : host { --color-brand-500 : #3665e4 ; }
@supports ( color : lab ( 0 % 0 0 ) ) {
  : root , : host { --color-brand-500 : lab ( 45.4587 % 19.4684 -70 . 6048 ) ; }
}

Turbopack always runs CSS through Lightning CSS, which converts newer CSS features based on your browserslist targets. The Next.js default targets include Firefox 111, while Tailwind CSS 4 itself needs Firefox 128 (plus Chrome 111 and Safari 16.4).

If Tailwind's baseline is fine for you, set it in package.json:

{ "browserslist" : [ "chrome 111" , "edge 111" , "firefox 128" , "safari 16.4" ] }

After a rebuild, the output kept --color-brand-500: oklch(55% 0.2 265); and the computed color was oklch(0.55 0.2 265). This step is optional. The lab() values are the same colors expressed in another color space, and the hex line is only a fallback for browsers without lab(). The browserslist change only makes the output and DevTools match your source.

Wrap-up

Put tokens that need a utility in @theme, plain values in :root. Use @theme inline for any token that points at another variable. Never nest @theme in a media query or selector. Switch the plain variables there instead. Write full var(--color-โ€ฆ) names, or use @theme static. After --color-*: initial, add white and black back. Check computed colors in a production build. A passing build log proves nothing here.

Do you use semantic tokens like surface and ink, or dark: variants on every element? I keep launch checks like these as free, MIT-licensed agent skills for Claude Code, Cursor and Codex: npx skills add nandot12/nextjs-launch-skills ยท GitHub

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.