Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Is it possible to override @apply directives?
I have this example:
https://play.tailwindcss.com/kyu6CxnBzB
.item {
@apply text-black font-light text-opacity-80 py-4 bg-gray-100;
<li class="item">About Us</li>
<li class="item">Success stories</li>
<li class="item text-red-50 bg-red-800">Contact</li>
<li class="item">Blog</li>
with this result:
when I expected
it seems like the value in the first class define with apply (item) takes precedence over any other class specified afterwards
how would you solve a scenerio like that? creating a component (I'm working with svelte) seems like an overwkill for this, and I´d like some way to avoid duplicating stuff like "font-sans text-black text-black text-opacity-80 my-4 hover:text-gray-800 hover:text-underline etc..."
This is a property of CSS, not tailwind. The specificity of your selector is greater than that of tailwinds utility classes. You can use the @layer
directive to fix this by defining your class like so:
@layer components {
.item {
@apply text-black font-light text-opacity-80 py-4 bg-gray-100;
This declares your class before tailwind's utility classes and so should have lower specificity.
Reference: https://tailwindcss.com/docs/functions-and-directives#layer
–
–
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.