You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Tim Phillips, Dorian Marié, Jeff Daley, Harshil Sharma, Henrique G. Testa, Kevin Morales, mohsen saremi, Nitish Mittal, Alex Nault, dgltsv and 18 more
It's expected behavior. This is how html and css works, you can't change the precedence in html with the order of class-names.
What matters is the specificity of css selectors and the order of declarations in the source code if the specificity is the same. Because these declarations have the same specificity (single class-names) and the .p-4 declaration is defined later than .p-0 it will be the one applied to the element.
Read more about specificity: MDN Article | Specifics on CSS Specificity (CSS-Tricks)
If you are trying to dynamically toggle between p-0 and p-4 you should always swap them out, toggle both, you shouldn't rely on css source order.
Reacted by Jason Gunawan Limantoro, Nestor Vera, Dorian Marié, Heng-Hao Pu, Ben Crook, Jake Worth, simone, Yu Inao / 稲尾 遊, Florian Ebeling, Piyush Aggarwal and 6 more
It would be interesting to make something like https://www.npmjs.com/package/classnames, but that removes p-4 from the output if it sees p-0 applied after. This would allow making React components that can have Tailwind styles overridden, even if they're being overridden with classes of lower specificity.
Reacted by Tim Phillips, asdfasdfasdfasdfff, amankkg, Jonas Kuske, alisoleimani, Adam Weber, Stefan K, mrmschwartz, Chirag Jain, Jeff Daley and 56 more
Reacted by Karthik, Sibasish Mohanty, Amit Bar, Kaan Uzdoğan, Alex Alonso?, donartk, Haizz and Léo Hafdane
I agree with @DylanVann. If tailwindcss used the "latest" class as the most specific, it would make it easier to create reusable components with a fallback class. For example:
Agreed! I also thought I could create a default styling for a component and override it by simply passing className props, but does not work if specificities of attributes are different.
constStatusBadge=({ status, className })=>{return(<spanclassName={`${className} px-4 py-2 ml-auto text-base rounded-full text-yellow-600 bg-yellow-200 `}>{status}</span>);}constApp=()=>{return(// text-base overrides text-sm both like this and when ${className} is at the end<StatusBadgeclassName='text-sm/> // text appears large
)
}
let a = 'p-4 bg-red-200 mx-6';
let b = 'p-2 font-bold m-3';
let f = mergeTailwindClasses(a, b);
console.log(f); // bg-red-200 font-bold m-3 mx-6 p-2
Notes:
The sort() isn't necessary, of course, but I like the classes in some order for easier debugging.
This doesn't work in properties that are semantically grouped towards the front (like bg-repeat-y, where the main semantic content is bg-repeat and the value is y, as opposed to bg-red-200, where the main semantic content is bg and the value is red-200).
I haven't needed the version that handles bg-repeat-style classnames yet, but someone might. Here's the function I wrote for that (for completeness).
Nice solution @sirrobert, but classes like .bg-transparent and .bg-contain would still cause trouble, it seems. I guess it would be hard to avoid that with Tailwind's nice shorthand naming scheme.
You could use the fact that the closest parent takes precedence to create a fallback hierarchy. Wrap the element in a div and apply the fallback styles to the div.
In a perfect world, you could write a function that first takes a string of tailwind classes and turns it into CSS. Then secondly, takes that CSS, ignoring and purging all the duplicated and conflicting styles, and turns it back into tailwind classes.
Activity
tlgreg commentedon Jul 7, 2019
It's expected behavior. This is how html and css works, you can't change the precedence in html with the order of class-names.
What matters is the specificity of css selectors and the order of declarations in the source code if the specificity is the same. Because these declarations have the same specificity (single class-names) and the
.p-4declaration is defined later than.p-0it will be the one applied to the element.Read more about specificity: MDN Article | Specifics on CSS Specificity (CSS-Tricks)
If you are trying to dynamically toggle between
p-0andp-4you should always swap them out, toggle both, you shouldn't rely on css source order.jasonlimantoro commentedon Jul 7, 2019
@tlgreg got it, thanks for the in depth answer
tim-phillips commentedon Mar 14, 2020
The order of class names defined on an HTML element have no effect on styling. CSS Specificity only cares about what is defined in stylesheets.
Most specificity articles, including the ones referenced above, don't talk about the order of HTML class names. This helped:
https://stackoverflow.com/questions/12258596/class-overrule-when-two-classes-assigned-to-one-div/12258654
As a workaround, I've defined some base styles in order to increase specificity in my tailwind.css:
DylanVann commentedon Sep 3, 2020
It would be interesting to make something like https://www.npmjs.com/package/classnames, but that removes
p-4from the output if it seesp-0applied after. This would allow making React components that can have Tailwind styles overridden, even if they're being overridden with classes of lower specificity.mracette commentedon Jan 29, 2021
I agree with @DylanVann. If tailwindcss used the "latest" class as the most specific, it would make it easier to create reusable components with a fallback class. For example:
Wouldn't this be possible by selectively removing properties from the final class output when there are conflicts?
cipriancaba commentedon Apr 21, 2021
You can do something like
<button class="rounded-lg p-4 inline-block p-!0 default ripple">No?</button>with the new jit compilerkuzdogan commentedon Apr 23, 2021
Agreed! I also thought I could create a default styling for a component and override it by simply passing
classNameprops, but does not work if specificities of attributes are different.sirrobert commentedon May 15, 2021
For what it's worth, here's a js function I wrote that does this. Later classes are given absolute priority.
Usage:
Notes:
The
sort()isn't necessary, of course, but I like the classes in some order for easier debugging.This doesn't work in properties that are semantically grouped towards the front (like
bg-repeat-y, where the main semantic content isbg-repeatand the value isy, as opposed tobg-red-200, where the main semantic content isbgand the value isred-200).I haven't needed the version that handles
bg-repeat-style classnames yet, but someone might. Here's the function I wrote for that (for completeness).Same usage. In reality, you probably need a manual switch to differentiate.
HertzaHaeon commentedon Jun 18, 2021
Nice solution @sirrobert, but classes like
.bg-transparentand.bg-containwould still cause trouble, it seems. I guess it would be hard to avoid that with Tailwind's nice shorthand naming scheme.I found this more complicated solution that seems to do the trick even for the above problem: https://github.com/richardgill/tailwind-override
abrehamgezahegn commentedon May 9, 2022
!p-0to apply important styles. It's a quick fix.Fix style for focused participant
Fix style for focused participant
bvanderdrift commentedon Jul 20, 2022
You could use the fact that the closest parent takes precedence to create a fallback hierarchy. Wrap the element in a
divand apply the fallback styles to thediv.Before:
Fixed:
bitttttten commentedon Nov 14, 2022
In a perfect world, you could write a function that first takes a string of tailwind classes and turns it into CSS. Then secondly, takes that CSS, ignoring and purging all the duplicated and conflicting styles, and turns it back into tailwind classes.
Input:
jahilliard commentedon Nov 16, 2022
I found this the package tailwind-merge seems to be what people are looking for.
mohitkumawat310 commentedon Mar 16, 2023
Thanks bro this works very well