Sub topic 1
What changes in Tailwind v4 and why use the Vite plugin
Prerequisites
This chapter assumes you have the following:
- Basic HTML and CSS.
- Node.js and npm installed.
- Comfort running commands in a terminal.
- A basic idea of what Vite does as a dev server and bundler.
You do not need any earlier Tailwind experience. If you have used v3, the comparisons below will help you map old habits to the new setup.
What Tailwind CSS v4 is
Tailwind CSS is a utility-first CSS framework. You style elements by adding small classes such as text-3xl or font-bold directly in your markup. Tailwind reads those class names and generates only the CSS your project uses.
Version 4 changes how you set Tailwind up and configure it. For a typical project, the setup is shorter than in v3:
- No
tailwind.config.jsis required. Customization moves into your CSS file. - No PostCSS setup is required when you use Vite. A first-party Vite plugin does the work.
- One CSS import replaces the three
@tailwinddirectives from v3. - Class detection is automatic. Tailwind scans your project for class names, so you no longer list files in a
contentarray.
Why use the Vite plugin
Vite already processes CSS and serves your files during development. The @tailwindcss/vite plugin hooks into that pipeline. It runs Tailwind when Vite loads your stylesheet, so there is no separate PostCSS config file to maintain.
Tailwind's own documentation recommends this plugin for Vite projects. The plugin integrates directly with Vite's module graph, which gives it accurate information about which files your app uses. That is how it can detect source files without a content list.
The PostCSS plugin, @tailwindcss/postcss, still exists. It is meant for projects that cannot use the Vite plugin. A later chapter covers it.
The v4 setup at a glance
The whole setup has four steps. Later chapters walk through each one.
npm create vite@latest my-app
cd my-app
npm install tailwindcss @tailwindcss/viteYou then register the plugin in vite.config.ts:
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})Finally, you add one line to your main CSS file:
@import "tailwindcss";In v3, the same file needed three directives: @tailwind base, @tailwind components, and @tailwind utilities. In v4, the single import replaces all of them.
How the pieces fit together
The diagram shows what happens during development. Vite loads your CSS file and passes it to the Tailwind plugin. The plugin scans your source files, generates the CSS for the classes it finds, and returns the result to the browser.
What you give up
The v4 defaults trade some flexibility for simplicity. Keep these points in mind:
- Browser support is narrower. v4 targets modern browsers and relies on newer CSS features. A later chapter lists the exact requirements, so check them before you commit if you must support older browsers.
- Old v3 setups do not carry over unchanged. Copying a v3 tutorial into a v4 project is a common source of errors.
- The Vite plugin only helps in Vite. Other build tools need a different integration.
Check your understanding
Exercise
A teammate's v3 project has a tailwind.config.js with a content array, a postcss.config.js, and three @tailwind lines in its CSS. Which of these can a new v4 Vite project leave out, and what replaces the three @tailwind lines?
Show answer
A typical v4 Vite project can leave out tailwind.config.js (no content array is needed because Tailwind detects sources automatically) and the PostCSS config (the @tailwindcss/vite plugin handles processing). The three @tailwind lines are replaced by a single @import "tailwindcss"; in the CSS file.
Exercise
Why does the Vite plugin not need a content list to know which files to scan?
Show answer
The plugin integrates with Vite's module graph and uses automatic source detection, so it finds the files your project uses without a manual list. You can add extra locations with @source when needed.