Sub topic 5
From variable to utility class
This chapter assumes you know basic CSS custom properties and var(). It also assumes you have used Tailwind utility classes in HTML and can import Tailwind into a CSS file with @import "tailwindcss";.
What @theme does
A variable declared inside @theme is a normal CSS custom property. Tailwind also reads its name and decides which utility classes and variants to generate. The name has two parts: a namespace prefix, then a key. In --color-avocado-500, the namespace is --color- and the key is avocado-500.
@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);
}Each line produces something different:
| Variable | Generated |
|---|---|
| `--font-display` | `font-display` utility |
| `--breakpoint-3xl` | `3xl:*` variant |
| `--color-avocado-500` | `bg-avocado-500`, `text-avocado-500`, `border-avocado-500` and other color utilities |
| `--ease-fluid` | `ease-fluid` utility |
Using the generated classes
These classes work only after the matching variables exist. Without the @theme block above, Tailwind would ignore bg-avocado-500 and emit no CSS for it.
<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:grid-cols-6 class applies six columns once the viewport is at least 120rem wide. The key 3xl in the variable name becomes the variant name.
Utilities appear only when used
Tailwind generates CSS for the classes it finds in your source files. Theme variables follow the same rule: by default, a variable is written to the output only if some generated utility or your own CSS uses it. Defining twenty colors you never use does not add twenty variables to the stylesheet. A later chapter covers @theme static, which changes this.
Common errors
- Wrong namespace.
--brand-main: #3f3cbb;creates a variable but no utility, because--brand-is not a known namespace. Use--color-brand-main. - Typo in the key. The class must match the key exactly.
bg-avocado-500needs--color-avocado-500, not--color-avacado-500. - Defining variables outside
@theme. A variable in:rootdoes not generate utilities. Only@themedoes. - Expecting a variable to appear in the browser. If nothing uses it, it may be missing from the output.
Exercise
You add --color-sunset: #ff6b35; to @theme. Which class can you now use for a background, and what would you name the variable if you wanted a 4xl: variant instead?
Show answer
You can use bg-sunset. For a 4xl: variant, you would define a variable in the breakpoint namespace, such as --breakpoint-4xl: 150rem;.
Exercise
A teammate writes --brand-blue: #1d4ed8; inside @theme and uses bg-brand-blue, but nothing happens. What is wrong, and how do you fix it?
Show answer
--brand- is not a theme namespace, so Tailwind generates no utility. Rename it to --color-brand-blue. Then bg-brand-blue works.