[v4] @apply inside Svelte / Vue <style>
not working as expected
#15205
Replies: 7 comments 3 replies
-
Hey! Check the relevant documentation here: https://tailwindcss.com/docs/v4-beta#using-apply-in-vue-svelte TL;DR: <style>
@import 'tailwindcss/theme' theme(reference);
.red {
@apply text-red-500;
}
</style> |
Beta Was this translation helpful? Give feedback.
-
I also noticed this with Vue. It took me a long time to realize that I need I agree that this doesn't feel intuitive. Adding I hope to improve "quality of life" for v4. :P |
Beta Was this translation helpful? Give feedback.
-
I'm also looking for a solution to this - I appreciate the preferred approach is to use inline classnames but I do end up using @apply across almost all components.... A solution for us few would be nice. |
Beta Was this translation helpful? Give feedback.
-
Anybody wanting a temporary solution that you could in theory, forget about. I wrote this tiny plugin for vite, which auto-injects the required fix into all .vue files, so you don't have to do it manually. Note: Place the plugin as the first 1. Ignore the above, it doesn't work properly with HMR :( |
Beta Was this translation helpful? Give feedback.
-
This is also required for Astro's |
Beta Was this translation helpful? Give feedback.
-
Just attempted to update to tailwind 4 & bumped into this issue when using it with Vue & Nuxt UI 4. |
Beta Was this translation helpful? Give feedback.
-
Here's the docs I wrote for this earlier this week that will be in the v4 docs, including the CSS modules docs since they go into more detail about the same stuff: Vue, Svelte, and AstroVue, Svelte, and Astro support If you're using Tailwind with these tools, we recommend avoiding If you do use <template>
<button><slot /></button>
</template>
<style scoped>
@import "../app.css" reference;
button {
@apply bg-blue-500;
}
</style> Or just use your globally defined CSS variables instead of features like <template>
<button><slot /></button>
</template>
<style scoped>
button {
background-color: var(--color-blue-500);
}
</style> CSS modulesTailwind is compatible with CSS modules and can co-exist with them if you are introducing Tailwind into a project that already uses them, but we don't recommend using CSS modules and Tailwind together if you can avoid it. Scoping concernsCSS modules are designed to solve scoping problems that just don't exist when composing utility classes in your HTML instead of writing custom CSS. Styles are naturally scoped with Tailwind because each utility class always does the same thing, no matter where it's used — there's no risk that adding a utility class to one part of your UI creates some unexpected side effect somewhere else. PerformanceWhen using CSS modules, build tools like Vite, Parcel, and Turbopack process each CSS module separately. That means if you have 50 CSS modules in a project, Tailwind needs to run 50 separate times, which leads to much slower build times and a worse developer experience. Explicit context sharingSince CSS modules are each processed separately, they have no This means features like @import "../app.css" reference;
button {
@apply bg-blue-500;
} Alternatively, you can also just use CSS variables instead of button {
background: var(--color-blue-500);
} I don't really consider this a workaround or anything, this is the API you need to use when you want to reference Tailwind theme variables from a totally disconnected entry point which is how these style blocks work. Imagine you had two main CSS files in your project that looked like this:
Now in some Vue file you do this: <template>
<h1>Hello world</h1>
</template>
<style>
h1 {
@apply text-primary;
}
</style> There's no way to know if that is supposed to be blue or red without loading the relevant theme configuration. So just like in JS when you need to import something to use it, the same thing is true with v4. |
Beta Was this translation helpful? Give feedback.
-
What version of Tailwind CSS are you using?
tailwindcss: 4.0.0-beta.2
@tailwindcss/vite: 4.0.0-beta.2
What build tool (or framework if it abstracts the build tool) are you using?
svelte: 5.2.9
sveltekit: 2.8.5
What version of Node.js are you using?
v20.17.0
What browser are you using?
Chrome, Firefox
What operating system are you using?
Ubuntu 24.04
Reproduction URL
Public github:
https://github.com/lucas-subli/svelte-5-tailwind-4-beta
Describe your issue
This code:
Generates this result:
With this error:
@apply
inside svelte<style>
does not work properly.After this PR: #14151
The below will work:
However, flagging the style as global and importing 'tailwindcss' in every component I want to use
@apply
is not a reasonable solution.The PR mentioned above also has a comment stating:
However, this does not seem to work, as seen in the provided reproduction URL, where tailwind is imported by
app.css
and made available through+layout.svelte
— as previously done with tailwind 3 + SvelteKit.Beta Was this translation helpful? Give feedback.
All reactions