Sub topic 10
@apply in Vue, Svelte, and CSS Module Styles
This chapter assumes you know how @apply works in a global stylesheet, that you have used Tailwind utilities, and that you have written a component in Vue, Svelte, or a similar framework. It covers why @apply can fail in a component's own stylesheet and how to fix that in Tailwind CSS v4.
Why scoped styles behave differently
In a typical Tailwind v4 project, one main stylesheet holds the import line:
@import "tailwindcss";That line loads the theme (colors, spacing, fonts) and all the utilities. @apply can only use a utility that the stylesheet it sits in knows about.
Vue <style> blocks, Svelte <style> blocks, and CSS module files (such as Button.module.css) are processed as separate stylesheets. They do not inherit anything from your main file. Tailwind sees @apply text-2xl in one of them and has no definition for text-2xl, so the build fails with an error like "Cannot apply unknown utility class".
Using @reference in Tailwind v4
The @reference directive tells a stylesheet where to find the theme and utilities. It imports them for lookup only. It does not copy any CSS into the output, so the component stylesheet does not duplicate your whole utility set.
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>The path is relative to the file that contains it, and it points to the stylesheet that has @import "tailwindcss". Custom theme values and custom utilities defined in that file also become available.
Svelte works the same way:
<h1>Welcome</h1>
<style>
@reference "../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>For a CSS module, put the directive at the top of the .module.css file:
@reference "../app.css";
.card {
@apply rounded-lg bg-white p-4 shadow;
}You can also reference the package directly if you do not need any custom theme:
@reference "tailwindcss";Tailwind v3 differs
In Tailwind v3, there is no @reference. The build tool reads tailwind.config.js, so @apply works in any processed stylesheet without extra setup. If you upgrade from v3 to v4, scoped styles that used @apply will break until you add @reference.
| Version | Setup in a component stylesheet |
|---|---|
| v3 | Nothing extra. The config file supplies the theme. |
| v4 | Add `@reference "path/to/app.css"` at the top. |
Common errors
- Unknown utility class. The
@referenceline is missing or points to the wrong file. Check the relative path. - Custom utility not found. The utility is defined in a file that your referenced stylesheet does not import. Define it in, or import it from, the referenced file.
- Styles missing in the browser. The
<style>block is scoped, so the rules only match elements in that component. That is by design.
Performance note
Each stylesheet that uses @reference is processed separately, but nothing is duplicated in the final CSS. Only the rules you write are emitted. For large projects, a reference to a small file that holds only your theme is a reasonable choice, though a single reference to the main file is usually simpler.
Exercise
A Vue component's <style> block contains @apply px-4 py-2 and the Tailwind v4 build reports an unknown utility error. What line should you add at the top of the block, and why?
Show answer
Add @reference "../../app.css"; (with the correct relative path to the stylesheet that has @import "tailwindcss"). A component style block is processed separately, so it does not know the theme or utilities until it references the main stylesheet. @reference makes them available for @apply without copying any CSS into the output.
Exercise
Why does the same @apply code work in Tailwind v3 without @reference?
Show answer
In v3 the build reads tailwind.config.js, which supplies the theme and utilities to every processed stylesheet, so no per-file reference is needed.