Sub topic 4
Namespaces: how a variable name picks its utilities
This chapter assumes you can write CSS custom properties, you have used Tailwind utility classes, and you have a stylesheet that starts with @import "tailwindcss";. The previous chapter showed that @theme turns variables into design tokens. Here you will learn the rule that decides which utilities and variants each token produces.
The naming rule
Tailwind does not guess what a theme variable is for. It reads the variable's name. Every theme variable starts with a namespace, a fixed prefix such as --color- or --font-. Each namespace is tied to a family of utilities. The part of the name after the prefix becomes the suffix of the generated class.
@import "tailwindcss";
@theme {
--color-avocado-500: oklch(0.84 0.18 117.33);
}Tailwind splits --color-avocado-500 into the namespace --color-* and the key avocado-500. It then generates every color utility with that key: bg-avocado-500, text-avocado-500, border-avocado-500, fill-avocado-500, ring-avocado-500, and so on. The same value is also usable in from-*, via-*, to-*, decoration-*, and the other color-based utilities.
The key can contain hyphens, so --color-brand-primary-hover gives bg-brand-primary-hover. The key you choose is the name you will type in your HTML for as long as the project lives, so pick it with care.
The main namespaces
The table lists the namespaces you will use most. Each row shows an example variable and what it generates.
| Namespace | Example variable | Generated |
|---|---|---|
| `--color-*` | `--color-brand` | `bg-brand`, `text-brand`, `border-brand` |
| `--font-*` | `--font-display` | `font-display` (font family) |
| `--text-*` | `--text-huge` | `text-huge` (font size) |
| `--font-weight-*` | `--font-weight-heavy` | `font-heavy` |
| `--tracking-*` | `--tracking-tight-2` | `tracking-tight-2` |
| `--leading-*` | `--leading-snug-2` | `leading-snug-2` |
| `--breakpoint-*` | `--breakpoint-3xl` | `3xl:*` variant |
| `--container-*` | `--container-narrow` | `@narrow:*` variant, `max-w-narrow` |
| `--spacing-*` | `--spacing-gutter` | `p-gutter`, `mx-gutter`, `w-gutter`, `gap-gutter` |
| `--radius-*` | `--radius-card` | `rounded-card` |
| `--shadow-*` | `--shadow-glow` | `shadow-glow` |
| `--ease-*` | `--ease-fluid` | `ease-fluid` |
| `--animate-*` | `--animate-fade-in` | `animate-fade-in` |
Two details in the table matter.
- Some namespaces produce variants, not utility classes.
--breakpoint-*creates responsive variants and--container-*creates container query variants. The default breakpointssm,md, andlgare defined the same way. - One namespace can feed several utilities.
--spacing-*powers padding, margin, width, height, gap, inset, and more.
One block, many namespaces
Here is a theme that uses several namespaces together.
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
--font-display: "Satoshi", "sans-serif";
--text-huge: 4.5rem;
--text-huge--line-height: 1;
--breakpoint-3xl: 120rem;
--container-narrow: 40rem;
--spacing-gutter: 1.5rem;
--radius-card: 0.75rem;
--shadow-glow: 0 0 24px oklch(0.84 0.18 117.33 / 0.5);
}And a page that uses what it generated:
<section class="mx-auto max-w-narrow px-gutter 3xl:max-w-none">
<h1 class="font-display text-huge text-brand">Welcome</h1>
<div class="rounded-card p-gutter shadow-glow">Card</div>
</section>Every class in the HTML comes from a variable in the theme block. Without --spacing-gutter, the class px-gutter would produce nothing. Tailwind only emits CSS for classes that match a known key.
Companion variables for font sizes
The line --text-huge--line-height: 1; uses a double hyphen. This pattern sets a related value alongside a token. When you use text-huge, Tailwind sets both font-size and line-height. Without the companion variable, text-huge changes only the size. The same pattern works for --letter-spacing and --font-weight companions on --text-* variables.
Variables outside any namespace
You can put any variable in @theme, even one whose prefix matches no namespace.
@theme {
--header-height: 4rem;
}This still becomes a normal CSS variable, and you can read it with var(--header-height). It generates no utility class, because --header-height is not in a namespace that Tailwind maps to utilities. Use this for tokens that only your own CSS needs.
Choosing between similar namespaces
Some prefixes look alike, and picking the wrong one is a common source of "the class does nothing" bugs.
--font-*is for font families.--font-weight-*is for weights. The name--font-bold: 700would be read as a font family namedbold, so the classfont-boldwould setfont-family: 700. Use--font-weight-bold.--text-*is for font sizes. Text colors come from--color-*, which is whytext-brandworks from--color-brand.--spacing-*controls spacing and sizing. For fractions, Tailwind multiplies a single base value. The next chapters cover--spacingitself.--shadow-*is for box shadows. Drop shadows use--drop-shadow-*, and text shadows use their own namespace.
When a class does not appear to work, open the generated CSS or the browser's styles panel. Look for the variable name first, then check that its prefix is exactly one of the namespaces above.
Namespaces and the default theme
Tailwind ships its own default theme written with the same namespaces. The default --color-red-500, --breakpoint-md, and --radius-lg are in the same blocks you write. This has two consequences.
- If you define a key that already exists, such as
--color-red-500, your value replaces the default one for that key. - If you define a new key in an existing namespace, it is added next to the defaults. For example,
--color-avocado-500sits next to--color-red-500, and both generate utilities.
The next chapters build on this behavior to extend and reset the theme. For now, note that namespaces are what make both operations possible: Tailwind can add or remove a whole family of tokens by prefix.
Verifying what you generated
A quick way to confirm a namespace works is to write the utility in HTML and inspect the result.
<p class="bg-avocado-500 text-avocado-100 font-display">Check me</p>If bg-avocado-500 has no effect, work through this list:
- Is the variable inside
@theme, not only in:root? - Does the name begin with the right namespace prefix, with a hyphen after it?
- Does the HTML use the key exactly, including digits and hyphens?
- Is the CSS file that holds the
@themeblock imported by your build?
Most mistakes come from item 2.
Exercise
You add @theme { --font-semibold: 600; --color-sky-brand: #0af; --radius-pill: 9999px; }. Which utility classes does each variable produce, and which one is a mistake?
Show answer
--color-sky-brand produces bg-sky-brand, text-sky-brand, border-sky-brand and the other color utilities. --radius-pill produces rounded-pill. --font-semibold is the mistake: the --font-* namespace is for font families, so font-semibold would set font-family: 600. The weight variable should be named --font-weight-semibold.
Exercise
You want a 4xl: responsive variant that starts at 160rem, and a utility max-w-article set to 42rem. Write the two theme variables.
Show answer
--breakpoint-4xl: 160rem; creates the 4xl:* variant. --container-article: 42rem; creates max-w-article and also the @article:* container query variant. Both go inside @theme.