Sub topic 15
Sharing a Theme Across Projects
This chapter assumes you can already write an @theme block, know how namespaces such as --color-* and --font-* map to utilities, and understand @theme inline and @theme static. It also assumes you have a working Tailwind CSS v4 setup in at least one project.
Once a theme works in one project, you will probably want a second site, an admin app, or a component library to use the same colors, fonts and breakpoints. Copying the @theme block into each project works at first, then the copies drift apart. This chapter shows how to keep one source of truth.
A theme is a CSS file
In Tailwind v4 the configuration is CSS. A file that contains only @theme blocks is a complete, importable theme. You don't need a plugin or a JavaScript config to share it.
/* brand/theme.css */
@theme {
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 120rem;
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}This file does not contain @import "tailwindcss". Leaving it out is deliberate. Each project imports Tailwind once in its own entry file, and the shared file only adds tokens. If the shared file also imported Tailwind, a project could end up with the framework's CSS included twice.
Importing the shared file
A project's entry stylesheet imports Tailwind first, then the shared theme:
/* app/src/styles.css */
@import "tailwindcss";
@import "../../brand/theme.css";Tailwind processes @import the same way a bundler would, so the contents of theme.css are merged into your stylesheet. The @theme block then behaves exactly as if you had typed it there. The utilities bg-avocado-500, font-display and 3xl:grid-cols-6 become available in this project.
<div class="bg-avocado-500 font-display 3xl:grid-cols-6 grid grid-cols-2">
<p class="text-avocado-100">Themed content</p>
</div>Overriding per project
Order matters. When two @theme blocks define the same variable, the later one wins. A project can therefore import the shared theme and then adjust a few tokens after it:
/* admin/src/styles.css */
@import "tailwindcss";
@import "../../brand/theme.css";
@theme {
--color-avocado-500: oklch(0.7 0.15 117);
--color-danger: #d92d20;
}In the admin app, bg-avocado-500 uses the darker value and bg-danger exists. The marketing site is untouched. This works well for a base brand with small local differences. If a project overrides many tokens, the shared theme may be too specific, so consider splitting it.
All @import rules must come before other rules in the file, so keep imports at the top and put local @theme blocks below them.
Resetting inside a shared theme
A shared theme can be additive or strict. An additive theme, like the one above, extends Tailwind's defaults. A strict theme removes the defaults first, so projects can only use the approved tokens.
/* brand/strict-theme.css */
@theme {
--color-*: initial;
--color-white: #fff;
--color-brand: #3f3cbb;
--color-brand-light: #8b89e6;
}With this file imported, bg-red-500 no longer exists in the project. Only white, brand and brand-light produce color utilities. To remove the whole default theme, use --*: initial; and define everything yourself.
A reset is a policy decision for every consuming project, so document it. A teammate who runs text-gray-700 and gets nothing will not guess that a shared file removed it.
Always emitting variables with `static`
By default, Tailwind writes a theme variable into the generated CSS only if something uses it, either through a utility class or a var() reference in your CSS. That keeps the output small. For a shared theme it has a side effect. Another tool, such as a charting library or a script that reads getComputedStyle, may need --color-brand at runtime even if no utility uses it.
Use @theme static for those tokens:
/* brand/tokens-static.css */
@theme static {
--color-brand: #3f3cbb;
--color-brand-light: #8b89e6;
}Every variable in a static block is emitted into :root, whether or not a class uses it. Use it for the small set of tokens that other code reads directly. Leave the rest in normal @theme blocks so projects only ship what they use.
Shipping runtime themes
A shared theme can also include the dark mode setup from the previous chapter. Put the :root and .dark rules and the @theme inline block in the same file:
/* brand/semantic.css */
: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);
}Any project that imports this file gets bg-surface and text-content, and both follow the .dark class. Using inline here matters, since it makes the utilities read the referenced variable directly, so overrides under .dark apply on the elements that use them.
Packaging the theme
A relative path such as ../../brand/theme.css works inside a monorepo. For separate repositories, publish the CSS as a package. The package only needs CSS files and an exports map that exposes them:
{
"name": "@acme/tailwind-theme",
"version": "1.0.0",
"files": ["theme.css", "semantic.css"],
"exports": {
"./theme.css": "./theme.css",
"./semantic.css": "./semantic.css"
}
}After installing it, a project imports the files by package name:
@import "tailwindcss";
@import "@acme/tailwind-theme/theme.css";
@import "@acme/tailwind-theme/semantic.css";Version the package like code. Renaming --color-brand to --color-primary removes bg-brand from every consumer, which is a breaking change. Adding a token is a minor release.
Scanning sources in shared packages
Sharing the theme does not share the class names used in your components. Tailwind generates only the utilities it finds in files it scans, and it skips node_modules by default. If the package also ships components with Tailwind classes, each project must point Tailwind at them:
@import "tailwindcss";
@import "@acme/tailwind-theme/theme.css";
@source "../node_modules/@acme/ui";Without @source, the components render with missing styles, even though the theme variables exist. Theme-only packages don't need this.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `bg-brand` does nothing | Shared file imported before `@import "tailwindcss"`, or not imported | Import Tailwind first, then the theme, and check the path |
| Styles appear twice or the build is large | The shared file also imports `tailwindcss` | Remove the Tailwind import from the shared file |
| A local override has no effect | The local `@theme` block is above the shared import | Put overrides after the import |
| Default colors vanished | The shared file contains `--color-*: initial` | Document the reset, or remove it |
| `var(--color-brand)` is undefined in a script | The variable is unused by utilities and was not emitted | Move it to `@theme static` |
| Package components are unstyled | `node_modules` is not scanned | Add `@source` for the package |
Exercise
A project has @import "tailwindcss"; followed by @theme { --color-brand: red; } and then @import "@acme/tailwind-theme/theme.css";, where the shared file defines --color-brand: #3f3cbb;. The local override does not work. What are two problems, and how do you fix them?
Show answer
First, the local @theme block comes before the import, so the shared definition is later and wins. Second, the @import is placed after another rule, and imports must come before other rules. Move the shared import directly under @import "tailwindcss"; and put the local @theme block below it.
Exercise
Your shared theme defines --color-brand, but a charting library reads it with getComputedStyle(document.documentElement).getPropertyValue("--color-brand") and gets an empty string. No class in the project uses bg-brand or text-brand. Why, and what change fixes it?
Show answer
Tailwind emits theme variables only when something uses them, so the unused variable was left out of the CSS. Define it in an @theme static block, which emits every variable in the block into :root regardless of use.