Sub topic 1
Refresher: CSS custom properties
Prerequisites
This chapter assumes you know basic CSS selectors and properties. You should have used Tailwind utility classes in HTML. You should also know that Tailwind is imported into a CSS file with @import "tailwindcss";. Nothing else is required. This chapter covers the CSS feature that @theme builds on.
What a custom property is
A custom property is a CSS name that starts with two dashes, such as --brand. You give it a value in a rule. You read it later with the var() function.
:root {
--brand: #3f3cbb;
--gap: 1rem;
}
.button {
background: var(--brand);
padding: var(--gap);
}Browsers resolve these values at runtime, not at build time. That is the main difference from variables in a preprocessor such as Sass. Tailwind v4 relies on this behavior.
Scope and inheritance
A custom property follows the cascade and is inherited, like color or font-family. An element sees the value set on itself or on the nearest ancestor that sets it.
:root matches the <html> element, so a variable defined there is available everywhere. A variable defined on a narrower selector applies only inside that element.
:root {
--surface: white;
}
.dark {
--surface: #111;
}
.panel {
background: var(--surface);
}A .panel inside an element with the class dark gets #111. Any other .panel gets white. The rule for .panel never changes. Only the inherited value does. Dark mode with variables works this way, and a later chapter returns to it.
Fallback values
var() accepts a second argument. The browser uses it when the variable is not defined.
.note {
color: var(--note-color, #444);
}The fallback applies only when the variable is missing. If the variable exists but holds an invalid value for the property, the declaration becomes invalid at computed-value time, and the property falls back to its inherited or initial value. It does not use the fallback.
Composing values
You can build one value from others with calc(). This pattern matters later, because Tailwind's spacing scale is a single base value multiplied by a number.
:root {
--spacing: 0.25rem;
}
.card {
padding: calc(var(--spacing) * 4);
}Here .card gets 1rem of padding.
Common errors
- Case matters.
--Brandand--brandare different properties. - Unitless numbers.
calc(var(--spacing) * 4)works.calc(var(--spacing) 4)does not. - Wrong scope. A variable defined on
.darkis not visible to elements outside it. - Using
var()in a name. You can't build a property name withvar(). It only replaces values.
How this connects to Tailwind
In Tailwind v4, a design token such as a color or font is stored as a CSS variable. Utilities like bg-brand produce CSS that reads var(--color-brand). The @theme directive tells Tailwind which variables are design tokens, so it can generate utilities from them. The next chapters cover that.
Exercise
Given the CSS below, what background color does <div class="dark"><p class="panel"></p></div> show for the panel, and what color does a .panel outside any .dark element show?
:root { --surface: white; }
.dark { --surface: #111; }
.panel { background: var(--surface); }
Show answer
The panel inside .dark is #111, because it inherits the value set on its ancestor. A .panel outside .dark is white, inherited from :root.
Exercise
What does .box { margin: calc(var(--spacing) * 3); } compute to when --spacing is 0.25rem?
Show answer
0.75rem.