Sub topic 2
Setting up Tailwind in a Vite and React project
This chapter takes a fresh Vite and React project and adds Tailwind CSS v4. You will install the packages, register the plugin, load Tailwind from your stylesheet, and confirm that it works. You will also learn what changed from Tailwind v3 and how to diagnose the usual setup failures.
Prerequisites
This chapter assumes you can do the following:
- Run npm commands and read a
package.json. - Write React function components in TypeScript or JavaScript.
- Read a basic CSS file.
- Know roughly what a bundler does.
You do not need to know any Tailwind classes yet. Later chapters cover them.
What changes in Tailwind v4
Older tutorials describe a v3 setup with several moving parts: postcss, autoprefixer, a tailwind.config.js file, a content array, and three @tailwind directives. Tailwind v4 removes most of that.
| Concern | Tailwind v3 | Tailwind v4 with Vite |
|---|---|---|
| Packages | `tailwindcss`, `postcss`, `autoprefixer` | `tailwindcss`, `@tailwindcss/vite` |
| Build integration | PostCSS config file | Vite plugin in `vite.config.ts` |
| Loading the CSS | `@tailwind base; @tailwind components; @tailwind utilities;` | `@import "tailwindcss";` |
| Finding source files | `content` array in the config | Automatic detection |
| Customization | `tailwind.config.js` | `@theme` blocks in CSS |
If you copy a snippet from an older article and it uses @tailwind directives or a content array, it targets v3. Mixing the two setups is the most common reason a new project shows no styles.
How the pieces fit together
Three files take part in the setup. The Vite config registers the plugin. The stylesheet tells Tailwind where to insert its CSS. The React entry file imports that stylesheet so Vite includes it in the page.
The plugin does the work in the middle. When Vite processes index.css, the plugin sees the @import "tailwindcss" line. It then scans your project's source files for class names and replaces that line with CSS for the classes it found. How the scan decides what counts as a class name gets its own chapter. For now, treat it as automatic.
Step 1: Create the project
Create a React and TypeScript project with Vite, then install its dependencies:
npm create vite@latest my-app -- --template react-ts
cd my-app
npm installThe -- passes the template option through to Vite. If you prefer JavaScript, use --template react instead. Everything below works the same, apart from the .tsx and .ts extensions.
Step 2: Install Tailwind and its Vite plugin
npm install tailwindcss @tailwindcss/viteTwo packages are involved. tailwindcss contains the framework itself. @tailwindcss/vite is the official plugin that connects it to Vite. You do not need postcss or autoprefixer. Tailwind v4 handles vendor prefixes and modern CSS features itself.
Step 3: 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 plugins array is a list of Vite plugins. Both entries are function calls, so keep the parentheses. Passing tailwindcss without calling it is a mistake that Vite reports with an error. The order of the two plugins does not matter for this setup.
Step 4: Load Tailwind from your stylesheet
Replace the contents of src/index.css with a single line:
@import "tailwindcss";The Vite template ships with many default rules in index.css and App.css. Those rules style the demo page, such as a centered layout and a dark background. They can fight with Tailwind's utilities, so remove them. Keep only the import for now, and add your own global CSS below it later if you need any.
@import "tailwindcss" is a CSS import resolved by the build, not a network request. It brings in three things in order:
- A theme layer that defines the default design tokens, such as colors and spacing, as CSS variables.
- A base layer, often called preflight, that resets browser defaults.
- The utilities layer, which holds the generated classes.
The reset has visible effects. Headings lose their default size and weight, lists lose their bullets, and images become block-level elements. If your first test page looks "unstyled", the reset is working as intended, and you restore the look with utilities.
Step 5: Import the stylesheet in the entry file
The Vite React template already imports the stylesheet in src/main.tsx:
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import './index.css'
import App from './App.tsx'
createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
)Check that the ./index.css line is present. If you renamed the stylesheet or created your own, this import must point to the right file. A stylesheet that no module imports is never part of the build, so none of its CSS reaches the page.
Step 6: Verify with a component
Replace src/App.tsx with a component that uses a handful of utilities. This also removes the template's App.css import, which no longer exists in your styles.
export default function App() {
return (
<main className="flex min-h-screen items-center justify-center bg-gray-50">
<h1 className="text-3xl font-bold text-blue-600">Tailwind is working</h1>
</main>
)
}Start the dev server:
npm run devOpen the printed local address. You should see a large, bold, blue heading centered on a light gray page. Change text-blue-600 to text-red-600 and save. The color should change without a full page reload, because Vite hot-replaces the stylesheet.
Common setup errors
When styles do not appear, work through the list below from the top. The causes are ordered by how often they occur.
| Symptom | Likely cause | Fix |
|---|---|---|
| No utility has any effect, and headings look reset | The plugin is not registered, or the stylesheet is never imported | Check `plugins` in `vite.config.ts` and the `import './index.css'` line |
| Page looks styled by the Vite template, not by Tailwind | Old template rules in `index.css` or `App.css` override your utilities | Delete the template CSS and remove stray `App.css` imports |
| Console warning about unknown `@tailwind` rules, or no output | v3 directives in the stylesheet | Use `@import "tailwindcss";` instead |
| `tailwind.config.js` changes have no effect | v4 does not load that file automatically | Move tokens into `@theme` in CSS, which a later chapter covers |
| Error that the plugin is not a function | `tailwindcss` imported from the wrong package | Import the default export from `@tailwindcss/vite`, not from `tailwindcss` |
| A new class shows nothing until restart | The dev server missed a file or a cached module | Restart `npm run dev` and hard-reload the browser |
| Layout looks wrong in an old browser | v4 targets modern browsers | Check the browser support section of the Tailwind docs |
Where setup choices matter later
Two decisions made in this chapter will come back.
- One stylesheet is the entry point. All theme customization, such as new colors and fonts, goes into this same CSS file, below the import. Dark mode configuration lands there too.
- The plugin scans files for class names. This is why later chapters insist that class names appear as complete strings in your source. The setup is correct. The detection rules are what you must learn.
Editor support and class sorting build on this project but need extra packages, so they have their own chapter.
Exercise
A teammate's new Vite and React project loads without errors, but none of the Tailwind classes in App.tsx have any effect. Their vite.config.ts contains plugins: [react(), tailwindcss()] and src/index.css contains @import "tailwindcss";. Name the first two things you would check, and say how you would tell the stylesheet is missing from the build.
Show answer
First, check that src/main.tsx imports ./index.css. A stylesheet that no module imports is never bundled. Second, check that the tailwindcss function in the config was imported from @tailwindcss/vite and that the package is installed. You can confirm the stylesheet is missing by running npm run build and opening the CSS file in dist/assets. If it has no .text-blue-600-style rules for the classes in use, Tailwind did not process the file. You can also use the browser's developer tools to see whether any stylesheet with Tailwind's reset rules is loaded.