Sub topic 13
Differences from v3 setup
This chapter assumes you have finished the v4 install in the earlier chapters: a Vite project with tailwindcss and @tailwindcss/vite installed, the plugin registered, and @import "tailwindcss"; in your CSS. It also assumes you have seen a v3 project, or will meet one in an older tutorial or codebase. The goal is to recognize each v3 step, know what replaced it, and avoid mixing the two.
The setup at a glance
A v3 project used three tools: Tailwind, PostCSS, and Autoprefixer. It also needed two config files. v4 setup needs one package pair, one plugin line, and one CSS line.
| Concern | v3 | v4 with Vite |
|---|---|---|
| Packages | `tailwindcss`, `postcss`, `autoprefixer` | `tailwindcss`, `@tailwindcss/vite` |
| Generated files | `tailwind.config.js` and `postcss.config.js` from `npx tailwindcss init -p` | None required |
| Build hook | PostCSS plugins listed in `postcss.config.js` | `tailwindcss()` in `vite.config.ts` |
| CSS entry | Three `@tailwind` directives | `@import "tailwindcss";` |
| Finding classes | `content` array in the config file | Automatic detection, plus `@source` |
| Customization | `theme.extend` in JavaScript | `@theme` in CSS |
| Vendor prefixes | Autoprefixer | Handled internally |
Packages and config files
In v3 you installed Tailwind next to PostCSS and Autoprefixer, then generated both config files.
npm install -D tailwindcss@3 postcss autoprefixer
npx tailwindcss init -pIn v4 the Vite plugin does the work that PostCSS and Autoprefixer did. Tailwind v4 bundles its own CSS processing, including @import inlining and vendor prefixing, so you do not install postcss-import or autoprefixer.
npm install tailwindcss @tailwindcss/viteThe two files from v3 look like this:
// tailwind.config.js (v3)
/** @type {import('tailwindcss').Config} */
export default {
content: ['./index.html', './src/**/*.{js,ts,jsx,tsx}'],
theme: {
extend: {
colors: {
brand: '#3b82f6',
},
},
},
plugins: [],
}// postcss.config.js (v3)
export default {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}In v4 neither file is needed for a typical Vite project. The only file you edit besides the CSS is vite.config.ts, where you add tailwindcss() to the plugins array.
The CSS entry file
In v3 the entry file held three directives, one for each layer Tailwind generated.
@tailwind base;
@tailwind components;
@tailwind utilities;In v4 these directives are gone. A single import replaces all three.
@import "tailwindcss";This import is a normal CSS @import of the Tailwind package, which Tailwind resolves itself. It pulls in the base styles (Preflight), the theme variables, and the utilities in the correct cascade layers. If you were splitting the three directives across files to control order, import the pieces explicitly instead: tailwindcss/theme.css, tailwindcss/preflight.css, and tailwindcss/utilities.css.
Finding your classes: `content` versus detection
In v3, a missing or wrong content array was the most common reason for "my classes do nothing". Tailwind generated CSS only for classes it found in the listed files.
In v4 you do not list files. Tailwind scans your project automatically and applies these rules:
- It starts from the project root and skips files matched by
.gitignore. - It skips
node_modules, binary files such as images and videos, and CSS files. - It treats everything else as text and looks for strings that could be class names.
When a source lives outside that scope, such as a component library in node_modules, you add it with @source in the CSS.
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";Because detection reads plain text, it does not understand your code. A class assembled at runtime, such as ` text-${color}-500 `, never appears as a complete string, so it is never generated. This limitation is the same as in v3. Write complete class names in your source.
Customization: JavaScript to CSS
The v3 theme.extend.colors.brand entry shown above becomes a CSS variable in a @theme block.
@import "tailwindcss";
@theme {
--color-brand: #3b82f6;
--font-display: "Georgia", serif;
}The variable name decides which utilities exist. A --color-* variable creates bg-brand, text-brand, border-brand, and so on. A --font-* variable creates font-display. Every theme variable is also emitted as a real CSS custom property, so you can use var(--color-brand) in your own CSS.
If you have a large v3 config you cannot migrate yet, v4 can still load it with the @config directive.
@import "tailwindcss";
@config "../tailwind.config.js";Treat this as a bridge during migration. Some v3 config options, such as corePlugins, safelist, and separator, are not supported in v4.
Renamed and changed utilities
The setup is only half of an upgrade. Several utilities changed meaning, so a v3 template can render differently under v4 even when installation succeeds.
| v3 class | v4 class |
|---|---|
| `shadow-sm` | `shadow-xs` |
| `shadow` | `shadow-sm` |
| `rounded-sm` | `rounded-xs` |
| `rounded` | `rounded-sm` |
| `blur-sm` | `blur-xs` |
| `outline-none` | `outline-hidden` |
| `ring` (3px) | `ring-3` |
| `bg-opacity-50` with `bg-black` | `bg-black/50` |
| `bg-[--brand]` | `bg-(--brand)` |
Some changes have no new class name. The default border color is now currentColor, where v3 used gray-200. A bare border therefore looks different unless you add a color such as border-gray-200. The default ring width is now 1px, not 3px. The standalone opacity utilities (bg-opacity-*, text-opacity-*) are removed in favor of the slash syntax.
Stacked variants also changed order. In v3 they applied right to left, so first:*:pt-0 meant something different from what it reads like. In v4 they apply left to right, in the order you read them.
Browser and Node requirements
v4 targets modern browsers: Safari 16.4 and later, Chrome 111 and later, and Firefox 128 and later. It relies on newer CSS features such as @property and color-mix(). If you must support older browsers, stay on v3. Tailwind's official upgrade tool also needs a current Node.js release, so check your version with node --version.
Using the upgrade tool
For an existing v3 project, Tailwind provides an automated migration.
npx @tailwindcss/upgradeThe tool updates dependencies, migrates the config to CSS, and rewrites renamed utilities in your templates. Run it on a new Git branch and read the diff, because it cannot cover every case. Utilities built from dynamic strings, for example, are not rewritten.
How the pieces connect
Checking your project for v3 leftovers
Search for these signs when you open an older project or copy a snippet from an old tutorial:
@tailwind base;or similar in a CSS file. Replace all three with@import "tailwindcss";.autoprefixerorpostcss-importinpackage.json. You can remove both when using the Vite plugin.tailwindcss: {}inside apostcss.config.js. This entry causes the plugin error described above.- A
contentarray that you assumed was still required. In v4 it does nothing unless you load the file with@config. - A
tailwind.config.jsthat you edit but whose changes never show up. v4 does not load it unless you add@config.
Exercise
A teammate upgrades a Vite project to Tailwind v4 and keeps @tailwind base; @tailwind components; @tailwind utilities; in style.css. They have also added tailwindcss() to vite.config.ts and deleted postcss.config.js. Their theme.extend.colors.brand entry in tailwind.config.js is not producing bg-brand. What do they need to change?
Show answer
Replace the three @tailwind directives with @import "tailwindcss";, because v4 no longer supports them. Then either move the color into a @theme block as --color-brand: #3b82f6;, or load the old file with @config "../tailwind.config.js";. v4 does not read tailwind.config.js automatically, which is why bg-brand is missing. The @theme route is preferred, since @config is a migration bridge.
Exercise
After upgrading, a card that used shadow-sm and rounded looks sharper and flatter than before, and a plain border is now black instead of light gray. Explain both changes.
Show answer
v4 renamed the scale. The v3 shadow-sm and rounded classes correspond to shadow-xs and rounded-xs in v3's sizes, so v3 shadow-sm should now be written shadow-xs, and v3 rounded should become rounded-sm, to keep the same look. The old names now map to larger values on the new scale. Separately, v4's default border color is currentColor, not gray-200, so a bare border takes the text color. Add an explicit color such as border-gray-200.