Tailwind Setup with Vite
You'll learn how to install and configure Tailwind CSS in a Vite-powered React project so you can start writing utility classes.
Why This Setup Matters
Tailwind CSS needs a build step to scan your files and generate only the CSS classes you actually use. Vite handles this through PostCSS, a tool that transforms CSS during the build process. Think of PostCSS as a pipeline: your @tailwind directives go in one end, and a complete stylesheet comes out the other.
The Installation Process
First, install Tailwind and its peer dependencies:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
This creates two files: tailwind.config.js (tells Tailwind where to look for classes) and postcss.config.js (tells PostCSS to use Tailwind).
Configuration
In tailwind.config.js, specify which files Tailwind should scan:
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
],
// ...
}
The Magic Directives
In your main CSS file (usually src/index.css), add the three Tailwind layers:
@tailwind base;
@tailwind components;
@tailwind utilities;
These directives inject Tailwind's reset styles, component classes, and utility classes respectively. Vite's dev server will process these through PostCSS automatically.
Import this CSS file in your main.jsx, and you're ready to use Tailwind classes in your components.
Key Takeaway: Tailwind setup requires installing the package, configuring the content paths so it knows which files to scan, and adding the three @tailwind directives to your CSS entry point—all of which Vite handles through PostCSS during the build.