How to Use Tailwind CSS with React

Set up Tailwind CSS v4 in a Vite React app. Install the Vite plugin, import Tailwind in your CSS, and style a component with utility classes.

6 min read

Tailwind CSS v4 styles React components with small utility classes, and it plugs into Vite through a dedicated plugin instead of a PostCSS setup. This guide installs Tailwind in a Vite React app, replaces the default stylesheet, and styles a button to confirm the pipeline works.

Prerequisites

You need Node.js 20 or higher, which the official Tailwind upgrade tooling requires. A working Vite React app is the starting point, and the setup assumes the default structure where main.jsx imports src/index.css.

Install and configure

The setup has four ordered steps: install two packages, register a plugin, import Tailwind in one CSS file, and restart the dev server.

Install the packages

Install the engine and the Vite plugin together with npm.

Register the Vite plugin

Add the plugin to the plugins array in vite.config.js.

Import Tailwind in your CSS

Replace the contents of src/index.css with a single import.

Start the dev server

Restart the server and open the printed local URL.

Install the packages

Tailwind v4 splits the engine and the Vite plugin into two packages that you install together.

bashbash
npm install tailwindcss @tailwindcss/vite

Register the Vite plugin

Open vite.config.js and add the plugin to the plugins array, next to the existing React plugin.

index.jsindex.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
 
export default defineConfig({
  plugins: [react(), tailwindcss()],
});

The plugin gives Tailwind access to the files Vite transforms, so it can scan them for utility classes.

Import Tailwind in your CSS

Replace the contents of src/index.css with a single import. The existing import in main.jsx stays in place.

csscss
@import "tailwindcss";

This import pulls in Tailwind's base styles, utilities, and theme as CSS that Vite processes.

Start the dev server

Restart the server so the new plugin is picked up, then open the local URL it prints.

bashbash
npm run dev

After these steps the app reloads with Tailwind active, even though no component uses a utility yet. The browser may briefly show unstyled markup on the first reload, which is normal while the plugin compiles.

Style your first component

Utilities are plain class names on JSX elements. Replace the default content in App.jsx with a small card to see Tailwind working.

App.jsxApp.jsx
function App() {
  return (
    <div className="max-w-sm rounded-xl bg-white p-6 shadow-md">
      <h2 className="text-lg font-semibold text-gray-900">Tailwind is active</h2>
      <p className="mt-2 text-gray-600">
        This card is styled entirely with utility classes.
      </p>
      <button className="mt-4 rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700">
        Try it
      </button>
    </div>
  );
}

The browser renders a white card with rounded corners, a shadow, a heading, and an indigo button that darkens on hover. Each class sets one property, so the layout reads left to right like a spec.

How Tailwind decides what to ship

Tailwind scans your source files for class names and generates only the CSS those names need. If you never write a class, its rule never enters the output, which keeps the stylesheet small without manual cleanup.

That scanning is why the plugin must be registered and why the import belongs in one CSS file that Vite processes. Tailwind v4 reads the theme and utilities from that import at build time.

Troubleshooting

  • No styles appear. Confirm the plugin is in vite.config.js and that your CSS file contains the import, then restart the server.
  • Utilities render but look different. v4 renamed some v3 classes, such as the shadow scale, so check the upgrade guide if you are porting an older project.
  • Unsupported browser. v4 targets Safari 16.4, Chrome 111, and Firefox 128 or newer, so stay on v3.4 for older browser support.

Once utilities work, the natural next step is a dark mode toggle. For the choice between this and scoped CSS, see CSS Modules vs Tailwind.

Rune AI

Rune AI

Key Insights

  • Install tailwindcss and @tailwindcss/vite together.
  • Register the Tailwind plugin in vite.config.js.
  • Replace your CSS with @import "tailwindcss";
  • Style components with utility classes.
  • Tailwind ships only the classes your code uses.
RunePowered by Rune AI

Frequently Asked Questions

Does Tailwind v4 still need a tailwind.config.js file?

No. v4 is configured in CSS with the @theme directive, and a JavaScript config file is only loaded if you point to it with @config. The basic setup needs no config file.

Why are my utilities not showing up?

Check that the Vite plugin is in the plugins array and that your CSS file imports tailwindcss. Then confirm the component imports that CSS file and restart the dev server.

Can I use Tailwind with a plain React app without Vite?

Yes. Tailwind also ships a PostCSS plugin and a CLI, so it works with any build setup. The Vite plugin is the recommended path for Vite projects.

Conclusion

Tailwind CSS v4 fits into a Vite React app through a single plugin. Install the packages, register the plugin, import tailwindcss in your stylesheet, and style components with utility classes that Tailwind compiles on demand.