Unexpected behavior when overloading classes #19781
-
|
Hello! ThanksThank you for your work, Tailwind 4 is great, even if the migration is not easy with a lib and several consumer projects! Tech detailsWhat version of Tailwind CSS are you using? v4.2.1 What build tool (or framework if it abstracts the build tool) are you using? Webpack encore What version of Node.js are you using? v20.19.0 What browser are you using? reproduced on Chrome, and firefox What operating system are you using? macOS Reproduction URL Describe your issue Architecture ContextI have a library which exports various things including basic css variables (which can be overloaded and extended by the consumer projects), a basic tailwind theme (which can be extended by the consumer projects) and react components using tailwind classes that exist in the tailwind theme mentioned (ie text-gray-500) One of my consumer projects has dynamic custom colors, which overload some elements (some of which are in the lib) and not others. Problematic styleTo do this, I overload some css variables in my consumer project, but I also must do the following, in order to overload certain library elements: .text-gray-500\!:not(
.tunnel-stepper .text-gray-500\!,
#tunnel-main .text-gray-500\!,
#cartValidation-special-request.text-gray-500\!,
#title-banner .text-gray-500\!,
.ratings-banner .text-gray-500\!,
.ratings-banner.text-gray-500\!,
.tippy-box.bg-white .text-gray-500\!,
.info-icon.text-gray-500\!,
dialog.bg-white.text-gray-500\!,
dialog.bg-white .text-gray-500\!
) {
@apply text-text-666!;
}This used to work fine in v3, but is buggy in v4 Current Behavior & steps to reproduce in the Tailwind playground
ConclusionI know this might seem like a marginal case, but this regression is quite problematic for us, and so far I haven't found another solution, and given the contradictory stuff happening in the devtools, I would say this is indeed a bug. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
|
You'd want to put your rule in ExplanationIn v4, CSS rules are placed in native cascade layers. Utility classes are placed in With |
Beta Was this translation helpful? Give feedback.
-
|
great thanks but does that mean all custom classes in the app should be in @layer utilities? also, there is still the issue of the devtools showing contradictory info as to which rule is active |
Beta Was this translation helpful? Give feedback.
-
|
ok thanks. I still feel like these are indeed an issue, if every unlayered style, internal or external, will override tailwind defined styles... using important everywhere doesn't seem great. i'l try with the layer(components) on the external style imports. |
Beta Was this translation helpful? Give feedback.
-
|
Yes right this is what I think as well
…On Thu, Mar 12, 2026, 6:50 PM Emmanuel ***@***.***> wrote:
Its not an issue nor is it directly related to tailwind, its how cascading
and layers work per the spec. You cannot expect to bring external styles
into a tailwind project without conflicts if you do not explicitly state
what precedence you expect them to have. If you instead use
custom/overloaded tailwind utilities, you most likely won't face these
problems (and would also probably need fewer !important styles):
text-gray-950 overload: https://play.tailwindcss.com/4LPb8U5WvE?file=css
swiper: https://play.tailwindcss.com/JGwsB0leiM?file=css
—
Reply to this email directly, view it on GitHub
<#19781 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/B7HZNK5CZTD5VZBD52SHTLD4QMBIFAVCNFSM6AAAAACWOMEJO2VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTMMJQGI3TONI>
.
You are receiving this because you are subscribed to this thread.Message
ID: <tailwindlabs/tailwindcss/repo-discussions/19781/comments/16102775@
github.com>
|
Beta Was this translation helpful? Give feedback.




You'd want to put your rule in
@layer utilities: https://play.tailwindcss.com/nCY33tCbKN?file=cssExplanation
In v4, CSS rules are placed in native cascade layers. Utility classes are placed in
@layer utilities.With
text-gray-950!, this would have the!importantflag on the properties.!importantflags inside cascade layers will always override non-layered rules, regardless of source order, specificity, etc. Hence, by placing our own rule inside@layer utilities, they are now on equal footing in terms of cascade layer. This means source order and specificity will have an effect. Thus, our custom rule now overrides thetext-gray-950!utility class rule.