Turning a Brand Palette Into a Tailwind v4 @theme and tokens.css File
Developer tokens · Updated 2026-11-24
Tailwind CSS v4 replaces JavaScript configuration files with CSS-first @theme blocks. Declaring --color-brand-primary and --font-heading inside @theme automatically generates utility classes like bg-brand-primary, text-brand-ink, and font-heading with zero extra config.
How Tailwind v4 @theme maps CSS variables to utilities
In Tailwind v3, adding custom brand tokens required editing tailwind.config.js and mirroring values manually if plain CSS files also needed them. Tailwind v4 reads CSS custom properties inside an @theme block directly and emits both the CSS variable and matching utility classes.
Prefixes decide which utilities Tailwind creates. Variables starting with --color-* generate text-, bg-, border-, and ring- classes; variables starting with --font-* generate font-* classes; variables starting with --radius-* generate rounded-* classes.
Drop-in tailwind-brand.css for Tailwind v4
@import "tailwindcss";
@theme {
--color-brand-primary: #1d4ed8;
--color-brand-secondary: #334155;
--color-brand-accent: #0f766e;
--color-brand-surface: #f4f6fa;
--color-brand-ink: #0f172a;
--font-heading: ui-sans-serif, system-ui, -apple-system, sans-serif;
--font-sans: ui-sans-serif, system-ui, -apple-system, sans-serif;
--font-mono: ui-monospace, SFMono-Regular, Menlo, monospace;
}When to pair @theme with a plain tokens.css file
Many teams run a marketing site in Astro or plain CSS alongside an application in Next.js and Tailwind v4. Keeping a standard :root tokens.css file in your brand kit ensures email templates, printable invoice views, and vanilla CSS modules share identical variable names.
Because Tailwind v4 exposes @theme variables on :root automatically, plain CSS rules can also reference var(--color-brand-primary) without duplicating hex strings across multiple files.
| Feature | tokens.css (:root) | tailwind-brand.css (@theme) |
|---|---|---|
| Works without build step | Yes, native browser CSS | Requires Tailwind CSS v4 compiler |
| Generates utility classes | No, use var(--brand-...) | Yes (bg-brand-primary, text-brand-ink) |
| Supports dark mode overrides | Yes via @media (prefers-color-scheme) | Yes by reassigning variables in :root |
| Includes 100-900 tonal ramp | Yes (--brand-primary-100..900) | Yes (bg-brand-primary-100..900) |
Handling dark mode cleanly in CSS-first Tailwind
Avoid littering every HTML element with paired dark:bg-... and dark:text-... utility classes when your semantic tokens can swap automatically. Define default light values inside @theme, then override --color-brand-surface, --color-brand-ink, and --color-brand-primary inside a @media (prefers-color-scheme: dark) block on :root.
With semantic token overrides in place, a button styled with bg-brand-primary and a canvas styled with bg-brand-surface adapt to dark mode automatically across every component while preserving WCAG 2.2 AA contrast.
Remember to pair --color-brand-primary with a dedicated --color-brand-on-primary text variable so white light-mode button text switches cleanly to dark #0a1317 ink when your dark-mode primary lightens to a soft pastel tint.
Keeping initial CSS small for Lighthouse 100
System font stacks (--font-sans and --font-mono) ship zero kilobytes over the network and trigger zero layout shift on first paint. Avoid importing four Google Font weights inside your Tailwind entry CSS unless your brand strictly requires a self-hosted display typeface.
Limit custom brand colors to your five semantic roles plus 9-step primary and accent ramps so unused utility classes are tree-shaken cleanly during production builds.
Migrating an older Tailwind v3 brand config to CSS-first @theme
Moving an existing project from Tailwind v3 to Tailwind v4 takes less than ten minutes when your brand tokens are already organized by semantic role. Open your old tailwind.config.js file, locate theme.extend.colors and theme.extend.fontFamily, and translate each entry into a --color-* or --font-* CSS custom property inside @theme.
For example, colors.brand.primary: '#1d4ed8' becomes --color-brand-primary: #1d4ed8 inside tailwind-brand.css. Existing markup using bg-brand-primary, text-brand-primary, and border-brand-primary continues working without renaming a single class in your components.
Next, remove hardcoded gray borders and arbitrary hex brackets like bg-[#1d4ed8] across your JSX templates. Replacing one-off bracket values with semantic brand utilities ensures that future palette refinements or dark-mode adjustments propagate everywhere in one edit.
Finally, pair your tailwind-brand.css theme file with a short 20-line token reference inside AGENTS-UI.md or your project README. Listing exact utility pairs such as bg-brand-primary text-white and bg-brand-surface text-brand-ink keeps both new developers and AI coding assistants from falling back to stock Tailwind blue or slate classes during rapid feature builds.
- Import tailwind-brand.css once in your global stylesheet right after @import 'tailwindcss'.
- Keep raw tonal ramps (--color-brand-primary-100 through -900) for subtle hover tints and dark-mode link highlights.
- Run a contrast audit on both light and dark :root overrides before shipping the migrated stylesheet.
Put these specifications to work immediately: Generate Tailwind v4 @theme and tokens.css.
Frequently asked questions
Does Tailwind v4 still need tailwind.config.js for brand colors?
No. Importing a CSS file with an @theme block registers every custom color and font token directly inside CSS.
Can I use OKLCH values inside Tailwind v4 @theme?
Yes. Tailwind v4 natively supports both HEX and OKLCH color definitions inside @theme custom properties.
How do I prevent accidental off-brand Tailwind colors in PRs?
Document your exact --color-brand-* class names in AGENTS-UI.md and review diffs for stray default utility colors like bg-indigo-500.