Replies: 3 comments 4 replies
-
Tailwind’s @apply is meant for applying utility classes inside CSS rules, not for referencing them in selectors directly. For example:
But this:
can confuse the JIT compiler because: .bg-primary-20 and other Tailwind utilities are generated classes, not normal CSS selectors. Tailwind’s compiler does not guarantee that you can use its classes inside complex selectors (like :is() or chained selectors). It may produce “corrupted” output or drop rules because it tries to tree-shake unused utilities. In short: Tailwind encourages using @apply for utilities, but not using utility classes as part of your selectors.
Tailwind’s JIT engine scans your source files (JSX, HTML, CSS) for class names. When you put utility classes in selectors like header:is(.bg-white, ...) h1, the compiler may misinterpret them or generate invalid output because they appear in a selector context rather than in a class attribute or @apply. Some rules might disappear, and some unrelated rules might appear — this is the “corruption” you’re seeing.
Option A – Use @apply inside regular selectors:
Then in your JSX:
Option B – Add custom variants if needed If you really need conditional styles for multiple backgrounds, you can use Tailwind’s variants or write custom classes:
Option C – Avoid referencing Tailwind classes in :is() Tailwind is not designed to support selectors like this. You should define a new custom class and apply Tailwind utilities inside it:
Then in JSX:
✅ Key takeaway: |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks for you answer! I was expecting something like that. What's funny is, that i wanted to mark your answer as answer but Github marked it as spam instead. LOL? What i don't quite understand yet is in Option B, why would this be valid?
Isn't it doing the same thing? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
since i am not sure if that is a bug, or just not allowed to do, i wanted to ask first.
To reproduce the issue:
Unfortunately, this may or may not lead to immediate CSS corruption. But at some point, i get CSS generated, that is not wrote by myself. In other words, the compiler must be mixing up classes and generates CSS rules that don't exist.
Therefore, i wanted to ask if it may generally be not allowed to use utility classes in selectors of custom CSS?
Code example:
Corrupted output:

In this case, the rule written is generated + other rules that do not exist.
Note, that in some cases the actual rule you write won't even be generated.
I would be happy to recieve any kind of help, since this issue is affecting production and i don't know what to do.
Best regards,
Marius
Beta Was this translation helpful? Give feedback.
All reactions