Reputation: 99
I'm trying to set up Tailwind CSS with Vite, but I'm getting "Unknown at rule @tailwind" errors in my index.css file. I've followed the official documentation and other solutions but can't get it working.
UPDATE: thanks for comments, I can confirm Tailwind CSS is now working with Vite, it appears dependent on the @ selectors in index.css, would be helpful to understand how to remove the VSCode Warning?
index.css
with error messagethis is where the error message occurs Unknown at rule @tailwindcss(unknownAtRules)
The CSS itself is correct according to Tailwind documentation on implemnting it into Vite, so the issue likely lies in the configuration or build process rather than the CSS directives themselves.
@tailwind base;
@tailwind components;
@tailwind utilities;
This suggests PostCSS isn't properly recognizing Tailwind's directives, despite having All required packages installed (tailwindcss, postcss, autoprefixer) and Configuration files in place (postcss.config.js, vite.config.js, tailwind.config.js)
tailwind.config.js
as per docsthis is as per official documentation on using Tailwind with Vite
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: []
}
vite.config.js
(attempted solution)This was a suggestion from Stack Overflow - Vite does not build Tailwind CSS - StackOverflow;
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from "tailwindcss";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
css: {
postcss: {
plugins: [tailwindcss],
},
},
});
App.css
instead of index.css
- same error# Remove existing node_modules and lock files
rm -rf node_modules package-lock.json
# Reinstall base dependencies
npm install
# Ensure all Tailwind-related dependencies are installed
npm install -D tailwindcss postcss autoprefixer
npm install -D postcss-import
npm install -D @vitejs/plugin-react
# Initialize Tailwind (generates tailwind.config.js and postcss.config.js)
npx tailwindcss init -p
Deleting the cache and building again with npm run build
Following solutions from this resources then repeating step 4: Vite does not build Tailwind CSS - StackOverflow;
The accepted solution suggests updating the Vite configuration to import tailwindcss directly with postcss
. I've tried this but still get the same error.
Alex Krasikau suggests Remove PostCSS config from Vite configuration and Use a separate postcss.config.js
with this structure:
export default {
plugins: {
'postcss-import': {},
'tailwindcss/nesting': {},
'tailwindcss': {},
'autoprefixer': {},
}
}
From package.json:
{
"devDependencies": {
"autoprefixer": "^10.4.20",
"postcss": "^8.4.47",
"postcss-import": "^16.1.0",
"tailwindcss": "^3.4.14",
"vite": "^4.3.2"
}
}
client/
folderThis is a Mern Template that is successfully deployed. Full Repo can be seen here: https://github.com/lmcrean/mern-template-2
.eslintrc.cjs
index.html
package.json
postcss.config.js
public/
vite.svg
src/
App.css
App.jsx
assets/
react.svg
index.css <--- error message appears here, unknown @ rule
main.jsx
tailwind.config.js
vite.config.js
Upvotes: 1
Views: 439
Reputation: 1
As mentioned in this question: unknown at rule @tailwind css(unknownAtRules) tailwind error, you should just add a new line: Item: *.scss - Value: tailwindcss (or sass)
That fixed it for me!
Upvotes: 0