Skip to main content

Command Palette

Search for a command to run...

Why you absolutely need to try TailwindCSS

Updated
5 min read
Why you absolutely need to try TailwindCSS
Q

Web lover, entrepreneur, interested by web-design, UX and open-source. Part-time traveller, space enthusiast and eternal optimist. Technical Sales Engineer @SpireGlobal.

Some background

I've started using TailwindCSS earlier this year after months of listening to a friend that had been recommending it to me. For some time, I was thinking: "Hmm, this will be yet another CSS framework with some built-in grid system, some default designs and UI's. Not so interested."

I could not have been more wrong.

For starters, TailwindCSS that was first released on alpha back in 2017. Instead of being a component based framework, it is much more utility based, and focuses its attention on actually changing the way you use CSS.

I cannot put enough emphasis on how using Tailwind actually changes the way you write and organise your CSS.

Tailwind core concept is built around its huge library of css utilities, that are totally composable as well as very verbose and easy to understand. With Tailwind, here is how a simple button might look like:

<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
  Button
</button>

You immediately get the idea, and that's the beauty of it. It's blue, changes colour on hover, has some bold white text with some padding and rounded edges. As simple as that.

But this would not be enough to make Tailwind the powerful tool that it is today. Its true strength comes from its configuration system, that will generate a whole custom and unique CSS framework for you and you only. Let's go through that.

It all starts with a configuration file

As many custom tools that are fully customisable, everything starts with a configuration file. Introducing your very first tailwind.config.js. Inside it, a lot of magic is gonna happen. Here you can:

  • Select exactly what you want to have in your build
  • Create your own custom theme (colors, fonts, spacing ...)
  • Add your own/some community plugins
  • Add some CSS purging
  • Add a custom prefix
  • Customise Tailwind experimental features

The whole point of that configuration is that you only get what you use in your build. No huge CSS file with 90% of unused CSS. That time is over ! Tailwind will even take care of some CSS purging for you if you enable it !

A typical configuration file can look like this:

module.exports = {
  purge: ["./src/**/*.html", "./src/**/*.vue", "./src/**/*.jsx"],
  theme: {
    extend: {
      colors: {
        "my-custom-color": `#fdd332`,
        ...
      },
    },
  },
  variants: {
    borderWidth: ["responsive", "last", "hover", "focus"],
    borderRadius: ["last", "first", "responsive", "hover", "focus"],
    backgroundColor: ["responsive", "hover", "focus", "disabled"],
    textColor: ["responsive", "hover", "focus", "disabled"],
    cursor: ["responsive", "hover", "focus", "disabled"],
  },
  plugins: [require("@tailwindcss/custom-forms")],
};

The most importants parts are the theme section as well as the variants sections.

The theme is where you will be able to either override the default attributes of a Tailwind configuration (which you can find here). You can also extend the default theme with additional attributes. The variant is where you will define which variants are usable with which attributes.

It even includes functions to reference other configuration values might you want to use them elsewhere ! Neat ! Here is an example of a manual way you can deduce a hue of another colour using (some plugins can do that for you directly).

module.exports = {
 theme: {
    backgroundColor: (theme) => ({
      ...theme('colors'),
      'gray-900-hue': `rgba(${hexToRgb(theme('colors.gray.900')).r},${hexToRgb(theme('colors.gray.900')).g},${hexToRgb(theme('colors.gray.900')).b}, 0.98)`,
    }),
  },
}

What about CSS outside HTML ?

I can already see you coming: "but what about using Tailwind outside the HTML scope ? What if I want to use it in my CSS/SASS/LESS and override some other properties I have there ?" Well, Tailwind got you covered for that as well, with the extraction system.

Remember the button shown earlier ? What about defining it like this:

.btn-blue {
  @apply bg-blue-500 text-white font-bold py-2 px-4 rounded hover:bg-blue-700;
}

Interesting isn't it ? That is the power of CSS extraction. This way, you can combine your new/existing classes with the power of Tailwind, keeping it simple, maintable and unified.

The best way to make the most out of Tailwind

Tailwind is a simple idea, that when used right will basically erase the usual hassles of CSS writing from your existence. Remember having to maintain some unreadable, duplicated, messy CSS ? Tailwind offers the possibility to never have to face that anymore.

Like all frameworks, Tailwind aims to unify and standardise, and that is something that CSS is in need of. When using Tailwind, ones objective should be to:

  • NEVER write non Tailwind-based CSS
  • Having an elegant mix of inline-HTML attributes for unique elements, and classes using extraction for more generic elements

Follow those 2 simples rules as much as you can, you will be able to create a project that ANY other person that has used Tailwind in the past will be able to work on instantaneously. No hassle to understand where things are, configuration is centralised, things are standardised. The dream ❤️.

Interesting resources

For a closing note, here are some notable resources that are definitely interesting to check-out, as they make Tailwind an even more powerful beast.

Awesome TailwindCSS - A great starting point. Perfect for finding new Add-ons, reading some material, finding some examples ...

Tailwind IntelliSense - Absolutely necessary add-on for VsCode. It will help you get used to the many attributes Tailwind has to offer as well as streamlining your development more.

TailwindUI - TailwindUI is a good place to go if you're looking for some components/inspiration/examples of interesting Tailwind usage !

K

Your point about the configuration file being the starting point resonates, but I'd push back slightly on utility-first being universally better — for design-system-heavy teams with strict component libraries, CSS-in-JS solutions like Stitches or Vanilla Extract can enforce design tokens at the type level. Have you worked in a codebase where both approaches coexisted?

G

Explained well! What are your thoughts on the css-in-js approach?

1
Q
Quentin5y ago

I like the approach as well, but from a purely logistical perspective, mixing JS and CSS might create more bottlenecks when working with designers/integrators that only familiar with HTML/CSS.

The CSS-in-JS approach is also tricker to organise properly in my opinion ! I overall prefer to separate the two. On the other hands, some developers will find it much more convenient to work with an all-in-one solution 🙂

1
T

Hi, I have seen more and more tailwind lately. I was wondering why you would use text-white instead of style=color:white. I know inline css is bad but it feels like tailwind is just a wrapper around css.

Would love you to hear your opinion

1
Q
Quentin5y ago

Hi Thomas,

Tailwind is indeed a wrapper (I prefer the term framework at that point because it adds value not just simplify), but also takes in customisation aspects at build time (which makes generated inline-css totally legit to use, since it is totally dynamic with the generated classes/variables ...).

It also offers the variants part (the hover:text-white for example) which is easy to read/use.

Finally, as I am saying in the article, it offers the possibility for a standard across projects and companies. That's where the biggest strength is, like any framework that becomes popular.

1
B

Really insightful, thanks for sharing!

E

This is the content I signed up for Quentin. Very insightful!

1
C

Hi Quentin,

Awesome write-up buddy!

these resource list is really helpful.