Sub topic 6
Extending the default theme
Tailwind CSS v4 ships with a default theme: colors, spacing, fonts, breakpoints, shadows and more, all defined as theme variables. This chapter shows how to add your own tokens next to those defaults and how to change individual defaults without losing the rest.
This chapter assumes you know:
- How
@themedeclares variables, and how a variable's namespace (--color-,--font-,--breakpoint-) decides which utilities it creates. - Basic CSS custom properties and
var(). - How to import Tailwind with
@import "tailwindcss";.
What "extending" means
In v4 every @theme block is merged into the default theme. If you declare a variable that doesn't exist yet, it is added. If you declare one that already exists, your value replaces the default for that one variable only. Every other default stays.
@import "tailwindcss";
@theme {
--color-brand: #3f3cbb;
--color-blue-500: oklch(0.62 0.19 260);
}This block does two things:
--color-brandis new, sobg-brand,text-brand,border-brandand the other color utilities now exist.--color-blue-500already exists, sobg-blue-500uses your value.blue-400,blue-600and every other color are untouched.
Adding new tokens in each namespace
The namespace tells Tailwind what kind of token you are adding. The part after the prefix is the name you use in class names.
@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);
}These variables create the following:
| Variable | Utility or variant it creates |
|---|---|
| `--font-display` | `font-display` |
| `--breakpoint-3xl` | `3xl:` variant, for example `3xl:grid-cols-6` |
| `--color-avocado-500` | `bg-avocado-500`, `text-avocado-500`, `border-avocado-500`, and so on |
| `--ease-fluid` | `ease-fluid` |
You can use them in markup straight away:
<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: variant shows that @theme creates more than utilities. Variants come from the same mechanism.
Font sizes with a line height
The --text-* namespace controls font-size utilities. A companion variable with the --line-height suffix sets the line height that comes with the size.
@theme {
--text-tiny: 0.625rem;
--text-tiny--line-height: 1.5;
}text-tiny then sets both font-size and line-height. The double dash links the second variable to the first. It is not a separate utility.
Overriding a single default
Overriding is the same syntax as adding. You only need to know the default name. Here is the same block applied to a few common defaults:
@theme {
--font-sans: "Inter", ui-sans-serif, system-ui, sans-serif;
--radius-lg: 0.75rem;
--breakpoint-sm: 36rem;
--shadow-md: 0 4px 12px rgb(0 0 0 / 0.12);
}Existing markup picks up the change without edits. Every rounded-lg now uses 0.75rem, and every sm: variant now starts at 36rem.
Overriding is the right tool when you want the same class names with different values. Adding is the right tool when you want new names. A design system often does both: it keeps p-4 and rounded-lg, changes what they mean, and adds bg-brand.
Choosing names that behave well
Names are matched exactly, so a few habits prevent surprises.
- Reuse the default scale shape when replacing a palette. If you add
--color-brand-100to--color-brand-900, you getbg-brand-100and so on. A single--color-brandgives onlybg-brand. - Use one unit for breakpoints. Tailwind sorts breakpoint variants by value. Mixing
pxandremmakes the order hard to predict. Keep the whole set inrem, as the defaults are. - Avoid reusing a default name by accident.
--color-gray-500is already taken. If you want a custom gray, a new name such as--color-slate-inkis safer than silently replacing a default that other code relies on.
Where `@theme` can go
Two rules matter when you extend.
First, @theme must be at the top level of a stylesheet. It cannot be nested inside a selector, an @media rule or another at-rule. This is invalid:
@media (min-width: 64rem) {
@theme {
--color-brand: #3f3cbb;
}
}Tailwind needs to know the whole theme before it generates anything, so a conditional theme makes no sense. Conditional values belong in regular CSS variables, which a later chapter covers.
Second, you can use several @theme blocks, in one file or across imported files. They are merged in order, and a later declaration of the same variable wins.
@import "tailwindcss";
@import "./tokens/colors.css";
@import "./tokens/typography.css";
@theme {
--color-brand: #5b21b6;
}If tokens/colors.css also declares --color-brand, the value in this file takes effect because it comes later.
Extended tokens and the generated CSS
Adding a variable to the theme does not mean it always appears in your compiled CSS. By default, Tailwind emits a theme variable as a CSS variable only when something in your project uses it. If you define --color-avocado-500 and never write a class that needs it, the variable is left out of the output. This keeps the file small, even though the default theme contains hundreds of tokens.
The utilities behave as you would expect:
bg-avocado-500compiles to a rule that readsvar(--color-avocado-500).- Tailwind includes the variable definition in the output because the rule uses it.
If you check the compiled CSS in the browser and a token is missing, first check whether any class actually uses it. The @theme static option, covered in a later chapter, changes this behavior.
Using extended tokens with arbitrary values
An extended token is also an ordinary CSS variable once it is emitted, so you can reference it from a class using the arbitrary value syntax:
<div class="border-(--color-avocado-500) border-2 p-4">
Uses the variable directly
</div>This is rarely needed for tokens that already have a utility. It is useful for tokens in a namespace that Tailwind doesn't map to the property you want.
Common mistakes
- Wrong prefix.
--colour-brandor--brand-colordoes nothing for color utilities. Only the--color-prefix produces them. The variable is still defined, but no utility is generated. - Editing the wrong layer. Changing
:root { --color-blue-500: ... }does not alterbg-blue-500the way@themedoes, because:rootvariables are not part of the theme. The next chapters compare the two. - Expecting a partial override to hide the rest. Overriding
--color-blue-500does not remove other blues. Removing defaults needs a reset, which is the next topic. - Putting
@themeinside a rule. It is ignored or rejected, as shown above. Move it to the top level.
Exercise
Your stylesheet contains @theme { --color-blue-500: #0044cc; --color-brand: #3f3cbb; }. Which utilities change meaning, which are new, and does bg-blue-400 still work?
Show answer
bg-blue-500 (and the other blue-500 color utilities) now use #0044cc, because --color-blue-500 replaces an existing default. bg-brand, text-brand and the other brand color utilities are new, because --color-brand did not exist before. bg-blue-400 still works with its default value, because extending only replaces or adds the variables you declare and keeps the rest of the default theme.
Exercise
You want a 4xl: variant that applies from 150rem and a font-heading utility using the font "Fraunces" with a serif fallback. Write the @theme block.
Show answer
@theme {
--breakpoint-4xl: 150rem;
--font-heading: "Fraunces", serif;
}
The --breakpoint- prefix creates the 4xl: variant, and the --font- prefix creates font-heading. Because 150rem is larger than the default 2xl value of 96rem, the new variant sorts after it.