Tailwind CSS - Using with Preprocessors


Preprocessor is a scripting language that extends CSS and gets compiled into regular CSS syntax, so that it can be read by your web browser.

As Tailwind is PostCSS Pluing so you can use preprocessors like SASS, LESS and Stylus but there are some tricks to use them. If you already know a few syntaxes of these preprocessors may those not works with tailwind as it used to do with regular CSS. For the guide please go through this article.

For the best development experience, we highly recommend that you use PostCSS plugin, and that you dont use Sass or Less preprocessors.

Using PostCSS as Preprocessor

PostCSS is recomended preprocessor to use with tailwind CSS if you are working on a new project then dont need to integrate it with any existing Sass/Less/Stylus stylesheets, you should highly consider relying on other PostCSS plugins to add the preprocessor features you use instead of using a separate preprocessor.

  • Your builds will be faster: Since your CSS doesnt have to be parsed and processed by multiple tools, your CSS will compile much quicker using only PostCSS.
  • No quirks or workarounds: Because Tailwind adds some new non-standard keywords to CSS (like @tailwind, @apply, theme(), etc.), you often have to write your CSS in annoying, unintuitive ways to get a preprocessor to give you the expected output. Working exclusively with PostCSS avoids this.

Build-time imports

One of the most useful features preprocessors offer is the ability to organize your CSS into multiple files and combine them at build time by processing @import statements in advance, instead of in the browser. To insall PostCSS run the following command.

npm install -D postcss-import

Then add it as the very first plugin in your PostCSS configuration

// postcss.config.js
module.exports = {
  plugins: {
    'postcss-import': {},
    tailwindcss: {},
    autoprefixer: {},
  }
}

The 'postcss-import' is strict that it's not allowed to use '@import' statements anywhere except at the very top of a file.

Wont work, @import statements must come first
/* components.css */

.btn {
  padding: theme('spacing.4') theme('spacing.2');
  /* ... */
}

/* Will not work */
@import "./components/card";
Use separate files for imports and actual CSS
/* components.css */
@import "./components/buttons.css";
@import "./components/card.css";
/* components/buttons.css */
.btn {
  padding: theme('spacing.4') theme('spacing.2');
  /* ... */
}
/* components/card.css */
.card {
  padding: theme('spacing.4');
  /* ... */
}

Nesting

We suggest using our bundled tailwindcss/nesting plugin, a PostCSS plugin that wraps postcss-nested or postcss-nesting and serves as a compatibility layer to ensure that your preferred nesting plugin is capable of correctly understanding Tailwind's unique syntax, in order to provide support for nested declarations.

The tailwindcss package already has it, so all you have to do to utilize it is add it to your PostCSS settings, just before Tailwind.

// postcss.config.js
module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': {},
    tailwindcss: {},
    autoprefixer: {},
  }
}

Under the hood, it employs the postcss-nested plugin by default, which powers nesting support in the Tailwind CSS plugin API and has a syntax similar to SASS.

If youd rather use postcss-nesting, first install the plugin.

npm install -D postcss-nesting

Then pass the plugin itself as an argument to tailwindcss/nesting in your PostCSS configuration.

// postcss.config.js
module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': 'postcss-nesting',
    tailwindcss: {},
    autoprefixer: {},
  }
}

If you wants to use specific version of postcss-nested and want to override the version we bundle with tailwindcss/nesting itself.

If you are using postcss-preset-env, you should disable nesting and let tailwindcss/nesting handle it for you instead.

// postcss.config.js
module.exports = {
  plugins: {
    'postcss-import': {},
    'tailwindcss/nesting': 'postcss-nesting',
    tailwindcss: {},
    'postcss-preset-env': {
      features: { 'nesting-rules': false },
    },
  }
}

Variables

As CSS supports variables so no need to use a preprocessor to use variables at all. To know more about the CSS Variables.

:root {
  --theme-color: #52b3d0;
}

/* ... */

.btn {
  background-color: var(--theme-color);
  /* ... */
}

Used varibales can be replaceable with tailwind 'theme()' function. This gives you access to all of your design tokens from your tailwind.config.js file directly in your CSS.

.btn {
  background-color: theme('colors.blue.500');
  padding: theme('spacing.2') theme('spacing.4');
  /* ... */
}

Vendor Prefixes

Autoprefixer will help you to manage vendor prefix in your CSS.

/* install it via npm */
npm install -D autoprefixer

/*  Add it to the very end of your plugin
    list in your PostCSS configuration */

module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  }
}

Installing Tailwind as a PostCSS Plugin

Before installing PostCSS plugin you should install Tailwind CSS.

Prerequisite

To install PostCSS pleae follow the below mentioned steps.

Step 1: Add Tailwind to your PostCSS Configuration

/* postcss.config.js */
module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  }
}

Step 2: Configure your Template Paths


Step 3: Add the Tailwind directives to your CSS

/* tailwind.config.js */
/** @type {import('tailwindcss').Config} */
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

Step 4: Start your build Process

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 5: Start using Tailwind in your HTML

npm run dev