But Why Tailwind, Though?
- Tailwind is not a component framework
- Tailwind is not just “inline styling”
- What is Tailwind?
- “Best practices” don’t actually work
- Small CSS bundle
- How to “learn” Tailwind
- Other resources
If you are reading this, you have probably heard people either praising or complaining about Tailwind.
Tailwind is the only mantainable way to style websites. - John Doe
Tailwind is an absolute mess of cluttered, unreadable classes. - Anonymous
Well, maybe no one has specifically said this, but you get the point. For some reason, it is always a hot topic.
What is Tailwind about? What are these “inline styles”? Why should you care?
First, let’s begin by clarifying what it is not:
Tailwind is not a component framework
Don’t get me wrong, UI component frameworks can be amazing.
But Tailwind is just about an opinionated set of low-level utility classes, that guide you to make good choices by default.
Because Tailwind is so low-level, it never encourages you to design the same site twice. Even with the same color palette and sizing scale, it’s easy to build the same component with a completely different look in the next project.
And you can also build amazing UI component frameworks on top of Tailwind, but Tailwind itself has nothing to do with other solutions that are often compared to.
There are also other similar (and really interesting) options, like unoCSS, that follow this line of thought.
Tailwind is not just “inline styling”
One of the first things that Tailwind is often blamed on, is that it is “just inline styling”.
What is the difference between using Tailwind and just adding inline style tags to the HTML elements?
There’s a few of them:
- Verbosity. Tailwind classes are concise, and some of them even map to more than one style property. With inline styles, you would end up writing much more.
- Inconsistency. No constraints on the values you choose, leading to inconsistencies in the design.
- Larger bundle size.
- No sensible defaults, design system, style reset, IDE integration or automatic sorting order of styles.
Ok so… what is Tailwind?
What is Tailwind?
Tailwind is a utility-first, component driven CSS framework.
It provides a set of opinionated utility classes, that you can use directly inside your markup.
As you can see, each of these classes has a single purpose, and they map to one or more CSS properties. You can see the full list here.
Utility classes help you work within the constraints of a system instead of littering your stylesheets with arbitrary values. They make it easy to be consistent with color choices, spacing, typography, shadows, and everything else that makes up a well-engineered design system.
In practice, a component using Tailwind, with your framework of choice, would look similar to this:
Now I know what you’re thinking, “this is an atrocity, what a horrible mess!” and you’re right, it’s kind of ugly. In fact it’s just about impossible to think this is a good idea the first time you see it — you have to actually try it. - Tailwind docs
But once you’ve actually built something this way, you’ll quickly notice some really important benefits:
-
You aren’t wasting energy inventing class names. No more adding silly class names like
sidebar-inner-wrapper
just to be able to style something, and no more agonizing over the perfect abstract name for something that’s really just a flex container. -
Your CSS stops growing. Using a traditional approach, your CSS files get bigger every time you add a new feature. With utilities, everything is reusable so you rarely need to write new CSS.
-
Making changes feels safer. CSS is global and you never know what you’re breaking when you make a change. Classes in your HTML are local, so you can change them without worrying about something else breaking.
When you realize how productive you can be working exclusively in HTML with predefined utility classes, working any other way will feel like torture.
“Best practices” don’t actually work
This is a bold statement, but in my own experience, it’s not far from the truth. It comes directly from the creator of Tailwind CSS.
I’ve written a few thousand words on why traditional “semantic class names” are the reason CSS is hard to maintain, but the truth is you’re never going to believe me until you actually try it. If you can suppress the urge to retch long enough to give it a chance, I really think you’ll wonder how you ever worked with CSS any other way. - Adam Wathan
This article explains various CSS approaches and “best practices”, from semantic CSS, BEM (Block-Element-Modifier), content-agnostic CSS, to how utility-first CSS and the use of components can avoid a lot of bloat and complexity in stylesheets.
Go check it out if you want a more in-depth read about it.
Small CSS bundle
Think about how many times you write margin: x
, or padding: x
in every CSS
file. Using semantic CSS, you need to ship every CSS property of every selector to the client.
Tailwind automatically removes all unused CSS when building for production, which means your final CSS bundle is the smallest it could possibly be. In fact, most Tailwind projects ship less than 10kB of CSS to the client.
How to “learn” Tailwind
Perhaps you want to try Tailwind and see if it’s as nice to work with as it sounds. How do I learn it?
You don’t really need to. If you already know CSS, you just have to start styling, looking up the utility classes you need to use, in a cheatsheet.
You will be slow at the start, but in a few hours or less, you will remember most of the most commonly used classes. A few special things here and there (hover states, media queries, dark mode), and you will be back to your normal pace, probably even faster.
And most importantly, you will have a highly maintainable solution to work in a team, with consistency, good defaults, a small bundle, immediate understanding of the codebase styling, and less overhead.
A few tips:
- Create and reuse components.
- Avoid using arbitrary values.
- Customize your own properties.
- Use the sorting plugin.
- Think mobile-first.