Sub topic 2
Extending the Default Theme
Assume you know basic CSS custom properties, var(), and Tailwind utility classes. In Tailwind CSS v4, configuration can live directly in your CSS file.
Extending the default theme
Add variables inside @theme to extend Tailwind’s default design tokens. The variable namespace determines which utilities Tailwind generates.
@import "tailwindcss";
@theme {
--color-avocado-500: oklch(0.84 0.18 117.33);
--font-display: "Satoshi", sans-serif;
--breakpoint-3xl: 120rem;
}These variables create utilities such as bg-avocado-500, font-display, and the 3xl: responsive variant. Existing default utilities remain available.
<div class="bg-avocado-500 font-display 3xl:grid-cols-6">
Themed content
</div>Resetting a namespace or the whole theme
Use initial when a project should allow only selected values. Reset one namespace by adding its wildcard:
@theme {
--color-*: initial;
--color-white: #fff;
--color-brand: #3f3cbb;
}This removes the default color utilities and generates only the colors declared afterward. To reset every default theme variable, use:
@theme {
--*: initial;
--color-brand: #3f3cbb;
--font-body: Inter, sans-serif;
}Resetting reduces the available API, but it also requires you to define every token your components need.
The spacing scale and the `--spacing` base value
Tailwind calculates numbered spacing utilities from --spacing. With the default value, p-4 means four times the spacing unit.
@theme {
--spacing: 0.25rem;
}You can change the base unit for the whole scale:
@theme {
--spacing: 1px;
}Now p-4 produces padding: 4px. Use this carefully because it changes every numbered spacing utility.
When to use `@theme` and when to use `:root`
Use @theme for tokens that should create Tailwind utilities or variants. Use :root for ordinary runtime values that CSS needs but Tailwind does not need to scan.
:root {
--surface: oklch(1 0 0);
}
@theme inline {
--color-surface: var(--surface);
}Here, :root owns the value, while @theme inline connects it to the bg-surface utility. A class such as .dark can override --surface at runtime.
> callout note > A variable in :root alone does not create a Tailwind utility. Put it in the correct @theme namespace when you need a class.
Exercise
How would you add a bg-brand utility while keeping Tailwind’s default colors?
Show answer
Add --color-brand: ... inside @theme without resetting the --color-* namespace.