Sub topic 6
Extending the default theme
This chapter assumes you know basic CSS custom properties and var(), you have used Tailwind utility classes in HTML, and your project already imports Tailwind in a CSS file with @import "tailwindcss";.
What "extending" means
Tailwind v4 ships with a default theme. It defines variables such as --color-red-500, --font-sans, and --breakpoint-md. When you write a new variable inside @theme, Tailwind adds it to the default theme. Nothing is removed.
So a new --color-avocado-500 sits next to --color-red-500. Both bg-avocado-500 and bg-red-500 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);
}Each name starts with a namespace: --font-, --breakpoint-, --color-, --ease-. The namespace decides which utilities Tailwind generates. The rest of the name becomes the part of the class after the utility prefix.
Using the new tokens
Once the variables exist, the classes work like any built-in class.
<div class="bg-avocado-500 font-display 3xl:grid-cols-6 grid grid-cols-2">
<p class="text-avocado-100">Themed content</p>
</div>Here is where each class comes from:
| Class | Source variable |
|---|---|
| `bg-avocado-500` | `--color-avocado-500` |
| `text-avocado-100` | `--color-avocado-100` |
| `font-display` | `--font-display` |
| `3xl:grid-cols-6` | `--breakpoint-3xl` |
--breakpoint-3xl creates a variant, not a utility. The 3xl: prefix applies the class only when the viewport is at least 120rem wide.
Overriding a default value
If you reuse an existing name, your value replaces the default for that name only. For example, setting --color-red-500 changes bg-red-500 and leaves every other color alone.
@theme {
--color-red-500: oklch(0.6 0.25 25);
}Use this when you want a small tweak. If you want to remove whole groups of defaults, that is a different step, covered in the next chapter.
Common errors
- Wrong namespace.
--brand-main: #3f3cbb;creates a variable but no utility, because--brand-is not a theme namespace. Use--color-brand-main. - Putting tokens in
:root. A variable in:rootis a plain CSS variable. Tailwind does not generatebg-*classes from it. Use@themewhen you want utilities. - Nesting
@themeinside a selector or media query.@thememust be at the top level of the stylesheet. - Forgetting the import.
@themeis processed only in a file that Tailwind builds, so keep it after@import "tailwindcss";.
Exercise
You add --color-sunset: #ff7a45; and --shadow-glow: 0 0 12px #ff7a45; to @theme. Which two classes can you now use, and do the default colors such as bg-blue-500 still work?
Show answer
You can use bg-sunset (from the --color- namespace) and shadow-glow (from the --shadow- namespace). Yes, bg-blue-500 still works, because @theme extends the default theme and does not replace it.
Exercise
A teammate writes :root { --color-lime-brand: #9be000; } and then uses bg-lime-brand. The class has no effect. Why, and what is the fix?
Show answer
Variables in :root are regular CSS variables, and Tailwind does not turn them into utilities. Move the variable into an @theme { ... } block so the --color- namespace generates bg-lime-brand.