Sub topic 2
Setting up Tailwind in a Vite and React project
This chapter assumes you can already build React components with props and JSX, and that you are comfortable running npm scripts and editing a Vite project. By the end you will have Tailwind CSS v4 compiling in a React and TypeScript app, and you will know how to check it works and what to look at when it doesn't.
How the pieces fit together
Tailwind v4 ships a Vite plugin. When Vite starts, the plugin scans your source files for class names, generates only the CSS those classes need, and serves it through your normal stylesheet import. You don't need a PostCSS config or a tailwind.config.js file for a basic setup.
Step 1: Create the project and install Tailwind
If you don't have a project yet, create one with the React and TypeScript template. Then install Tailwind and its Vite plugin.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm install
npm install tailwindcss @tailwindcss/viteStep 2: Register the plugin
Open vite.config.ts and add the Tailwind plugin next to the React plugin.
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
})The order of the two plugins doesn't matter here. Both must be in the array.
Step 3: Load Tailwind in your stylesheet
Replace the contents of src/index.css with one line.
@import "tailwindcss";This single import pulls in Tailwind's base reset, its theme, and its utilities. The v3 directives @tailwind base, @tailwind components and @tailwind utilities are not used in v4.
Step 4: Import the stylesheet from your entry file
Tailwind only runs if something imports the CSS file. In src/main.tsx, make sure the import is present.
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>
)Step 5: Check that it works
Replace src/App.tsx with a component that uses a few utilities, then start the dev server with npm run dev.
export default function App() {
return (
<div className="max-w-sm rounded-lg border border-gray-200 bg-white p-6 shadow-sm">
<h1 className="text-lg font-semibold text-gray-900">Tailwind is working</h1>
<p className="mt-1 text-sm text-gray-600">Styled only with utility classes.</p>
</div>
)
}You should see a white card with a thin gray border, a soft shadow, and a bold heading. If you see unstyled text, go through the checks below.
Common setup errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No styles at all | `index.css` is not imported in `main.tsx` | Add `import './index.css'` |
| Styles missing, no errors | Plugin not in the `plugins` array | Add `tailwindcss()` to `vite.config.ts` |
| Build error about unknown directives | v3 `@tailwind` lines left in the CSS | Replace them with `@import "tailwindcss";` |
| Config change has no effect | Dev server still running with the old config | Stop and restart `npm run dev` |
Exercise
Your app shows completely unstyled HTML after you installed Tailwind and added @import "tailwindcss"; to src/index.css. Name two things you would check first.
Show answer
Check that src/main.tsx (or your entry file) imports ./index.css, and check that tailwindcss() from @tailwindcss/vite is in the plugins array in vite.config.ts. Restarting the dev server after changing the config is also worth doing.