admin管理员组文章数量:1356364
I installed Tailwind as a Vite plugin, and it works fine when used directly in app.css
. However, when I apply Tailwind classes inside a separate component file and import it into app.jsx
, the styles don’t appear. How can I fix this?
I installed Tailwind as a Vite plugin, and it works fine when used directly in app.css
. However, when I apply Tailwind classes inside a separate component file and import it into app.jsx
, the styles don’t appear. How can I fix this?
2 Answers
Reset to default 0If you want to use @apply
in a separate component, you first need to reference the global app.css
using the @reference
directive like this:
./src/css/app.css
@import "tailwindcss";
@theme {
}
- Using @apply with Vue, Svelte, or CSS modules - TailwindCSS v4 Docs
After that, TailwindCSS v4's CSS-first configuration provides a @reference
directive, which allows us to integrate TailwindCSS functionality into the scoped styles of each extra Vue, Svelte, and other components.
If you want to use
@apply
or@variant
in the<style>
block of a Vue or Svelte component, or within CSS modules, you will need to import your theme variables, custom utilities, and custom variants to make those values available in that context.To do this without duplicating any CSS in your output, use the @reference directive to import your main stylesheet for reference without actually including the styles:
./src/components/component.jsx
<style>
@reference "./../css/app.css";
h1 {
@apply text-2xl font-bold text-red-500;
}
</style>
If you're just using the default theme with no customizations, you can import
tailwindcss
directly without app.css' customizations:
./src/components/component.jsx
<style>
@reference "tailwindcss";
h1 {
@apply text-2xl font-bold text-red-500;
}
</style>
@reference
directive - TailwindCSS v4 Docs
TailwindCSS v4
Starting from January 2025, with the release of TailwindCSS v4, running npm install tailwindcss will install the new v4 instead of v3. This is important because v4 comes with several breaking changes.
- What's breaking changes from TailwindCSS v4? - StackOverflow
Removed @tailwind directives
In v4 you import Tailwind using a regular CSS
@import
statement, not using the@tailwind
directives you used in v3:Not supported from v4
@tailwind base; @tailwind components; @tailwind utilities;
Supported from v4
@import "tailwindcss";
- Upgrade guide - TalwindCSS v3 to v4
- Problem with "npx tailwindcss init -p" command - StackOverflow - related to v4 upgrade
- Unable to upgrade Tailwind CSS v3 to v4 - StackOverflow - related to v4 upgrade
- How to setting Tailwind CSS v4 global class? - StackOverflow - related to v4 upgrade
- Change TailwindCSS default theme with Vite - StackOverflow - related to v4 upgrade
TailwindCSS v3
If you still want to use v3, install it with a specific version:
npm install tailwindcss@3
本文标签:
版权声明:本文标题:vite - Tailwind is not getting applied to individual components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743959108a2568688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@apply
inside a<style>
block: in the answer – rozsazoltan Commented Mar 31 at 9:10