Sub topic 10
@apply in Vue, Svelte, and CSS module styles
This chapter assumes you know the basics of @apply and @layer components from earlier in the tutorial. You should also be comfortable with Vue single-file components or Svelte components, and know what a CSS module is. The examples use Tailwind CSS v4, and the version differences are noted where they matter.
Why scoped stylesheets are a special case
In a normal Tailwind project, one main stylesheet starts with @import "tailwindcss";. Tailwind processes that file, finds the utilities your templates use, and generates the CSS. Because @apply lives in that same file, it can see every utility and every theme value.
Component frameworks add more stylesheets. A Vue <style> block, a Svelte <style> block, and a .module.css file are each processed as a separate CSS file by your build tool. Each of these files is a separate entry point. None of them contains @import "tailwindcss", so Tailwind has no theme, no utilities, and no custom utilities to work with in that file.
The symptom is a build error such as "Cannot apply unknown utility class". The cause is not a typo in the class name. The file simply does not know what Tailwind is.
Tailwind v4: use @reference
In Tailwind v4, a separate stylesheet must import the theme and utilities before it can use @apply. You do this with @reference.
<template>
<h1>Page title</h1>
</template>
<style>
@reference "../../app.css";
h1 {
@apply text-2xl font-bold;
}
</style>The path points to your main CSS file, the one that contains @import "tailwindcss";. The path is relative to the file that contains the @reference line, so it changes depending on where the component lives.
@reference imports the definitions but does not output any CSS. It makes the theme, utilities, custom utilities, and custom variants available to @apply in this file, and it adds nothing to the final stylesheet. This is the main difference from @import. If you used @import "../../app.css" instead, every component would copy Tailwind's full output into its own stylesheet. That duplicates CSS, slows the build, and can reorder the cascade.
Referencing Tailwind directly
If you do not have a main stylesheet with custom theme values, you can reference Tailwind itself:
@reference "tailwindcss";This gives access to the default theme and built-in utilities only. Your own @theme values, @utility definitions, and @custom-variant rules live in your main stylesheet. To use them in a component, reference that file instead.
Using theme variables without @apply
Tailwind v4 also exposes theme values as CSS variables, such as --color-blue-600 and --spacing-4. In v4 you can use them directly in plain CSS, and in some setups you do not need @reference at all:
h1 {
color: var(--color-blue-600);
font-size: var(--text-2xl);
}This approach has less build overhead, because the file does not have to be processed by Tailwind. It also makes the relationship to the theme explicit. The trade-off is that you give up variants such as hover: and md: unless you write the media query or pseudo-class yourself.
Svelte
The same rule applies to Svelte. Add @reference at the top of the <style> block.
<button class="btn">Save</button>
<style lang="postcss">
@reference "../app.css";
.btn {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
}
.btn:hover {
@apply bg-blue-700;
}
</style>Two details matter in Svelte:
- Svelte scopes component styles by adding a generated class to the elements in that component. Your
.btnrule only matches elements in the same component. This is what you want for a local button, and it is not suitable for a shared class that several components must use. - Svelte removes selectors that it cannot find in the markup. If a class is applied dynamically in a way the compiler cannot see, the rule can be dropped as unused. Use
:global(...)for those selectors.
Whether the lang="postcss" attribute is needed depends on your preprocessor setup. If the Tailwind Vite plugin or PostCSS plugin already handles <style> blocks in your project, a plain <style> block can be enough.
CSS modules
A CSS module file, such as Button.module.css, is also a separate stylesheet. It needs the same reference line.
@reference "../app.css";
.button {
@apply rounded-md bg-blue-600 px-4 py-2 font-semibold text-white;
}
.button:hover {
@apply bg-blue-700;
}Used from a React component, it looks like this:
import styles from "./Button.module.css";
export function Button({ children, ...props }) {
return (
<button className={styles.button} {...props}>
{children}
</button>
);
}The bundler renames .button to a unique name, so the class cannot collide with others. The @apply still resolves to the same utilities as in any other file.
Tailwind v3: no @reference
Tailwind v3 does not have @reference. In v3, @apply works in any CSS file that the Tailwind PostCSS plugin processes, and it reads the theme from tailwind.config.js. You write @apply directly, with no import line.
The v3 and v4 mechanisms differ as follows:
| Concern | Tailwind v3 | Tailwind v4 |
|---|---|---|
| Where the theme comes from | `tailwind.config.js` | `@theme` in CSS |
| Separate stylesheet setup | None, if processed by PostCSS | `@reference "path/to/main.css"` |
| Custom utility syntax | `@layer utilities { ... }` | `@utility name { ... }` |
| Risk of duplicating output | Low | Present if you use `@import` instead of `@reference` |
If you copy a snippet from an older blog post into a v4 project, @apply in a Vue block will fail with an unknown utility error. If you copy a v4 snippet into a v3 project, @reference is an unknown at-rule. Check your installed version first.
Custom utilities and variants in component styles
@reference also brings in custom definitions from your main stylesheet. If app.css defines a custom utility:
@import "tailwindcss";
@utility btn-base {
@apply rounded-md px-4 py-2 font-semibold;
}then a component can use it after referencing the file:
<style>
@reference "../../app.css";
.save-button {
@apply btn-base bg-blue-600 text-white;
}
</style>Without the reference line, btn-base is unknown. This is a common source of confusion, because the same class works in markup, where the main stylesheet is loaded, and fails in a component <style> block, where it is not.
Build performance
Each @reference makes the build tool process the referenced file again for that component. In a project with hundreds of components, this can add up. Some practical ways to limit the cost:
- Keep the referenced stylesheet small. Put heavy parts, such as large
@font-facesets, in a separate file that you do not reference. - Use
@applyin component styles only where the benefit is clear, such as styling elements you do not control (rendered Markdown, a third-party widget). - For the rest, put the classes in the template and keep the component stylesheet empty.
How the pieces fit together
The next diagram shows how the main stylesheet and a component stylesheet relate in v4.
Common errors
- "Cannot apply unknown utility class". The component stylesheet has no
@reference, the path is wrong, or the utility is defined in a file that you did not reference. - A wrong relative path.
@referenceresolves from the current file. Moving a component to a different folder breaks the path. Many projects configure an alias, such as@reference "@/styles/app.css", if the bundler supports it. - Duplicated CSS in the output. An
@import "tailwindcss"was added to component styles. - A rule that has no effect in Svelte or Vue. Scoping applies the rule only to elements in that component. Child component roots and dynamic content need
:deep()in Vue or:global()in Svelte. - Variants not generated.
@apply hover:bg-blue-700works when the variant is known. A custom variant defined in the main file also needs the reference.
Choosing whether to use @apply here
A scoped stylesheet is a reasonable place for @apply when you need a CSS selector and cannot add classes to the elements. Examples are prose rendered from Markdown, a third-party widget, or a rule that combines pseudo-elements and utilities. If you control the markup, a component with utility classes is usually simpler and avoids the extra stylesheet, the reference line, and the build cost.
Exercise
A Vue component has a <style> block with h1 { @apply text-2xl font-bold; }. The project uses Tailwind v4 and the build fails with "Cannot apply unknown utility class text-2xl". What is the cause, and what is the correct fix? Why is adding @import "tailwindcss"; to the block a poor fix?
Show answer
The <style> block is processed as a separate stylesheet, so it does not know Tailwind's theme or utilities. Add @reference "../../app.css"; (with the correct relative path to the main stylesheet that imports Tailwind) as the first line of the block. @reference makes the definitions available to @apply without emitting any CSS. @import "tailwindcss"; would work, but it copies all of Tailwind's generated CSS into that component's output, which duplicates the stylesheet for every component and makes the cascade harder to predict.
Exercise
You define @utility btn-base { @apply rounded-md px-4 py-2 font-semibold; } in app.css. Using class="btn-base" works in a template, but @apply btn-base fails inside a Svelte <style> block. What is missing?
Show answer
The Svelte block does not reference app.css. Add @reference "../app.css"; (adjusted to the correct path) at the top of the <style> block. Templates work because the main stylesheet is loaded on the page, while the component stylesheet is built separately and needs the reference to see custom utilities.