Vite

Integrating Tailwind CSS v4 into a Vite project

Move a Vite project from Tailwind v3 PostCSS to the v4 Vite plugin, define theme tokens in CSS, and compare build output and timing.

Integrating Tailwind CSS v4 into a Vite project

Moving Tailwind CSS from the v3 PostCSS pipeline to the v4 Vite plugin changed both my CSS entry point and the work done during a build. The following migration keeps theme definitions in CSS and checks the difference in the generated output.

The comparison uses the Tailwind v3 PostCSS setup and Tailwind v4 with @tailwindcss/vite.

What changes when the Vite plugin owns the build

A typical v3 setup runs Tailwind as a PostCSS plugin. It needs explicit content patterns, such as ./src/**/*.{html,js,ts,jsx,tsx}, and may also run Autoprefixer and CSSNano. Missing a source directory can leave a class with no corresponding style. The scanning and optimization steps all add work to the build.

Tailwind v4 introduces the Rust-based Oxide scanner, a dedicated Vite plugin, a Lightning CSS pass for transforms and optimization, and CSS-first configuration. The Vite plugin wires these pieces into the build directly. A project using other PostCSS plugins can still need a PostCSS configuration for their own work.

flowchart TD
    subgraph V3["Tailwind CSS v3 (JavaScript-based setup)"]
        A1["Source files (.tsx / .vue)"] --> B1["PostCSS runner (Node.js)"]
        B1 --> C1["Match source patterns and candidates"]
        C1 --> D1["JavaScript JIT generation"]
        D1 --> E1["Run Autoprefixer and CSSNano"]
        E1 --> F1["Emit bundled CSS"]
    end
    subgraph V4["Tailwind CSS v4 (Oxide & Lightning CSS)"]
        A2["Source files (.tsx / .vue)"] --> B2["@tailwindcss/vite plugin"]
        B2 --> C2["Discover class candidates with Oxide"]
        C2 --> D2["Generate utility rules"]
        D2 --> E2["Transform and optimize with Lightning CSS"]
        E2 --> F2["Emit bundled CSS"]
    end

I keep source detection and CSS optimization separate when diagnosing missing styles. Tailwind scans source files for class candidates. Lightning CSS can process the generated CSS for transforms and optimization. A missing candidate cannot be recovered by the later CSS pass.

Install the Vite integration

Update Tailwind and add its Vite plugin.

1
2
# Install Tailwind CSS v4 and its Vite plugin
pnpm add tailwindcss@^4.0.0 @tailwindcss/vite@^4.0.0

Add the plugin to vite.config.ts alongside the framework plugin. When switching from a v3 PostCSS configuration, remove the old Tailwind plugin so there is a single Tailwind processing path.

1
2
3
4
5
6
7
8
9
10
// vite.config.ts
import { defineConfig } from 'vite';
import tailwindcss from '@tailwindcss/vite';

export default defineConfig({
  plugins: [
    // Register the Tailwind CSS v4 integration
    tailwindcss()
  ]
});

If PostCSS was only there for Tailwind and its supporting passes, the unused config and dependencies can be removed after checking the build. Keep the ones still used by the rest of the application.

Define the CSS entry and the theme

In the application’s stylesheet, import Tailwind with @import "tailwindcss" and define the project colors and fonts with @theme. A custom utility can live in the same entry through @utility.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* src/styles/app.css */
@import "tailwindcss";

/* Theme tokens declared in the v4 CSS entry */
@theme {
  --color-brand-primary: #89b4fa;
  --color-brand-surface: #1e1e2e;
  --color-brand-accent: #fab387;

  --font-sans: 'Pretendard', system-ui, -apple-system, sans-serif;
  --font-mono: 'Menlo', monospace;
}

/* Example custom utility */
@utility glass-panel {
  background-color: rgba(30, 30, 46, 0.8);
  backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

Import the stylesheet from the application entry if the framework does not already include it. Theme variables such as --color-brand-primary can make related utility names available across components.

Automatic source detection replaces many manual content globs. It scans source text for complete class candidates. For shared packages that are outside its default search, configure an explicit source with @source. Avoid constructing a utility name out of fragments if Tailwind cannot see the full name in the source. Tailwind source detection

Check the migrated styles in a component

The example uses the custom theme colors and a glass panel utility in a React component. Rendering it after migration gives a quick check that the theme tokens and ordinary utility classes both made it into the stylesheet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// src/components/DashboardCard.tsx
import React from 'react';

export function DashboardCard() {
  return (
    <div className="glass-panel rounded-2xl p-6 shadow-xl transition hover:scale-[1.02]">
      <h3 className="font-sans text-xl font-bold text-brand-primary">
        System resource monitoring
      </h3>
      <p className="mt-2 text-sm text-gray-300">
        View current resource usage in one place.
      </p>
      <button className="mt-4 rounded-lg bg-brand-accent px-4 py-2 font-semibold text-gray-950 transition hover:brightness-110">
        View details
      </button>
    </div>
  );
}

Before removing the v3 theme config, identify any plugin, theme extension, or custom utility the new entry still needs. A successful build only confirms the files were processed. Inspect representative component states and test the supported browsers before deploying.

Compare build time and generated CSS

The sample benchmark used hyperfine for 10 runs on a project with 24 modules and about 140 utility classes. The v3 PostCSS setup averaged 862.4 ms. The v4 Vite setup averaged 161.8 ms, a 5.33-fold difference in this test. Generated CSS dropped from 22.4 KB to 13.9 KB, about 37.9 percent. Compare equivalent builds and output content when trying to reproduce these numbers.

Hyperfine results for Tailwind v3 PostCSS and Tailwind v4 Vite builds Figure 1. Ten production builds compare the two Tailwind configurations on the sample project.

I used pnpm vite build --debug vite:css to inspect the CSS stage. The recorded scanner step took 4.8 ms and found 146 utility candidates. The output flattens nested CSS, and the example logs show the theme’s custom properties changing from 1,420 candidates to 48 retained definitions. Those numbers belong to the measured project. Tailwind generates the style and theme definitions it needs, and the optimization pass prepares the resulting CSS.

Vite CSS debug output showing the Tailwind scanner and Lightning CSS transformation Figure 2. The debug log exposes the detection and CSS transformation stages in the sample build.

For migration, I check source discovery first, then theme and utility presence, browser support, and output size. Measuring the exact configurations gives a useful baseline when deciding which parts of an older CSS pipeline can be removed.

Tailwind’s Vite installation guide

This post is licensed under CC BY 4.0 by the author.