Sub topic 5
From variable to utility class
This chapter follows one theme variable from the @theme block to the CSS that Tailwind writes. It assumes you know basic CSS custom properties and var(), and that you have used Tailwind utility classes. It also assumes you have a Tailwind v4 project that imports tailwindcss in a CSS file. Earlier chapters covered how @theme and namespaces work. Here you will see how a variable name turns into class names, what ends up in the output file, and why a class sometimes fails to appear.
The naming rule
Tailwind reads every variable in @theme and splits its name in two parts: a namespace and a key.
@import "tailwindcss";
@theme {
--color-avocado-500: oklch(0.84 0.18 117.33);
}- The namespace is
--color-. It tells Tailwind this value is a color. - The key is
avocado-500. It becomes the name you use in classes.
Each utility that accepts colors then gets a version with that key. You can write bg-avocado-500, text-avocado-500, border-avocado-500, fill-avocado-500 and ring-avocado-500. You did not declare any of them. They exist because the namespace says "this is a color".
What each namespace generates
The namespace decides which utilities and variants you get. This table lists the common ones.
| Variable | Namespace | Result |
|---|---|---|
| `--color-brand: #3f3cbb` | `--color-*` | `bg-brand`, `text-brand`, `border-brand`, `fill-brand` and others |
| `--font-display: "Satoshi", sans-serif` | `--font-*` | `font-display` (font family) |
| `--text-huge: 5rem` | `--text-*` | `text-huge` (font size) |
| `--breakpoint-3xl: 120rem` | `--breakpoint-*` | `3xl:` responsive variant |
| `--radius-card: 0.75rem` | `--radius-*` | `rounded-card` |
| `--shadow-soft: 0 2px 8px rgb(0 0 0 / 0.1)` | `--shadow-*` | `shadow-soft` |
| `--ease-fluid: cubic-bezier(0.3, 0, 0, 1)` | `--ease-*` | `ease-fluid` |
| `--animate-fade-in: fade-in 0.3s ease-out` | `--animate-*` | `animate-fade-in` |
The same variable can feed several utilities. A --spacing-* key like --spacing-gutter: 1.5rem produces p-gutter, mx-gutter, gap-gutter, w-gutter and more, because spacing values are accepted by padding, margin, gap and sizing utilities.
A key can contain dashes, and everything after the namespace prefix is the key. --color-brand-primary gives bg-brand-primary. Tailwind does not parse the key any further.
Using the generated classes
With the theme from the topic's first example defined, these classes all work:
@import "tailwindcss";
@theme {
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 120rem;
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}<div class="bg-avocado-500 font-display 3xl:grid-cols-6 grid grid-cols-2">
<p class="text-avocado-100">Themed content</p>
</div>The 3xl: prefix is a variant, not a utility. It wraps any utility in a media query built from the breakpoint value, so 3xl:grid-cols-6 applies at 120rem and wider. Variants such as hover: and dark: combine with theme-based utilities in the usual way, and color utilities accept opacity modifiers like bg-avocado-500/50.
What Tailwind writes to the output
Tailwind produces two pieces of CSS for each class you use: the utility rule and the variable it reads. The output is roughly this (formatting simplified):
@layer theme {
:root, :host {
--color-avocado-500: oklch(0.84 0.18 117.33);
}
}
@layer utilities {
.bg-avocado-500 {
background-color: var(--color-avocado-500);
}
}The utility does not contain the color value. It contains var(--color-avocado-500). This has three consequences:
- You can read the value in the browser's dev tools and override it for one subtree. Setting
--color-avocado-500on an element changesbg-avocado-500for that element and its children. - Your own CSS can use
var(--color-avocado-500), which the later chapter on using theme variables in custom CSS covers. - The value lives in one place, so changing it updates every utility that uses it.
Only used variables are emitted
Tailwind scans your source files for class names. It writes a utility rule only for classes it finds, and it writes a theme variable only when a generated rule or your own CSS uses it. If you define 20 colors and use two, the output contains two color variables.
This keeps the CSS small, but it can surprise you. If you open the built CSS and a variable you defined is missing, no class that needs it was found. The static option (covered later in this tutorial) forces all variables to be written.
Why a class does not appear
Most "my theme variable does not work" problems fall into three groups.
The variable is outside a namespace
Tailwind only builds utilities from variables whose names start with a known namespace. A variable with an unknown prefix is still valid CSS and is still emitted when used, but it creates no class.
@theme {
--brand-primary: #3f3cbb;
}No bg-brand-primary exists here, because --brand- is not a namespace. Rename it to --color-brand-primary.
The class name is built at runtime
Tailwind reads your files as plain text and looks for complete class names. It does not run your code. This template string hides the class from the scanner:
<div class="bg-${tone}-500">Not detected</div>Write the full names so they appear somewhere in the source, or map a value to a complete class:
<div class="bg-avocado-500">Detected</div>The namespace does not match the utility
A color variable will not give you a font size. If text-huge does nothing, check that you defined --text-huge and not --font-huge or --color-huge. Note that text- is shared. With --color-* it sets the text color, and with --text-* it sets the font size. Tailwind resolves which one applies from the namespace of the key you used.
Spacing is computed
Most namespaces map a key to one value. Spacing works differently. The core --spacing variable is a base unit, and numeric utilities multiply it:
.p-4 {
padding: calc(var(--spacing) * 4);
}This is why p-13 works without a --spacing-13 variable. A named key such as --spacing-gutter creates a fixed value instead of a multiple. Both styles can coexist. The spacing scale gets its own chapter, so keep this point only as a contrast with the one-variable, one-value pattern above.
A quick way to check your work
When a utility seems missing, work through these checks in order:
- Open the generated CSS and search for the class name, for example
.bg-avocado-500. - If the rule is missing, check that the full class name appears in a scanned file and is not assembled from pieces.
- If the rule exists but has the wrong value, check the key spelling and which namespace you used.
- If the rule is correct but has no effect, inspect the element and look for a later rule or an overriding variable.
- If the variable is missing from
@layer theme, no used class or CSS references it.
Exercise
You add --brand-accent: #ff5a1f; inside @theme, then write <button class="bg-brand-accent">. The button has no background. Give two changes that fix it, and explain why the original did not work.
Show answer
The prefix --brand- is not a theme namespace, so Tailwind builds no utilities from it. Rename the variable to --color-brand-accent and keep the class bg-brand-accent. That puts the variable in the --color-* namespace, and the key brand-accent becomes the class suffix. Alternatively, rename it to --color-accent and change the class to bg-accent, which uses the same namespace with a shorter key. Either way the variable name must start with --color- for bg- to work.
Exercise
You define ten colors in @theme but your built CSS only contains three of the matching variables. Is this a bug? What would you check?
Show answer
No, this is the expected behavior. Tailwind emits a theme variable only when a generated utility or your own CSS uses it. Check that your markup contains the full class names of the other seven colors, such as bg-avocado-500. If you need every variable in the output regardless of use, use the static option on @theme.