Sub topic 3
Using Theme Variables in Your Own CSS
Assume you know basic CSS custom properties, var(), and Tailwind utility classes. In Tailwind CSS v4, @theme defines design tokens that Tailwind can turn into utilities. You can also read those tokens from custom CSS.
@import "tailwindcss";
@theme {
--color-surface: oklch(0.98 0 0);
--color-content: oklch(0.2 0 0);
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from { opacity: 0; }
to { opacity: 1; }
}
}
.card {
padding: calc(var(--spacing) * 4);
background: var(--color-surface);
color: var(--color-content);
animation: var(--animate-fade-in);
}The --animate-* variable creates an animate-fade-in utility. The @keyframes rule defines the animation used by that variable.
Referencing other variables
Use @theme inline when a theme variable refers to another variable. This keeps the reference intact, so a runtime override can change the result.
:root {
--surface: oklch(1 0 0);
--content: oklch(0.2 0 0);
}
.dark {
--surface: oklch(0.2 0 0);
--content: oklch(0.98 0 0);
}
@theme inline {
--color-surface: var(--surface);
--color-content: var(--content);
}Now bg-surface and text-content respond when .dark changes the regular variables.
Static output and runtime themes
By default, Tailwind may emit only variables needed by generated classes. Add static when every variable in the block must appear in the output.
@theme static {
--color-brand: oklch(0.5 0.2 260);
}A common dark-mode flow is:
flowchart TD
A[Root variables] --> B[Theme inline variables]
B --> C[Tailwind utilities]
D[.dark overrides] --> B
C --> E[Light or dark appearance]Sharing and migration
Put shared tokens in a CSS file and import that file into each project:
@import "tailwindcss";
@import "./brand-theme.css";This makes the CSS file your reusable theme package.
In Tailwind v4, @theme replaces much of tailwind.config.js. Existing projects can use @config to load a legacy configuration, but new tokens should usually be defined in CSS. Convert theme.extend entries into namespaced variables such as --color-brand, --font-display, or --breakpoint-3xl.
Exercise
Which option should you use when a theme variable references a value that changes under .dark?
Show answer
Use @theme inline, and define the changing value as a regular CSS variable.