19

I created a React application using Vite and used the documentation provided here to install Tailwind: Tailwind CSS Installation Guide.

However, it's not picking up any of the Tailwind styles.

tailwind.config.cjs

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

index.css

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

main.tsx

import React from "react";
import ReactDOM from "react-dom/client";
import { RouterProvider } from "react-router-dom";
import { router } from "./router";
import "./index.css";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
  <React.StrictMode>
    <RouterProvider router={router} />
  </React.StrictMode>
);

The only dependency I have installed is react-router-dom.

How can I fix this?

3

11 Answers 11

24

Here are the Changes I made in vite.config.js and it worked

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from 'tailwindcss'

export default defineConfig({
  plugins: [react()],
  css: {
    postcss: {
      plugins: [tailwindcss()],
    },
  }
})
Sign up to request clarification or add additional context in comments.

2 Comments

it is mentioned in the docs: one needs to run the npx tailwindcss init -p command which creates postcss config file, and then you don't need the css: {...} config in vite.config.js
@DimaKnivets I already had a postcss file with the proper settings, and this change finally worked for me vue3/vite, after NOTHING else did. Not even running the npx command
14

If you have installed postcss and autoprefixer (as outlined in step #2 here: https://v3.tailwindcss.com/docs/guides/vite), you should see both libraries in the "devDependencies" section of your package.json. If they are installed, you will need a postcss.config.cjs file in the root of your project which contains the following:

module.exports = {
  plugins: [require("tailwindcss"), require("autoprefixer")],
};

2 Comments

Thank you, I had all three libraries installed in devDependecies, postcss.config.cjs was not there, it helped. But it's not mentioned in the documentation. And thanks for correcting the question grammatically.
Running command npx tailwindcss init -p will create postcss
11

Double-check to see that you imported the index.css file. In my case, the silly mistake I made was that, while removing some boilerplate code, I inadvertently removed the 'import "./index.css"' line in my main.jsx.

import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
**import "./index.css";**

Comments

9

After following the guides from the official documentation, you may want to check in your package.json file and see under the devDependencies if tailwind is appearing there or not. Also, check in your postcss.config.cjs file and see if the following is there:

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

Hope this helps.

1 Comment

Thank you, posts.config.cjs was not there, it helped.
3

To solve this issue, you need to run this command line below, to generate tailwind.config.js and postcss.config.js files inside the root of your project, rerun your app and should be fixed. visit: https://v3.tailwindcss.com/docs/guides/vite

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p

Comments

3

Update (TailwindCSS v4 with Vite)

I faced the issue setting up TailwindCSS v4 in my React Vite Project. Finally I fixed it and I noticed that the whole process has changed, Now we no longer need to setup postcss or install autoprefixer. You will be amazed to know how easy the process has become. Here are the steps:

  1. First install tailwindcss and @tailwindcss/vite via npm.

     npm install tailwindcss @tailwindcss/vite
    
  2. While creating react Vite Project, a configuration file by the name vite.config.js was created. We need to add the @tailwindcss/vite plugin to your Vite configuration. This will be your final setup of vite.config.js file

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite' // We have added this line

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), tailwindcss()] // & We have added tailwindcss() here
})

TailwindCSS-4 setup with Vite

  1. Then import tailwind to your project in index.css file:

    @import "tailwindcss";
    

index.css file importing tailwind

  1. Then run npm run dev & start using tailwind in your React Vite Project successfully.

  2. You package.json should have the following tailwind packages:

Package.json file update

Try writing some tailwind code in your React Components and see the magic for yourself & appreciate the updates.

Working fine now

Official Docs to keep an eye for future updates:

https://tailwindcss.com/docs/installation/using-vite

1 Comment

This really helped me and none other worked because I was using latest tailwind css.
2

Your postcss.config.js file is missing. Run command npx tailwindcss init -p This will create postcss.config.js file with the following code

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

Comments

1

In my case the problem came from tailwind.config.js, index.html was missing from the content.

export default {
  content: [
    "./index.html",
    "./src/**/*.{js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

Comments

0

In my case, I had css: { postcss: { ... } } in my vite config and separate postcss.config.js. Vite postcss config has a priority over postcss.config.js. And since I've tailwind config only in my postcss.config.js file, vite just ignored it completely.

Comments

0

in my case the problem was the you need to specify the correct purge

module.exports = {
      ---------> purge: ['./resources/**/*.blade.php', './resources/**/*.js', './resources/**/*.vue'],
      darkMode: false, // or 'media' or 'class'
      theme: {
        extend: {},
      },
      variants: {
        extend: {},
      },
      plugins: [],
    };

Comments

0

No need of using all of these imports or extra libraries.

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

Install Tailwind into your react application using following Command

npm install tailwindcss @tailwindcss/vite

Then remove your App.css file and it's reference from App.tsx

import './App.css' -- remove this in App.tsx

Also remove css classes in index.css like following

a {
  font-weight: 500;
  color: #646cff;
  text-decoration: inherit;
}
a:hover {
  color: #535bf2;
}

body {
  margin: 0;
  display: flex;
  place-items: center;
  min-width: 320px;
  min-height: 100vh;
}

h1 {
  font-size: 3.2em;
  line-height: 1.1;
}

button {
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 0.6em 1.2em;
  font-size: 1em;
  font-weight: 500;
  font-family: inherit;
  background-color: #1a1a1a;
  cursor: pointer;
  transition: border-color 0.25s;
}
button:hover {
  border-color: #646cff;
}
button:focus,
button:focus-visible {
  outline: 4px auto -webkit-focus-ring-color;
}

Then go into your vite.config.js and update the plugins like below with importing tailwind.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";
import tailwindcss from "@tailwindcss/vite";
import path from "path";

// https://vite.dev/config/
export default defineConfig({
  plugins: [tailwindcss(), react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
});

Now try add some tailwind css classes into your prefered file, this should work. I have done this for multiple times now, all the times these steps worked without an error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.