Skip to content

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.

Updated 23 November 2026Tested with
  • 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:

src/components/Card.astroAstro
<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:

Terminal
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:

src/components/Card.astroAstro
<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:

Terminal
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:

src/styles/global.cssCSS
@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:

src/styles/global.cssCSS
@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-50 gives the same "unknown utility" error (write bg-black/50 instead). Renamed classes are sneakier. shadow-sm, rounded and outline-none still 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 :root instead of @theme. Then bg-brand isn'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.

Was this page helpful?

Still stuck? We answer every message ourselves. Write to us

This page is built from UI Pro parts: the docs article block (docs-quernly) with the code, terminal, install command, steps, contents and feedback components. They ship in the next UI Pro release; the install command and the contents rail are free.