Sub topic 1
What changes in Tailwind v4 and why use the Vite plugin
Tailwind CSS v4 changes how the framework is installed, configured, and run. This chapter explains those changes and why the first-party Vite plugin is the recommended way to use Tailwind in a Vite project. The later chapters walk through each setup step. Here you build the mental model that makes those steps make sense.
Prerequisites
This chapter assumes you have:
- Basic HTML and CSS knowledge, including what a class selector does.
- Node.js and npm installed, and comfort running commands in a terminal.
- A basic idea of what Vite does. Vite is a dev server that serves your source files quickly during development. It is also a bundler that produces optimized files for production.
- Some exposure to Tailwind's utility-class approach, such as
flexortext-center. Experience with v3 helps but is not required.
The short version of what changed
Tailwind v3 was a PostCSS plugin driven by a JavaScript configuration file. A typical v3 project had three moving parts: tailwind.config.js, postcss.config.js, and a CSS file with three @tailwind directives. You also usually installed postcss and autoprefixer.
Tailwind v4 reduces that to a package, a bundler plugin, and one line of CSS. The main changes are:
- A new engine. The core was rewritten, with performance-critical parts in Rust. The Tailwind team reports full builds up to about 5 times faster and incremental builds (rebuilds after a small edit) over 100 times faster than v3.
- CSS-first configuration. Design tokens such as colors, fonts, and spacing are declared in CSS with the
@themedirective, instead of in a JavaScript object. - One import.
@import "tailwindcss";replaces@tailwind base; @tailwind components; @tailwind utilities;. - Automatic source detection. Tailwind finds the files that contain class names without a
contentarray. - A first-party Vite plugin.
@tailwindcss/viteplugs the engine directly into Vite's pipeline. - Modern CSS features. v4 relies on newer browser features such as
@propertyandcolor-mix(), which raises the minimum browser versions.
A before-and-after view
In v3, the stylesheet entry looked like this:
@tailwind base;
@tailwind components;
@tailwind utilities;In v4, the same entry is:
@import "tailwindcss";Tokens that lived in tailwind.config.js in v3:
// tailwind.config.js (v3 style)
export default {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {
colors: { brand: "#3b82f6" },
},
},
}now live in your CSS file in v4:
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
}Both versions produce utilities such as bg-brand and text-brand. The v4 version has no content array because source detection is automatic. You will build this setup step by step in later chapters.
Why a Vite plugin
Tailwind v4 ships two integration paths: @tailwindcss/vite and @tailwindcss/postcss. For a Vite project, the Vite plugin is the better default. The reasons are practical.
Fewer layers
With PostCSS, Tailwind runs as one step inside a chain that Vite invokes for each CSS file. The Vite plugin skips that chain. It hooks into Vite directly, so there is no postcss.config file to maintain and no PostCSS plugin ordering to think about.
Better use of Vite's module graph
Vite already knows which modules your app imports and which files changed. The plugin can use that information to decide when to rebuild the generated CSS. The Tailwind team reports that this gives the best performance in Vite projects. Your edit-to-browser loop stays fast because only the affected output is recomputed.
No companion tools
In v3, you installed autoprefixer to add vendor prefixes and often postcss-import to resolve @import statements. v4 handles both itself, using Lightning CSS internally for prefixing, nesting, and minification. For a typical project you do not add those packages.
Less configuration
For a typical project, the setup is one package pair, one plugin entry, and one CSS import:
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [
tailwindcss(),
],
})The plugin takes no required options. You will install and register it step by step in the next chapters.
How the pieces fit together
The diagram shows the path from your source files to the CSS that the browser receives. The plugin sits inside Vite and does the scanning and generation work.
Two points are worth noting. First, the plugin generates only the utilities your files actually use, so the stylesheet stays small. Second, the CSS file is the single place where Tailwind is "turned on" and configured. That is what "CSS-first" means in practice.
What "CSS-first" means for your workflow
In v3 a token change meant editing a JavaScript file. The tool then rebuilt, and you hoped the editor picked up the change. In v4, @theme variables are real CSS custom properties. Tailwind exposes them on :root, so you can also use them in your own CSS and in inline styles:
.banner {
background-color: var(--color-brand);
}This also removes the split where some values were reachable from CSS and others only from JavaScript. You still can use a JavaScript config for legacy projects through the @config directive, but a new project does not need one.
Source detection in one paragraph
Instead of a content list, v4 scans your project automatically. It respects .gitignore, skips binary files and CSS files, and ignores node_modules by default. Because it ignores node_modules, class names used inside a dependency, such as a shared component library, are not seen unless you register that directory with @source. A later chapter covers this in detail. Keep it in mind as the most common reason a class "does nothing" in a monorepo or a UI-library setup.
Differences you should know up front
| Area | Tailwind v3 | Tailwind v4 |
|---|---|---|
| CSS entry | Three `@tailwind` directives | `@import "tailwindcss";` |
| Configuration | `tailwind.config.js` | `@theme` in CSS (JS config optional via `@config`) |
| Content paths | `content` array | Automatic detection, plus `@source` |
| Vite integration | PostCSS plugin | `@tailwindcss/vite` (PostCSS plugin still available) |
| Prefixing and imports | `autoprefixer`, `postcss-import` | Built in |
| Browser baseline | Older browsers | Safari 16.4+, Chrome 111+, Firefox 128+ |
Trade-offs and limits
The Vite plugin is not free of constraints, and you should check these before adopting v4.
- Browser floor. v4 targets Safari 16.4, Chrome 111, and Firefox 128 or newer. If you must support older browsers, stay on v3.4. Tailwind's compatibility notes say this directly, and v4 does not provide a polyfill.
- Preprocessors. v4 is designed as the CSS processor itself. Using Sass, Less, or Stylus with it is not supported. Write plain CSS, which now supports nesting and variables.
- Vite only for the plugin. If your build tool is webpack or another bundler without Vite's plugin API, use the PostCSS plugin or the Tailwind CLI instead. This is covered under the PostCSS alternative later in the tutorial.
- Renamed utilities. Some utilities changed names or defaults between v3 and v4. A migrated project may render slightly differently until you review them. A fresh project is not affected.
- Version alignment. The plugin version should match your
tailwindcssversion. Mismatched versions are a common cause of strange errors. Check the plugin's supported Vite versions in its package metadata.
What you will build in this tutorial
The next chapters follow the order of the concept graph. You start from a Vite project, install tailwindcss and @tailwindcss/vite, register the plugin, import Tailwind in CSS, load the stylesheet, and verify utilities in the dev server. After that you cover browser support, @theme, @source, the production build, the PostCSS alternative, differences from v3, and troubleshooting.
Exercise
A teammate's v4 Vite project uses a button component from a shared UI library inside node_modules. Utility classes written in your own src files work, but classes that appear only in the library's files produce no styles. Which v4 feature explains this, and what is the fix?
Show answer
Automatic source detection ignores nodemodules by default, so Tailwind never scans the library for class names. The fix is to add the library's directory to your CSS with the @source directive, for example @source "../nodemodules/@my-company/ui-lib"; placed after @import "tailwindcss";.
Exercise
Name three things a typical v3 project needed that a typical v4 project with the Vite plugin does not.
Show answer
Any three of these: a tailwind.config.js file with a content array, a postcss.config file, the autoprefixer package, the postcss-import package, and the three @tailwind directives (replaced by a single @import "tailwindcss";).
Exercise
Your product must support Safari 15. Should you adopt Tailwind v4? Give the reason.
Show answer
No. v4 requires Safari 16.4 or newer, Chrome 111 or newer, and Firefox 128 or newer because it relies on modern CSS features. For Safari 15 you should stay on Tailwind v3.4.