Sub topic 1
Refresher: CSS custom properties
Tailwind CSS v4 stores its design tokens as CSS custom properties. Colors, fonts, breakpoints and spacing all end up as variables in the generated stylesheet. Before using @theme, you need a firm picture of how those variables behave in the browser. This chapter covers the parts of custom properties that matter for the rest of the tutorial.
Prerequisites
This chapter assumes you already have the following:
- Basic CSS: selectors, the cascade, and how declarations are written.
- Experience using Tailwind utility classes in HTML, such as
p-4orbg-white. - A rough idea of how Tailwind is installed and imported in a CSS file with
@import "tailwindcss";.
You do not need to have written any @theme rules yet. That starts in the next chapter.
Declaring and reading a custom property
A custom property is a CSS property whose name you choose. The name must start with two dashes. You read its value with the var() function.
:root {
--brand: #3f3cbb;
--gap: 1rem;
}
.button {
background: var(--brand);
padding: var(--gap);
}Three points are worth keeping in mind:
- Names are case-sensitive.
--Brandand--brandare different properties. - A custom property can hold almost any token sequence. It can be a color, a length, a list, or a font stack.
- The browser does not check the value when you declare it. It only checks it when a normal property uses it through
var().
Because --brand is a real property, you can inspect it in the browser's developer tools and change it live. That is the main advantage over preprocessor variables such as Sass $brand. Sass variables vanish at build time, while custom properties remain in the final CSS.
Fallback values
var() accepts a second argument. The browser uses it when the variable is not defined.
.badge {
color: var(--badge-color, #555);
border: 1px solid var(--badge-border, currentColor);
}The fallback applies only when the variable is missing. If the variable exists but holds a value that is invalid for the property, the fallback is not used. Browsers handle that case differently, as the section on invalid values below explains.
You can also nest var() calls to build a chain of fallbacks:
.badge {
color: var(--badge-color, var(--brand, #555));
}Scope and inheritance
Custom properties inherit by default. A value declared on an element is available to that element and all of its descendants. This is why :root is the usual place for global values. It matches the <html> element, which is the ancestor of everything on the page.
A descendant can redeclare the same name. Its own subtree then sees the new value, and the rest of the page keeps the old one.
<style>
:root {
--brand: #3f3cbb;
}
.promo {
--brand: #d9480f;
}
.link {
color: var(--brand);
}
</style>
<a class="link" href="/">Normal link</a>
<section class="promo">
<a class="link" href="/sale">Promo link</a>
</section>Both links use the same .link rule. The first resolves --brand to #3f3cbb. The second sits inside .promo, so it resolves to #d9480f.
Resolution happens at computed-value time
The browser resolves var() when it computes styles for each element, not when it parses the file. This has two consequences.
First, one rule can produce different results in different places, as in the example above. Second, changing a variable on an element updates everything that reads it, with no extra CSS. Dark mode depends on this behavior, and a later chapter returns to it.
The cascade applies to custom properties just like other properties. Specificity, source order and !important all decide which declaration wins on a given element.
Variables that reference variables
A custom property value can contain var(). The reference is resolved on the element where the property is declared.
:root {
--space-base: 0.25rem;
--space-4: calc(var(--space-base) * 4);
}This is how Tailwind v4 builds its spacing scale. A single base value is multiplied to produce each step. A later chapter on the --spacing base value covers it in detail.
There is one trap here. If a variable references another variable, the reference is resolved where the first variable is declared, not where it is used.
:root {
--accent: blue;
--button-bg: var(--accent);
}
.theme-warm {
--accent: orange;
}
.button {
background: var(--button-bg);
}Inside .theme-warm, .button is still blue. --button-bg was computed on :root as blue, and descendants inherit that computed result. Overriding --accent further down does not change it. To get orange, you would also have to redeclare --button-bg inside .theme-warm.
This trap is the reason @theme inline exists in Tailwind v4. It is covered later in this tutorial.
Invalid values
If var() produces something that is not valid for the property using it, the declaration becomes invalid at computed-value time. The property then falls back to its inherited or initial value. It does not fall back to a previous rule in your stylesheet.
:root {
--size: blue;
}
.box {
width: 200px;
width: var(--size);
}Here width resolves to auto, which is its initial value. The 200px declaration is lost, because the browser accepted var(--size) at parse time and only found the problem later. When a layout breaks and a variable is involved, check the computed value in the developer tools first.
Using variables with calc() and units
A variable holding a bare number is useful for multiplication.
:root {
--columns: 3;
--gutter: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(var(--columns), 1fr);
gap: var(--gutter);
padding: calc(var(--gutter) * 2);
}You cannot attach a unit afterwards by writing var(--gutter)px. The browser sees two tokens, not one value. Either store the unit in the variable, or multiply inside calc(), for example calc(var(--n) * 1px).
Changing variables with JavaScript
Because custom properties live in the style system, scripts can read and write them. This is how many runtime theme switchers work.
const root = document.documentElement;
root.style.setProperty("--brand", "#d9480f");
const value = getComputedStyle(root).getPropertyValue("--brand").trim();
console.log(value); // "#d9480f"getPropertyValue returns the raw text, often with leading whitespace, so call trim() before comparing.
Registered properties
By default, custom properties are untyped strings, so the browser cannot animate them smoothly. The @property rule registers a type, an initial value, and whether the property inherits.
@property --angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}You will rarely need this for basic theming. Tailwind v4 uses @property internally for some utilities, such as gradients and shadows, so you may see it in generated output.
Why this matters for Tailwind v4
The table below maps each behavior in this chapter to what you will meet later.
| Behavior | Where it appears in Tailwind v4 |
|---|---|
| Variables remain in the output CSS | Theme values can be read with `var()` in your own CSS |
| Inheritance and scope | Dark mode by redeclaring variables under a class |
| Variables referencing variables | The spacing scale, and the `inline` option on `@theme` |
| Resolution where declared | The reason `@theme inline` exists |
| Names with a prefix such as `--color-` | Namespaces that choose which utilities are generated |
When you write bg-brand in Tailwind v4, the generated CSS reads a variable such as var(--color-brand). Every behavior above applies to that variable.
Exercises
Exercise
Given the CSS below, what color is the link inside .sidebar, and why?
:root { --accent: blue; --link: var(--accent); }
.sidebar { --accent: green; }
a { color: var(--link); }
Show answer
The link is blue. --link is resolved on :root, where --accent is blue, and descendants inherit that computed result. Redeclaring --accent on .sidebar does not change --link. To get green, also redeclare --link: var(--accent) inside .sidebar.
Exercise
A rule sets width: 200px; followed by width: var(--size);, and --size is blue. What is the computed width, and why does the first declaration not rescue it?
Show answer
The computed width is auto, which is the initial value for width. The browser accepted var(--size) at parse time, so it discarded the earlier 200px declaration. The value only turns out to be invalid at computed-value time, and then the property uses its inherited or initial value.