Sub topic 1
Theme Variables with @theme in Tailwind CSS
Prerequisites
This chapter assumes basic CSS, including custom properties and var(), and familiarity with Tailwind utility classes. You should also know how to import Tailwind from a CSS file.
Refresher: CSS custom properties
A CSS custom property is a named value that you can reuse. Its name starts with --. Define it on an element, often :root, then read it with var().
:root {
--brand-color: oklch(0.55 0.2 250);
--space-unit: 0.25rem;
}
.button {
background: var(--brand-color);
padding: calc(var(--space-unit) * 4);
}Custom properties inherit by default. A child element can use a value defined on an ancestor, and a more specific rule can override it.
Configuring Tailwind in CSS
Tailwind CSS v4 uses CSS-first configuration. Import Tailwind in your main stylesheet:
@import "tailwindcss";You can then configure design tokens in that same file with the @theme directive.
Defining design tokens with `@theme`
A design token is a named design decision, such as a color, font, spacing value, or breakpoint. Variables inside @theme are special: Tailwind uses them to generate utilities and variants.
@import "tailwindcss";
@theme {
--color-avocado-500: oklch(0.84 0.18 117.33);
--font-display: "Satoshi", sans-serif;
--breakpoint-3xl: 120rem;
}The values are CSS custom properties, but their namespaces tell Tailwind what they represent.
Namespaces: how a variable name picks its utilities
The part after -- identifies a namespace. For example, --color-* creates color utilities, while --font-* creates font-family utilities.
| Theme variable | Generated utility or variant |
|---|---|
| `--color-avocado-500` | `bg-avocado-500`, `text-avocado-500` |
| `--font-display` | `font-display` |
| `--breakpoint-3xl` | `3xl:` |
Common namespaces include --spacing-*, --radius-*, --shadow-*, and --ease-*.
From variable to utility class
Once the matching variable exists, use the generated class in HTML:
<div class="grid grid-cols-2 3xl:grid-cols-6 bg-avocado-500 font-display">
Themed content
</div>Tailwind detects the classes and emits the CSS needed for them. A class such as 3xl:grid-cols-6 works because --breakpoint-3xl defines the 3xl variant.
Exercise
Which utility does --font-display: "Satoshi", sans-serif enable?
Show answer
It enables the font-display utility.