| sidebar_position | 3 |
|---|---|
| title | Installing Tailwind with Vite |
| sidebar_label | 3. Setup & Installation |
| description | A step-by-step guide to adding Tailwind CSS to your React project. |
Adding Tailwind to a Vite project is a standard 3-step process. If you already have your React app running from our previous Vite Setup guide, you are ready to go!
Open your terminal in your project's root folder and run these two commands. This installs Tailwind and its "helpers" (PostCSS and Autoprefixer) which make sure your CSS works on all browsers.
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -pTwo new files appeared in your folder:
tailwind.config.js: The "Brain" of your styles.postcss.config.js: The "Translator" that helps browsers understand Tailwind.
We need to tell Tailwind which files it should look at to find your CSS classes. Open tailwind.config.js and update the content section:
/** @type {import('tailwindcss').Config} */
export default {
content: [
"./index.html",
"./src/**/*.{js,ts,jsx,tsx}", // Scan all React files in the src folder
],
theme: {
extend: {},
},
plugins: [],
}Now, we need to inject Tailwind's "DNA" into your CSS.
- Open your
src/index.cssfile. - Delete everything inside it.
- Paste these three "Directives" at the very top:
@tailwind base;
@tailwind components;
@tailwind utilities;Let's make sure everything is working. Open your App.jsx and replace the code with this:
function App() {
return (
<div className="flex h-screen items-center justify-center bg-blue-100">
<h1 className="text-4xl font-bold text-blue-600 underline">
Tailwind is Working!
</h1>
</div>
)
}
export default App- Is the background light blue? (
bg-blue-100) - Is the text large and bold? (
text-4xl font-bold) - Is everything centered? (
flex justify-center)
If yes, congratulations! You just styled a page without writing a single line of traditional CSS.
function App() {
return (
<div className="flex h-screen items-center justify-center bg-blue-100">
<h1 className="text-4xl font-bold text-blue-600 underline">
Tailwind is Working!
</h1>
</div>
)
}- [x] I installed the 3 core packages via NPM.
- [x] I initialized the config files.
- [x] I pointed Tailwind to my
srcfolder. - [x] I added the
@tailwinddirectives to my CSS file.
:::tip Extension Recommendation If you use VS Code, search for the extension "Tailwind CSS IntelliSense". It will give you "Autocomplete" suggestions as you type, so you don't have to memorize every class name! :::