Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readCSS

Define Tailwind v4 tokens with @theme

Move a custom color and display font from Tailwind’s JavaScript config into CSS and keep using the same utility classes.

Your Tailwind config extends the palette with a brand color and adds a display font. The project now builds with v4, and you want to change those values in the stylesheet while keeping the class names your markup uses.

Move each value into its namespace

Put the custom values in a top-level @theme block. The namespace tells Tailwind which utilities to generate. Map the old theme.extend.colors.lagoon entry to --color-lagoon and the theme.extend.fontFamily.display entry to --font-display.

Keep the main stylesheet wired into the build. Add these values next to its Tailwind import.

@import "tailwindcss";

@theme {
  --color-lagoon: oklch(0.43 0.08 195);
  --font-display: Georgia, serif;
}

The build can now generate color utilities from lagoon and the font-display utility from the font variable.

<h1 class="bg-lagoon p-4 font-display text-white">Trail conditions</h1>

The heading gets the lagoon background and Georgia, with white text and the default padding utility.

The import keeps the default theme. There is no need to reset the color namespace to add this one value. A reset with --color-*: initial would remove the default colors from the theme, including the one behind text-white unless you restored it.

Keep ordinary variables for local CSS

Use an ordinary custom property when you want a value available to a CSS rule without adding utility names. Putting --color-lagoon in :root alone would give you a variable for var(--color-lagoon), but Tailwind would have no lagoon entry in its utility namespace.

After the move, inspect the built stylesheet for .bg-lagoon and .font-display. If a class is missing, check both the variable prefix and whether Tailwind can see the full class name in your templates.

Keep @config while the JS file supplies settings you still use. After migrating those settings, remove the reference and rebuild to check that the old config has no remaining callers.