Sub topic 12
When to use the PostCSS plugin instead
This chapter assumes you have already completed the Vite setup from the earlier chapters. You should be comfortable with vite.config.ts, with importing a CSS file, and with the idea that a build tool transforms CSS before the browser sees it. You do not need to know PostCSS internals.
Why this is a fallback, not the default
The @tailwindcss/vite plugin is the recommended way to use Tailwind CSS v4 in a Vite project. It plugs directly into Vite's module graph. Vite already knows which files changed, so the plugin can rebuild only what it needs. In Tailwind's own benchmarks, this is faster than running Tailwind through PostCSS.
Tailwind v4 also ships a PostCSS plugin, @tailwindcss/postcss. It produces the same CSS from the same @import "tailwindcss" line. The difference is where the work happens. PostCSS runs as a general CSS pipeline, so the plugin works in any tool that supports PostCSS, not just Vite.
Use the PostCSS plugin when the Vite plugin is not an option. Typical reasons:
- Your build tool is not Vite. Examples are webpack, Parcel, or a framework whose bundler you cannot replace.
- Your project already has a
postcss.configfile with other plugins that you need to keep in one pipeline. - A framework or toolchain configures PostCSS for you and does not let you add Vite plugins.
- You are using an older Vite version that the Vite plugin does not support. Check the plugin's current requirements on the Tailwind site.
If none of these apply, stay with @tailwindcss/vite.
Choosing between the two
| Situation | Better choice |
|---|---|
| New Vite project | `@tailwindcss/vite` |
| Vite project with no PostCSS config | `@tailwindcss/vite` |
| Non-Vite bundler such as webpack | `@tailwindcss/postcss` |
| Existing PostCSS pipeline you must keep | `@tailwindcss/postcss` |
| Command-line build without a bundler | The Tailwind CLI (`@tailwindcss/cli`) |
The last row is outside this tutorial, but it is worth knowing that PostCSS is not the only fallback.
Installing the PostCSS plugin
Install Tailwind and the PostCSS plugin as development dependencies. PostCSS itself must also be present. Vite includes it, but other tools may need it installed explicitly.
npm install tailwindcss @tailwindcss/postcss postcssConfiguring PostCSS
Create a postcss.config.mjs file in the project root. It registers the Tailwind plugin by name.
export default {
plugins: {
"@tailwindcss/postcss": {},
},
}The .mjs extension makes this an ES module, so export default works regardless of your package.json type setting. Vite, webpack, and most tools that read PostCSS config accept this file name.
The CSS side does not change. Your entry stylesheet still contains one line:
@import "tailwindcss";Then import that stylesheet from your app, exactly as you did in the Vite setup. Everything else you learned still applies: @theme for design tokens, @source for extra scan paths, and automatic source detection.
Using PostCSS inside a Vite project
Vite reads postcss.config.* automatically. If you have this file in a Vite project and you do not register @tailwindcss/vite, Tailwind runs through PostCSS. This is a valid setup. It is also an easy one to create by accident.
The two integrations should not both be active. If you register the Vite plugin and also leave @tailwindcss/postcss in postcss.config.mjs, Tailwind processes your CSS twice. Output may be wrong, and builds take longer. Pick one.
What you give up and what you keep
The generated CSS is the same in both setups. The trade-offs are about tooling.
- Build speed. The Vite plugin integrates with Vite's own file watching and module graph. The PostCSS route is generally slower, because PostCSS has less information about what changed.
- Configuration surface. The Vite plugin needs one line in
vite.config.ts. The PostCSS route needs a separate config file, which is one more place for settings to drift. - Portability. PostCSS works across many tools. If you may change bundlers later, the PostCSS config moves with you.
- Other PostCSS plugins. If you use plugins that must run in a fixed order, a single PostCSS pipeline keeps them together.
One related point: Tailwind v4 handles @import inlining and vendor prefixing itself. In v3 you listed postcss-import and autoprefixer next to Tailwind. In v4 you do not need them in the PostCSS config for Tailwind's sake. Leaving them in is usually harmless, but it is redundant.
Migrating from the PostCSS config to the Vite plugin
If you started with PostCSS and want to move to the Vite plugin, the change is small.
- Install the Vite plugin:
npm install @tailwindcss/vite. - Add
tailwindcss()from@tailwindcss/viteto thepluginsarray invite.config.ts. - Remove
"@tailwindcss/postcss"frompostcss.config.mjs. If the file has no other plugins, delete it. - Restart the dev server.
- Confirm that your utility classes still render.
Your CSS does not change.
Common errors with the PostCSS plugin
- Using the old package name in the config. In v3 the PostCSS plugin was
tailwindcssitself. In v4 it moved to@tailwindcss/postcss. Puttingtailwindcss: {}in the plugin list produces an error that tells you to install the new package. - Forgetting to install
postcss. Some bundlers do not bring it in. If the build says it cannot findpostcss, install it. - Config in the wrong place. The file must be in the project root, or in a directory your tool searches. A config in a subfolder is ignored.
- Styles missing after a config edit. Restart the dev server. Tools usually read PostCSS config once at startup.
- Double processing. See the warning above.
Exercise
Your team uses Vite, and the repository already contains a postcss.config.mjs that lists @tailwindcss/postcss. A teammate adds tailwindcss() from @tailwindcss/vite to vite.config.ts without touching the PostCSS file. What problem does this create, and what is the fix?
Show answer
Tailwind now runs through both integrations, so the CSS is processed twice. This can cause wrong output and slower builds. The fix is to keep only one. If you want the Vite plugin, remove "@tailwindcss/postcss" from postcss.config.mjs (or delete the file if it has no other plugins) and restart the dev server.
Exercise
You are adding Tailwind v4 to a project that builds with webpack. Which package do you install, what file do you create, and does your CSS entry file change?
Show answer
Install tailwindcss, @tailwindcss/postcss, and postcss. Create postcss.config.mjs with "@tailwindcss/postcss": {} under plugins. The CSS entry file does not change. It still contains @import "tailwindcss";.