You can think of Tailwind utilities as components — because you can literally make a card “component” out of Tailwind utilities.
@utility card { border: 2px solid black; padding: 1rlh;}<div class="card">...</div>
This blurs the line between “Components” and “Utilities” so we need to better define those keywords.
The Great Divide — And The Great Unification
CSS developers often define Components and Utilities like this:
- Component = Group of Styles
- Utility = Single Rule
This collective thinking has emerged from the terminologies and literature we have gathered over many years. Unfortunately, they’re not really the right terminologies.
So let’s take a step back and consider the actual meaning behind these words.
Component means: A thing that’s a part of a larger whole
Utility means: It’s useful.
So…
- Utilities are Components because they’re still part of a larger whole.
- Components are Utilities because they’re useful
The division between Components and Utilities is really just a marketing effort designed to sell those utility frameworks — nothing more than that.
It. Really. Doesn’t. Matter.
The meaningful divide?
Perhaps the only meaningful divide between components and utilities (in the way they’re commonly defined so far), is that we often want to override component styles.
So it kinda maps this way:
- Components: Group of styles
- Utility: The style used to override component styles
Personally, I think that’s a very narrow way to define something that actually means “useful”.
Just overriding the damn style
Tailwind provides us with an incredible feature that allows us to override component styles. To use this feature, you would have to:
- Write your component styles in a
componentlayer - Override the styles via a Tailwind utility
@layer components { .card { border: 2px solid black; padding: 1rlh; }}<div class="card border-blue-500">...</div>
But this is a tedious way of doing things. Just imagine writing @layer component in all of your component files. There are two problems with doing that:
- You lose the ability to use Tailwind utilities as components
- You gotta litter your files with many
@layer componentdeclarations — which is one extra indentation and makes the whole CSS a little more difficult to read…
There’s a better way of doing this. (See the next lesson).
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson