Playing Next Lesson In
seconds

Let's Learn AdonisJS 6 #2.6

Setting Up Tailwind CSS

In This Lesson

We'll learn how to install and configure PostCSS and Tailwind CSS within our AdonisJS 6 project using Vite.

Created by
@tomgobich
Published

⚠️ Quick Note

TailwindCSS released v4 on Jan 22nd, 2025 which contains a few breaking changes to the installation process. For the most up-to-date installation guide. Please refer to the AdonisJS framework guide in the TailwindCSS documentation.

Tailwind v3

Since the release of this lesson, we've learned an alternative way we can register our PostCSS plugins within our application. The way shown in our lesson is perfectly valid and works great, however, instead of registering our PostCSS plugins in a separate postcss.config.js file, like the below.

// postcss.config.js

export default {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  },
}
Copied!

We can instead define these plugins directly within our vite.config.js, which allows us to eliminate one extra file within the root of our project structure.

We can do this using the css configuration option within our Vite configuration and nesting a postcss configuration within it, like below.

// vite.config.js

import { defineConfig } from 'vite'
import adonisjs from '@adonisjs/vite/client'
import autoprefixer from 'autoprefixer'
import tailwindcss from 'tailwindcss'

export default defineConfig({
  plugins: [
    adonisjs({
      entrypoints: ['resources/js/app.js'],
      reload: ['resources/views/**/*.edge'],
    }),
  ],

  css: { 
    postcss: { 
      plugins: [tailwindcss, autoprefixer] 
    } 
  },
})
Copied!

As you can see above, we can define our PostCSS configuration directly within our Vite configuration. The only difference is, we'll need to import our plugins directly. Once that's set, we can delete our postcss.config.js file and everything will behave just the same as before!

Join the Discussion 8 comments

Create a free account to join in on the discussion
  1. @adonisdev

    thank you so much for wonderful tutorial!

    I have all changes working as described in tutorial but I dont see vscode code completion suggestions for css classes etc.

    It would be great if you can share information on how to configure vscode to show those autocomplete suggestions.

    1
    1. Responding to adonisdev
      @tomgobich
      1
  2. @secondman

    You can also set your postcss directly in package.json and avoid imports or extra files altogether.

    {
      "postcss": {
        "plugins": {
          "postcss-import": {},
          "tailwindcss/nesting": {},
          "tailwindcss": {
            "config": "./tailwind.config.js"
          },
          "autoprefixer": {}
        }
      },
    }
    Copied!

    As you can see here I also use Tailwind's nesting and postcss-import (yes order does matter)

    Cheers

    1
    1. Responding to secondman
      @tomgobich

      That's awesome! Thank you for sharing, secondman!! 😁

      0
  3. @cepoumian

    Hi. Anyone having issues with npx tailwindcss init -p, just install Tailwind at version 3 :-)

    1
    1. Responding to cepoumian
      @tomgobich

      Yeah, I should probably add a note in this video for that. npx tailwindcss init -p is a TailwindCSS 3 command. TailwindCSS 4's default installation doesn't include a tailwind.config.js file anymore and comes with its own Vite plugin for simplified configuration.

      2
      1. Responding to tomgobich
        @ikeh-victor

        just saw this now after my complaint. thank you

        2
        1. Responding to ikeh-victor
          @tomgobich

          Oh shoot - I recorded these updates and got the Inertia one updated but missed this one. I'll get this updated after work!

          1