Hi everyone. Today we're going to talk about best practices for using Tailwind CSS. My name is Nina. I'm a front-end engineer at Devilmartian. We're engaged in product development for large companies and startups, and we also develop our own open-source products.
Tailwind CSS is a popular and controversial tool. It balloons within developers into two opposing camps. Tailwind has become so popular because many developers love it for its convenience and for speeding up the layout process. But at the same time, there are developers who can't stand it, especially because of really long Tailwind CSS class lists. But the truth is somewhere in the middle.
For some tasks, Tailwind is extremely cool. We don't need to think about the name of CSS classes. And with a ready-made list of classes, we can build a project extremely quickly. But on the other hand, Tailwind is not a silver bullet. There are cases when it will just pollute the project with a huge list of classes as it grows.
After working with Tailwind for a long time, I have identified two main requirements where Tailwind is suitable, and without them, it's better to choose an alternative tool. And the first requirement is the project must have a design system. Tailwind is closely connected with the philosophy of the design system in which we use design tokens. Design tokens are atomic values such as colors, sizes or margins that are used throughout the project. If we use magic constants for the design properties everywhere, there is a high possibility that you can break something if you need to change a property, like a certain color, in all the places in the project, because you will need to search for a specific class in all your Tailwind class lists and replace it with another value. But if we use the design tokens, it becomes much easier and safer, because if we decide to change some design property, like a certain color, we can apply it in just one place, and it will be changed everywhere.
And the second requirement for using Tailwind CSS is that your project must have a component approach. The approach with CSS classes that Tailwind uses leads to verbose HTML structures, since classes are applied directly to elements. And if we can't encapsulate code into separate components, then as a project grows, the markup will become extremely difficult to read and maintain.
Here's an example of how the component approach works with Tailwind. Instead of copying and pasting a button like this everywhere with a large list of classes, we encapsulate it in a separate component and call it in one line.
Now, the good news is if you have a design system and a component approach, then Tailwind CSS may be a nice option for you. But to make long-term work pleasant and painless, you have to use this tool wisely and follow some best practices. The first, in Tailwind a long list of classes are a harsh reality, but this disadvantage should be mitigated by trying to use only those Tailwind classes that are necessary. Here are some examples of how you can reduce the number of classes and get the same result.
Comments