Tailwind v4 @apply not working: "Cannot apply unknown utility class"
Style blocks in Astro, Vue and Svelte and CSS modules need @reference in v4. Custom classes need @utility. The error, the fix and the other causes.
- Tailwind CSS 4.3.5
- Astro 7.4.13
If Tailwind CSS v4 stops your build with Cannot apply unknown utility class, the CSS you're compiling can't see your Tailwind setup. That happens in every stylesheet that's compiled on its own: a <style> block in an Astro, Vue or Svelte component, or a CSS module. The fix is one line at the top of that style block: @reference "../styles/global.css";, pointing at the file that imports Tailwind.
Below are the error, the fix for each framework, and the second, less obvious cause: applying a class you made yourself.
The error
We built this Astro component on Astro 7.4 with Tailwind 4.3.5:
<div class="card">A card styled from a scoped style block</div> <style> .card { @apply rounded-lg bg-brand p-6 text-white; }</style>The build fails, and not on our custom bg-brand. It fails on rounded-lg, a core utility:
npx astro build[ERROR] [vite] ✗ Build failed in 40msCannot apply unknown utility class `rounded-lg`. Are you using CSS modules or similar and missing `@reference`?In v3, every stylesheet was processed with the full config in scope. In v4 each stylesheet that Vite hands to Tailwind is compiled on its own. The component's style block never imported Tailwind, so it knows no utilities at all.
The fix: @reference
@reference gives the block access to your theme and utilities without copying any CSS into it. Point it at your main stylesheet, the one with @import "tailwindcss" and your @theme:
<style> @reference "../styles/global.css"; .card { @apply rounded-lg bg-brand p-6 text-white; }</style>The build passes, and the scoped rule contains only what you applied:
grep -o '\.card\[data-astro-cid-[a-z0-9]*\]{[^}]*}' dist/_astro/*.css.card[data-astro-cid-yk4hkwyg]{border-radius:var(--radius-lg,.5rem);background-color:var(--color-brand,oklch(55% .2 260));padding:calc(var(--spacing,.25rem) * 6);color:var(--color-white,#fff)}The path is relative to the component file. The same line works in a Vue or Svelte <style> block and at the top of a .module.css file. If you don't use custom theme values in that block, @reference "tailwindcss"; is enough.
Applying your own classes
The second cause looks like the first but isn't fixed by @reference. In v3 you could define a class in @layer components and @apply it elsewhere. In v4 that fails:
@import "tailwindcss"; @layer components { .btn { padding: 1rem; }} .x { @apply btn; } /* Error: Cannot apply unknown utility class `btn` */Tailwind v4 only lets you @apply utilities it knows. Define the class with @utility and it becomes one, including variants like hover:btn:
@utility btn { padding: 1rem;} .x { @apply btn; }Other reasons @apply seems not to work
- A typo or a removed class. v4 dropped the opacity utilities:
@apply bg-opacity-50gives the same "unknown utility" error (writebg-black/50instead). Renamed classes are sneakier.shadow-sm,roundedandoutline-nonestill exist, but at a different size or with a different meaning, so they apply without an error and look wrong. The upgrade guide lists every rename. - A custom colour in
:rootinstead of@theme. Thenbg-brandisn't a utility anywhere. See custom colours not working. - Old v3 directives. A stylesheet that still starts with
@tailwind base; @tailwind components; @tailwind utilities;compiles to almost nothing in v4. We measured 65 bytes: the licence comment. Replace the three lines with@import "tailwindcss";.
Questions
Does @reference duplicate Tailwind's CSS in every component?
No. That's the difference with @import. @reference makes the theme and utilities known for @apply and theme() but emits nothing.
Can I avoid adding @reference to every Astro component?
Use global styles for shared rules (a <style is:global> in your layout, or the main stylesheet) and plain classes in components. There's no setting that injects @reference everywhere.
Related articles
- Tailwind CSS v4Does Tailwind CSS v4 have a config file?Not by default: v4 is configured in CSS with @theme. Where every v3 setting went, how to load an old tailwind.config.js with @config, and when to keep it.
- Tailwind CSS v4Tailwind v4 custom colors: how to add them, and why they don’t workAdd a --color-* variable in @theme and every colour utility follows. The four reasons a custom colour silently does nothing, each reproduced.
- shadcn/uishadcn/ui not working with Tailwind v4: six causes and fixesUnstyled or transparent shadcn components on Tailwind v4 come from v3 leftovers: the old PostCSS plugin, @tailwind directives, hsl() wrappers, missing imports.
Still stuck? We answer every message ourselves. Write to us