Tailwind CSS v4 @theme: The Complete Guide
October 1, 2026 | 13 min read | Tailwind CSS
By Syed · Frontend Engineer & Creator of UITweak
Tailwind CSS v4 moved theming out of JavaScript config files and into CSS itself. The @theme directive is where your design system now lives: colors, fonts, spacing, animations, all defined as CSS variables that automatically become utilities. This CSS-first shift is also why many teams are re-evaluating Sass itself, see our honest nesting vs Sass breakdown. If you are still writing tailwind.config.js the v3 way, this guide will get you fluent in the new model.
Shortcut: build your v4 palette visually in our free Tailwind Config Generator, then paste the values into an @theme block as shown below.
1. The One Big Change: Theme Lives in CSS
In v3, customization lived in tailwind.config.js. In v4, you import Tailwind in your CSS and define tokens with @theme. Every --color-*, --font-*, or --spacing-* variable you declare becomes a working utility class automatically:
@import "tailwindcss";
@theme {
--color-brand-500: oklch(0.62 0.19 250);
--color-brand-700: oklch(0.45 0.16 250);
--font-display: "Sora", sans-serif;
}That is the whole setup. bg-brand-500, text-brand-700, and font-display now work everywhere in your project. No config file, no rebuild plugins, no restart.
2. How Tokens Become Utilities
The namespace prefix decides which utilities a variable unlocks. This is the core mental model of v4 theming:
@theme {
--color-mint-500: oklch(0.72 0.11 178); /* bg-mint-500, text-mint-500, border-mint-500... */
--font-display: "Sora", sans-serif; /* font-display */
--spacing-card: 1.75rem; /* p-card, m-card, gap-card... */
--radius-card: 1.25rem; /* rounded-card */
--shadow-card: 0 8px 30px rgb(0 0 0 / 0.08); /* shadow-card */
--breakpoint-xs: 30rem; /* xs: variant */
}- --color-*: unlocks background, text, border, ring, gradient, and every other color utility.
- --font-*: unlocks
font-*utilities. - --spacing-*: plugs into the spacing scale used by padding, margin, and gap.
- --animate-*: creates animation utilities (see section 4).
3. Overriding the Defaults
Redefining a default token replaces it. Want your own blue scale or a different base font? Just declare it:
@theme {
/* Replace Tailwind's default blue-500 everywhere */
--color-blue-500: oklch(0.6 0.2 255);
/* Replace the default sans stack */
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
}This is also how you trim the framework: tokens you never reference generate no CSS, so a tight custom theme ships less code than the full default palette.
4. Custom Animations in @theme
Animations are theme tokens too. Define --animate-* and put the keyframes inside the @theme block so Tailwind includes them in the output:
@theme {
--animate-fade-up: fade-up 0.6s ease-out both;
@keyframes fade-up {
from { opacity: 0; transform: translateY(12px); }
to { opacity: 1; transform: translateY(0); }
}
}
/* Usage: <div class="animate-fade-up">...</div> */}
Keyframes defined inside @theme are emitted only when the animation utility is actually used, keeping unused motion out of your bundle.
5. Dark Mode in v4
v4 defaults to the prefers-color-scheme media query for the dark: variant. If you want class-based toggling like v3 had, add one line with @custom-variant:
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
@theme {
--color-surface: oklch(1 0 0);
--color-surface-dark: oklch(0.21 0.02 260);
}Now dark:bg-surface-dark responds to a .dark class on a parent, exactly like the v3 behavior teams are used to.
6. Referencing Tokens in Your Own CSS
Theme variables are real CSS custom properties, so your hand-written CSS can use them directly. The @theme inline option even lets you compute values from other tokens:
.btn-primary {
background: var(--color-brand-500);
border-radius: var(--radius-card);
}
@theme inline {
/* Computed at build time from another token */
--color-brand-soft: color-mix(in oklch, var(--color-brand-500) 12%, white);
}7. v3 Config to v4 @theme: Migration Map
Moving an existing project? Here is where each v3 config section goes:
theme.extend.colorsbecomes@theme { --color-* }theme.extend.fontFamilybecomes@theme { --font-* }theme.extend.spacingbecomes@theme { --spacing-* }theme.extend.keyframes / animationbecomes@theme { --animate-* }with keyframes insidedarkMode: "class"becomes@custom-variant darkas shown abovepluginsbecome plain CSS,@utility, or@variantdefinitions
8. Common Mistakes
- Forgetting the namespace:
--brand-500does nothing. It must be--color-brand-500to unlock color utilities. - Keyframes outside @theme: put them inside, or Tailwind may tree-shake them away.
- Mixing v3 config and v4 theme: a legacy
tailwind.config.jsstill loads in compatibility mode and can silently override your @theme tokens. Migrate fully or remove it. - Unlayered custom CSS beating utilities: in v4 everything lives in cascade layers, so your own unlayered styles silently win over utilities. Our cascade trap fix explains the debugging story.
- Opacity modifiers doing nothing:
bg-brand/50silently fails when the token holds a barevar(). The opacity modifier fix shows why and how to repair it.
Prototype your palette and spacing in our free Tailwind Config Generator and paste the values straight into your @theme block. For layout tokens, the Tailwind Grid Builder pairs well with a custom spacing scale.
9. @theme vs @theme inline: When Each Wins
One of the most asked questions about Tailwind v4 theming: what is the difference between @theme and @theme inline? Both define tokens, but they compile differently:
- @theme (default): tokens become real CSS variables on
:root. Utilities reference the variable, so you can override them at runtime (dark mode, user themes). - @theme inline: token values are inlined directly into utilities at build time. Smaller CSS, but no runtime overrides, and
var()references resolve statically.
Rule of thumb: use plain @theme for colors and anything you might theme at runtime; use @theme inline for tokens that are truly static, like font stacks or fixed radii, to shave bytes.
# Expansion draft: tailwind-v4-theme-guide
The Dynamic Spacing Scale: Why p-17 Just Works Now
In v3, the spacing scale was a fixed list. If you wanted p-17, you extended the config. In v4, spacing utilities are derived mathematically from a single token, --spacing, which defaults to 0.25rem. Any numeric utility multiplies that base: p-17 is 17 * 0.25rem, mt-21 works, w-29 works. No configuration needed.
This changes two habits. First, stop extending the spacing scale for one-off values. The v3 instinct of adding spacing: { 17: '4.25rem' } to the config is dead weight in v4; the utility already exists. Second, treat --spacing itself as the design decision. If your design system uses an 8px base grid instead of 4px, one line changes every spacing utility in the project:
@theme {
--spacing: 0.5rem; /* every p-*, m-*, gap-* now on an 8px grid */
}That is powerful and slightly dangerous: it rescales the entire site. I have seen a well-meaning --spacing tweak shift every layout in an app by a few pixels. Change it deliberately, once, with a visual review. Because spacing is computed, not looked up, there is no list of "valid" values to memorize. If the number times the base gives the size you want, the utility exists.
Multi-Brand Theming: Swapping Tokens Instead of Classes
The dark-mode section of the main guide shows redefining tokens under .dark. The same mechanism themes entire brands. If you ship a white-label product or a marketing site with per-client skins, you do not need per-brand class variants. You need token scopes:
@theme {
--color-brand-500: oklch(0.62 0.19 250); /* default brand */
}
.brand-acme {
--color-brand-500: oklch(0.65 0.22 30); /* Acme's brand */
}
.brand-globex {
--color-brand-500: oklch(0.55 0.18 160); /* Globex's brand */
}Markup stays bg-brand-500 everywhere. Wrapping the app in .brand-acme re-skins it. The mechanism is the same cascade the dark-mode pattern uses: @theme emits real custom properties on :root, and a more specific selector redefines them for its subtree.
In practice I keep brand tokens in a separate stylesheet from the default theme, one file per brand. That keeps the default theme readable and makes it obvious which tokens a brand overrides. One limit: this only re-skins what goes through tokens. Hardcoded values in arbitrary properties (bg-[#3b5bdb]) and inline styles will not follow the brand, so route colors through tokens from the start if you plan to white-label later. Retrofitting is tedious.
Debugging Walkthrough: The Token Exists but the Utility Doesn't
Section 8 of the main guide lists common mistakes. This is the order I check them in when a token is defined but its utility generates nothing, because the checks go from most to least likely:
**Check 1: the namespace.** Open the @theme block and read the variable name character by character. --brand-500 does nothing; it must be --color-brand-500. The compiler will not warn you. An unknown namespace is silently ignored.
**Check 2: the file is processed.** Is this @theme block in a stylesheet Tailwind actually processes? A @theme block in a CSS module that is never imported, or in a file outside the Vite/PostCSS pipeline, defines nothing. The @apply guide's Fix 5 covers the same structural issue. Confirm the file is in the import chain from your Tailwind entry.
**Check 3: legacy config interference.** If a tailwind.config.js still exists, v4 loads it in compatibility mode and its values can silently override @theme tokens. When a token "should" work and does not, and checks 1 and 2 pass, the legacy config is the prime suspect. Migrate it or delete it. Keeping it around "just in case" is how this bug is born.
**Check 4: @theme inline versus plain.** If the token was defined with @theme inline, its value is baked into utilities at build time and there is no runtime variable. Code that reads var(--color-brand-500) in hand-written CSS will find nothing. Either switch to plain @theme or stop referencing the variable directly. This one bites when two developers set up the theme differently in different files.
Run the checks in order and you will find it. Every "my token does nothing" case I have debugged landed in one of these four.
FAQ
How do you add custom colors in Tailwind CSS v4 without a config file?
Define them in CSS with @theme: @theme { --color-brand-500: #3b5bdb; } inside your stylesheet after @import "tailwindcss". That unlocks bg-brand-500, text-brand-500, and every other color utility automatically. No JavaScript config needed.
What is the difference between @theme and @theme inline in Tailwind v4?
Plain @theme emits real CSS variables you can override at runtime (for dark mode or user themes). @theme inline bakes values directly into utilities at build time for smaller CSS, but you lose runtime overrides. See section 9 above for the full comparison.
How do you define color shades like 100 to 900 in Tailwind v4?
Declare each step as its own token: --color-brand-100 through --color-brand-900 inside @theme. Tailwind maps the numeric suffix to utilities like bg-brand-100 automatically. Generate the scale with a palette tool, then paste all nine lines.
Can I still use tailwind.config.js for colors in Tailwind v4?
Yes, v4 loads a legacy config in compatibility mode, but it can silently override your @theme tokens and confuse debugging. Migrate colors to @theme (section 7 has the mapping) and delete the config file for a clean setup.
How do you implement dark mode with custom colors in Tailwind v4?
Add @custom-variant dark (&:where(.dark, .dark *)); to your CSS, toggle the dark class on <html>, then use dark: variants with your custom tokens: bg-brand-500 dark:bg-brand-400. Because @theme tokens are real variables, you can also redefine them inside .dark directly.