Tailwind encourages you to use components. Instead of copy pasting the classes all over the place, you should use a system that allows you to create and use components.
Since your question is HTML + CSS only, you don’t really have the right tools for this. But if you were using a scripting language like JS, Python, PHP etc., you could create components from elements and reuse them. Since I am familiar with React framework, I can show an example of that:
As you can see, with this approach, you extract the huge list of modifiers into a small component that you can use multiple times without much repetition in the code.
You are free to choose any tool, language, system that will enable making components. That is what Tailwind kind of expects you to do.
after this code if you hover on div element, p elements backgroundColor will be change to red.
Problem :
I am trying to group clases so the code will be cleaner and legible. In the documentation of Tailwind it talks about “@apply”, that can be used for this objective but I am using the CDN and therefore this is not working for me. So my question is, ¿Is there any form I can accomplish what I am looking for? Maybe by using SASS/SCSS or LESS?
I see what you are trying to tell me but the case is that i want to group the clases and then do what you are saying me. I mean, I want to have a amount of clases in a stylesheet where each class is a group af clases of Tailwind. So at the end I will have my custom clases using Tailwind. I dont know if you undertand what I am looking for, let me know if you don’t and I will try to improve the question.
Comment posted by Jakub Kotrs
That’s not what you
Comment posted by Hello There
So this is not a “problem” and if I have a long line of classes is not bad?. I am new with Tailwind, and when I start coding I thougth that this was not like “good practice” to have a pile of classes.
Comment posted by Jakub Kotrs
No, it’s not a problem. It can be an issue for maintenance if you have to maintain them manually, which is why we create small reusable components and not copy paste the classes everywhere, but the list is definitely not a problem on its own. Either you “get used” to it, or you will dislike it, so the subjective feeling plays a big part it this.
Comment posted by Hello There
Thanks for the help, now I understand better how to use Tailwind and the objective of it
Comment posted by Hello There
I try it and the vs code shows me this alert: “Unknown at rule @layercss(unknownAtRules)” and it dosn’t apply anything to the element with the class I stablish
Comment posted by joshxfi
I think he meant how to group tailwindcss classes like @apply (but on cdn), not how to use the group class