TailwindCSS - Technological watch
TailwindCSS is a utility-first CSS framework for rapid UI development.
CSS problematics
CSS is a language created in 1996. And since 1996, developers are struggling about design and how to structure their CSS. Spoiler : at the end, it is often ugly and unmaintainable !
To avoid problems, some technics and CSS frameworks have born to give class naming conventions. The most popular are Bootstrap, Fundation or Bulma, which are a high level component philosophy. Out of the box, it provides generic classes which we can use.
Those frameworks provide components like buttons, dropdowns, cards, forms … For example, if we need styled components, we can write:
Here, we don’t focus about the style of the components, but we focus about the role of a component.
High level component approach is great. We only have to define a role for each component, and the framework built a UI for us. But a big disadvantage is that it is very hard to customize.
Philosophy of TailwindCSS
There are only two hard things in Computer Science: cache invalidation and naming things — Phil Karlton
Utility first
TailwindCSS is a very different CSS Framework. Instead of offering high level components to use, Tailwind offers a more functional approach.
To display a card, we will write this instead:
The approach here is to use a lot of utility classes.
flex
meansdisplay: flex
bg-white
meansbackground-color: white
mx-auto
meansmargin-left: auto; margin-right: auto
- etc…
The approach is very different ! Instead of defining roles, we define styles.
Tailwind offers thousands and thousands of classes we can use.
And why is it more maintainable ? Isn’t it better to separate HTML and CSS ? Actually, Tailwind community says that this organization gives a much more maintainable CSS structure. 🤷🏼♂️
Huge teams create a bunch of css class names that override other developers CSS classes etc… then some get deprecated so now you have CSS classes in your CSS file that don’t exist anymore etc…
Utility classes aren’t the same as inline styles
Do you think these two lines are equivalent ?
But why ? We already have the style
attribute we can apply on HTML tags.
Let’s dig out that !
Constants
First, Tailwind offers a lot of constants that we can use.
For example, tailwind offers :
- A huge color palette : https://tailwindcss.com/docs/customizing-colors
- Margin and padding constants : https://tailwindcss.com/docs/margin
- Defaults font sizes, line heights…
- A grid / flex system with default values (like
1/2
,5/12
…) - Shadows
- Border radius
- Transformations
- And much more !
Responsive design
Tailwind offers default breakpoints we can use. We can conditionally apply styles if we are on a mobile, tablet, computer, TV, printer… The syntax is very nice.
Here, the image will have a width:
Size 16
by default on small devicesSize 32
on medium size screens (@media (min-width: 768px)
)Size 48
on large size screens (@media (min-width: 1024px)
)
Hover, focus, states…
With tailwind, we can define Pseudo-classes, Pseudo-elements, Attribute selectors, and so much more…
https://tailwindcss.com/docs/hover-focus-and-other-states#pseudo-classes
These states aren’t available on inline styles
We can even add an !important
property by adding a !
before the class name !
Dark mode
Tailwind offers a dark mode strategy :
Here, the background color will change if the user requests a dark theme or not.
Combinations
And of course we can combine as we want the selectors : 😊
Classes generations
And even you need more, you can generate specific classes with []
syntax like this:
A lot of customization
Finally, if you aren’t satisfied, or if you want specific constant values for a specific design system, you can create a tailwind.config.js
config file, and configure as you want. Here is an example:
How TailwindCSS works
JIT (Just in time) mode
With all these classes, you might ask :
OK, but the CSS file must be HUGE with all these CSS classes ?!
With the tailwind.config.js
config file, you can specify where the HTML code is, and Tailwind will automatically parse and generate a (small) CSS file with only the used classes.
Plugins
CSS Plugins
TailwindCSS offers some other plugins to customize forms, aspect ratio or line clamp.
A very interesting one is the typography plugin.
[…It] provides a set of prose classes you can use to add beautiful typographic defaults to any vanilla HTML you don’t control, like HTML rendered from Markdown, or pulled from a CMS. https://tailwindcss.com/docs/typography-plugin
So we don’t need to add classes on every element to style everything !
Other plugins
TailwindCSS offers a VSCode plugin, to automatically autocomplete classes. It can be found there : https://marketplace.visualstudio.com/items?itemName=bradlc.vscode-tailwindcss.
There is a prettier plugin, which does a sorting of class name. It can be found there : https://tailwindcss.com/blog/automatic-class-sorting-with-prettier.
Design patterns
When we will use Tailwind, of course, duplication will happen :
But how to manage with that ?
@Apply
If you still love codding in CSS, you can create your CSS components like this:
This approach works, but it is not the recommended approach.
Component approach
The component approach is to export our code to external partials.
Then, we can reuse the components ! No duplications anymore !
Conclusion
Advantages
-
TailwindCSS offers a lot of personalization with its functional approach. It offers a lot of constants and shortcuts that we can use to compose our page. Development is faster ! 🚀
-
No more struggling to name things! TailwindCSS also helps avoid name collisions by enforcing rules for CSS classes.
-
Tailwind removes the fear of breaking stuff when we are doing a design update. CSS is more maintainable !
-
As Tailwind is close from CSS, upgrades are very easy to make ! 🔥 If you are using bootstrap, migrating from Bootstrap 4 to Bootstrap 5 is a hell ! 🌋
-
CSS won’t become wider and wider as the project grows 😊 If you don’t use classes, Tailwind won’t generate them. No CSS dead code anymore !
-
The ecosystem of Tailwind is very nice. Development is comfortable with the VSCode plugins. It is very easy to include it in a ReactJS or VueJS project too !
-
The documentation of TailwindCSS is great and has a lot of examples !
Disadvantages
-
Tailwind needs a lot of tools to work (Tailwind CLI, setup config, install the VSCode extension…)
-
Styling and HTML are mixed. The HTML can become ugly.
-
Learning Tailwind takes time. OK, Tailwind is intuitive, but the first days may be hard because you have to always look at the documentation.
-
Tailwind isn’t a standard. Classnames may change over the time.
-
Tailwind doesn’t come with any pre-styled components. So if you don’t know how your website will look, starting may be harder.
- Note: You can find some pre-designed components on the internet. The official dev team of Tailwind sells a pack of nice looking components : https://tailwindui.com/components
My opinion
For large projects, Tailwind is the perfect choice for me !
If you already know CSS, Tailwind is worth to learn. It changes the way you develop. But if you aren’t familiar, first spend time to learn CSS instead. Yes, HTML is ugly, but please at least try it 😊 !
TailwindCSS powers my blog, and I am very happy with it ! I could design very quickly with all the constants and helpers. A lot of configuration is already made.
All the styles are in the same place : so no separate media queries in CSS files !
I loved the developer experience and how Tailwind integrates well with Prettier or VSCode !
Go to further
- The blog post of Adam Wathan, the creator of Tailwind: https://adamwathan.me/css-utility-classes-and-separation-of-concerns/
- A small playground to play with Tailwind: https://play.tailwindcss.com/
- Tailwind UI, a website where you can find tailwind components: https://tailwindui.com
- The official Youtube channel of Tailwind team: https://www.youtube.com/@TailwindLabs/videos
Atomic design - Technological watch
Learn what is the atomic design in less than 5 minutes !
Redis - Technological watch
Learn what is Redis in less than 5 minutes !
Svelte - Technological watch
Learn what is Svelte in less than 5 minutes !
Jenkins - Technological watch
Learn what is Jenkins in less than 5 minutes !
PicoCSS - Technological watch
Learn what is PicoCSS in less than 5 minutes !
OpenAPI / Swagger - Technological watch
Learn what is OpenAPI / Swagger in less than 5 minutes !
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
RabbitMQ - Technological watch
Learn what is RabbitMQ in less than 5 minutes !