6 reasons to use Tailwind over traditional CSS
Tags: css, tailwindcss, react, vue
Published: 2021-08-05 08:46:50

<!-- wp:heading -->
<h2>1. Tiny bundle size = incredible performance</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When in development mode, the size of Tailwind's output is pretty large. This is by design: Every possible class is generated at this stage so you don't need to wait for a build process to finish when you want to use something.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Once Tailwind CSS is put into production mode, however, it will <strong>purge</strong> any classes which aren't used with a tool called PurgeCSS. This works by searching through your project files for the names of the classes, keeping only those which are used. You can configure which file paths it will search through in your <code>tailwind.config.js</code> in the <code>purge</code> array.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// tailwind.config.js
module.exports = {
  purge: &#91;
    './src/components/**/**/*.jsx',
    './src/layout/**/**/*.jsx',
    './src/pages/**/**/*.jsx',
  ],
  theme: {},
  variants: {},
  plugins: &#91;],
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>It's quite easy to get a very small CSS bundle size without even trying, which really helps in getting your performance score up. You'd be pretty hard pressed to get a bundle size over 10kb, so websites that use Tailwind tend to load very quickly without needing much optimisation on the CSS front - so long as the rest of the site is also optimised.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>All you need to do is make sure you always reference classes by their full name, not by building names using JavaScript.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// bad
&lt;div className={ 'bg-' + props.isDark ? 'neutral-dark' : 'neutral-light' }&gt;

// good
&lt;div className={ props.isDark ? 'bg-neutral-dark' : 'bg-neutral-light' }</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Also, while this would go mostly unnoticed on faster machines, the simplicity of Tailwind classes, in that they aren't built using <a href="https://csswizardry.com/2011/09/writing-efficient-css-selectors/">complex selectors</a>, means your browser is a little bit faster when parsing and rendering CSS. This helps with performance on slower computers and old phones. As someone whose phone was already slow when I bought it 6 years ago, I'll take any improvement I can get.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>2. Prototype and build quickly</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>With regular SCSS, you need to write custom classes for every single element on your page. While it can offer finer control, writing custom classes takes a decent amount of time: You have to add the class in the HTML, then create it in the CSS, and then write out every property in long format. Then you have to wait for the CSS to build before you can see the result - plus, if you need to make more changes you'll need to rebuild every time, which can take valuable seconds and interrupt your flow.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Tailwind CSS takes out those extra steps and gives you a simple, snappy developer experience when styling elements. You see the element you want to style, add the properties it needs using shorthand, and the changes appear very quickly without having to wait for a CSS bundle. Your attention can stay in one place so you aren't switching around to different files constantly, and the whole process just feels <em>simple</em>.</p>
<!-- /wp:paragraph -->
<code>
<div class="bg-white rounded p-4">
  <h1 class="text-24 font-heading underline">
    Foobar
  </h1>
  <p class="text-16 font-body>
    Hello world!
  </p>
</div>
</code>
<!-- wp:paragraph -->
<p>I can understand that it may take a while to learn all the shorthand. There's some good tools to help with this, chiefly the <a href="https://tailwindcss.com/docs">official docs</a> or <a href="https://twitter.com/nerdcave">@nerdcave</a>'s <a href="https://nerdcave.com/tailwind-cheat-sheet">Tailwind cheat sheet</a>. But to be honest, after spending a week or so using it you will have remembered most of the important stuff. There's a couple of weird gotchas, such as not having classes for <code>flex-basis</code>, but there's always a good reason: In this case, you can just use the width attributes such as <code>w-1/2</code> in its stead. It just takes a little bit of time, but has the potential to save a whole lot more.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've used Tailwind in some production sites that had very tight deadlines and I have to say, it definitely took a lot of pressure off when it came to styling up the frontend. If you're on a tight deadline, Tailwind could make life a little easier.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>3. It handles CSS variables automatically</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Tailwind generates a file called <code>tailwind.config.js</code>. This file contains all your settings for your theme, including colours, widths, spacing (used for padding, margins and similar properties), fonts and so on. Your entire CSS bundle and all the classes you use will be based on this file.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Each property can be assigned a name which will be how you can refer to the classes associated. For example:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// tailwind.config.js

colors: {
  brand: {
    DEFAULT: '#f0000',   // bg-brand
    primary: '#f0000',   // bg-brand-primary
    secondary: '#f0000'  // bg-brand-secondary
}

// other ways to use these colours
.text-brand
.border-brand-secondary</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>And you can change all these properties any time you like, the names will stay the same unless you change them. So Tailwind will handle all of your CSS variables - it influences the entire look of your site. <em>This </em>is your source of truth - if a brand colour changes, or you need to change the fonts everywhere, <code>tailwind.config.js</code> is where you can do that.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>4. Eliminates scope leak</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>One of the traits people love of BEM and why it's so popular today, is that the naming system classes are built to represent the structure of a component. While making it easy to read and understand, developers also benefit from a side effect of this structure: Since the layout is easy to understand, you can write classes without using CSS selectors.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// this
.block {
  &amp;__element { ... }
  &amp;__element--modifier { ... }
}

// not this
.block {
  &amp; > .element {
    &amp;.modifier { ... }
  }
}</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The problem with CSS selectors is they introduce complexity into CSS where components become very dependent on a particular HTML structure. Since generic CSS classes such as <code>.container</code> can repeat a lot, using these names can lead to overlap, where changing one class will affect many other classes. It's one of the main reasons we use BEM, because it makes this structure clear and flattens every class to the top-level scope so nothing depends on anything else. Working without a CSS methodology like this can lead to headaches, and keeping it as simple as possible means more people can understand it more easily.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The way utility-first CSS works is by embracing this idea: Don't have anything depend on anything else. In Tailwind classes are single purpose, generally made up of only one or two CSS properties, and they never depend on each other except for intentional exceptions, such as the group hover feature. You really don't even need to think about scope leak, because unless you add those selectors yourself with custom CSS, the option to nest classes in complex selectors isn't really built in.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// this is all tailwind classes are
.w-1/2 {
  width: 50%;
}
.mx-auto {
  margin-left: auto;
  margin-right: auto;
}</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// using hover groups
&lt;div class="group bg-white hover:bg-black">
  &lt;h3 class="text-grey group-hover:text-black group-hover:underline">Title&lt;/h3>
  &lt;p class="text-grey group-hover:text-black"
&lt;/div></code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>What I really like about it is that there's no need for vague, overly specific or generally throwaway class names. You don't need to name every text style, every random CSS shape or flourish. Again, you're still free to comment the HTML if you need to, but sometimes there's no need to name every class that only has <code>display: flex</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>What if I still need to use selectors?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Of course, there are times when selectors may be the best option, such as for styling WYSIWYG blocks, but generally avoiding selectors can help to maintain an easy to read and understand codebase. Remember you can still use SCSS alongside Tailwind if you prefer. If you do need to use selectors, prefixes can help to keep track of what's custom CSS and what's not. I like to use this method for WYSIWYGs:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>// typography.scss

.t-wysiwyg {
   &amp; p {
     font-size: 12px;
   }
   &amp; strong {
     @apply font-bold;
   }
}

.t-wysiwyg--contact { ... }</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>5. SCSS is still there if you need it</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It's good to keep in mind that you don't need to use Tailwind CSS <em>instead of</em> SCSS. There will still be times when you need to create an element that uses :<code>before</code> and <code>:after</code> elements, or when you need a custom keyframe animation. When this happens, you can just use SCSS or any other preprocesser in the exact same way you did before - no need to make a choice between the two.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> I prefer to use Tailwind for all the heavy lifting, and for elements that need CSS features I can switch over to SCSS. This allows me to work much faster while still achieving a high level of quality, customisation and detail.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The simplicity of using shorthand classes over long, descriptive class names does feel freeing. There's no denying that BEM or a similar methodology's class names are helpful - you're informed on exactly what a class is meant to do. At least, that's the theory. The issue is that when <em>every</em> element needs to have a unique class name, you end up with some vague, strange, or downright daft classes. It can be difficult to get used to not having classes described, but once you have a good understanding of the syntax it's just like reading regular CSS, and you can always comment your HTML with a description of what the class is.</p>
<!-- /wp:paragraph -->

<p>In my other post, <a href="https://dev.to/npmrundev/how-to-keep-tailwind-dry-2jfe">How to keep Tailwind DRY</a>, I explore how we can make use of JavaScript frameworks such as Vue or React to build truly DRY applications that mesh with the utility-first way of working.</p>

<!-- wp:code -->
<pre class="wp-block-code"><code>&lt;-- Contact Section - 2 column, 1 column on mobile -->
&lt;div class="flex flex-row items-center">
  &lt;-- Map -->
  &lt;div class="w-full tablet:w-1/2">
    &lt;img src="map.jpg" />
  &lt;/div>
  &lt;-- Content -->
  &lt;div class="w-full tablet:w-1/2">
    &lt;h3>&lt;/h3>
    &lt;p>&lt;/p>
    &lt;ul>&lt;/ul>
  &lt;/div>
&lt;/div></code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2>6. Works great with JavaScript frameworks</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>It should be known that Tailwind is not going to be DRY when used in an environment where you'll be copy and pasting HTML, so don't expect things to go well if you're working with plain HTML files. Once you start copy and pasting like this, you no longer have a central source of truth where you can change the way your components look, since the source of truth for your website styling is no longer tied to CSS files.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Modern JavaScript frameworks are where Tailwind's features can really shine. This is because frameworks like Vue or React are based around creating reusable components, in order to minimise the amount of HTML that needs to be written. These components are the building blocks of your application, and can be a simple container or a large component composed of smaller components.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>These components are stored in their own custom files which combine HTML and JS, and in Vue you have the option to combine your CSS into a single file as well. This means your sources of truth no longer need to be separated by file type, they can all exist in the same file which handles a single component. This is what's known as component-based development, which JavaScript frameworks embrace with open arms: we've seen styled-components, CSS-in-JS, and now Tailwind, all of which help to contain all the CSS related to a component within that component. They can still be split out into different files, but if you want to make a change to that component you will be going straight to that component's folder instead of three separate locations.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":191,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://npmrundev.files.wordpress.com/2021/08/filez.png?w=275" alt="" class="wp-image-191"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>I've written about this concept more in my other post, <a href="https://npmrundev.wordpress.com/2021/08/01/learning-to-love-tailwind/">How to love Tailwind</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I'll be the first to admit that Tailwind CSS probably isn't the right choice in every situation. It has some really fantastic features, it's really speedy and it compiles down to ridiculous file sizes, but it requires a totally different mindset and set of tools to take advantage of these features without getting frustrated.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>I've noticed a lot of teams, particularly those building headless sites with frontend frameworks, are adopting Tailwind and solutions like it. I think we're still in the early days of this approach and there's a lot to figure out, but the potential is huge. I'm excited to see where it goes in future.</p>
<!-- /wp:paragraph -->