Sub topic 15
Sharing a theme across projects
This chapter assumes you can write an @theme block, know the difference between extending and resetting a namespace, and have Tailwind v4 installed through its Vite, PostCSS, or CLI integration.
Why a theme can be shared
Tailwind v4 keeps theme variables in CSS, not in a JavaScript config file. A theme is therefore an ordinary stylesheet. Put it in its own file and any project that can import CSS can use it. That includes a monorepo package, a separate repository, or a published npm package.
A shared theme file
Keep the file small. It should contain only design tokens and no @import "tailwindcss".
/* packages/brand/theme.css */
@theme {
--font-display: "Satoshi", "sans-serif";
--color-brand-100: oklch(0.96 0.03 280);
--color-brand-500: oklch(0.52 0.2 280);
--breakpoint-3xl: 120rem;
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}Each project imports Tailwind first and the brand file second.
/* apps/web/app.css */
@import "tailwindcss";
@import "../../packages/brand/theme.css";Both projects now get bg-brand-500, font-display, 3xl:* variants, and ease-fluid. If you change a token in the shared file, every consuming project picks it up on its next build.
How the pieces fit together
The build resolves the imports and merges every @theme block into one theme.
Choosing what the shared file does
You can write the shared file in one of two ways.
| Approach | What the file contains | Result in each project |
|---|---|---|
| Extend | Only new tokens | Default utilities stay, brand tokens are added |
| Replace | `--color-*: initial;` plus your own colors | Only the brand colors exist |
Use "replace" when a design system must stop developers from reaching for off-brand colors. Use "extend" when the shared file is a convenience layer.
/* packages/brand/strict-theme.css */
@theme {
--color-*: initial;
--color-white: #fff;
--color-brand-500: oklch(0.52 0.2 280);
}Emitting every variable with static
By default, Tailwind writes a theme variable to the output CSS only if some utility or your own CSS uses it. This keeps the output small. A shared theme is often read by things Tailwind cannot see, such as a charting library or inline styles that call var(--color-brand-500). Mark the block static so every variable is always emitted.
@theme static {
--color-brand-500: oklch(0.52 0.2 280);
--font-display: "Satoshi", "sans-serif";
}Common errors
- Utilities missing: the path in
@importis wrong, or the build tool does not process the file. Check that the path resolves from the importing stylesheet. - Defaults still present: the theme file was imported before
tailwindcss. - Variable missing in the browser: nothing used it, so Tailwind skipped it. Use
@theme static.
Exercise
Two apps import a shared theme.css that defines --color-brand-500. Neither app uses a brand-500 utility, but a third-party widget reads var(--color-brand-500) and gets nothing. What change fixes this, and where do you make it?
Show answer
Change the shared file's @theme block to @theme static. Tailwind then writes every variable in that block to the output CSS, even when no utility or custom CSS uses it. Because the change is in the shared file, both apps receive the fix.