Sub topic 11
Build for production
This chapter covers what vite build does with Tailwind CSS v4 and how to check the result. It assumes you have a working Vite project with tailwindcss and @tailwindcss/vite installed, the plugin registered in vite.config.ts, and @import "tailwindcss"; in a stylesheet that your app loads. You should also know the basic Vite commands.
What changes between dev and build
In development, the dev server generates CSS on demand. When you save a file, Tailwind rescans it and updates the stylesheet in the browser. In production there is no watcher. You run one build and get a fixed set of files.
The @tailwindcss/vite plugin takes part in both modes. During a build it does these things:
- It scans your project for class names, using the same automatic source detection as in development.
- It generates only the utilities it found, plus the base styles and the theme variables.
- It minifies the CSS, because the plugin includes Lightning CSS for this.
- Vite then fingerprints the file name and writes it to the output directory.
You don't add a purge or content step. In v3 you listed paths in content and Tailwind removed unused styles. In v4 the generator only produces the classes it finds, so unused utilities never reach the output.
Run the build
Check your scripts in package.json. A project created with npm create vite@latest already has them:
{
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
}
}If your project uses TypeScript, the template's build script is usually tsc -b && vite build. The type check runs first, and a type error stops the build before Tailwind runs.
Build the project:
npm run buildThe output looks similar to this. The file names, hashes, and sizes differ in your project.
vite v7.0.0 building for production...
✓ 5 modules transformed.
dist/index.html 0.45 kB │ gzip: 0.29 kB
dist/assets/index-DiwrgTda.css 5.12 kB │ gzip: 1.61 kB
dist/assets/index-BcR2fQmL.js 1.38 kB │ gzip: 0.76 kB
✓ built in 312msThe CSS file is small because it only holds the base layer (Tailwind's preflight reset), your theme variables, and the utilities that your files use. A typical small app ends up in the single-digit kilobytes before compression.
Preview the built output
The dev server and the production build can differ. The dev server serves modules directly, and the build bundles them. Always look at the built files before you deploy:
npm run previewvite preview serves the dist directory on a local port, usually http://localhost:4173. Open it and check that the heading from the earlier chapters is still large, bold, underlined, and blue.
Preview is a check, not a production server. Do not use it to host your site.
Inspect the generated CSS
You can confirm that Tailwind generated what you expect by searching the output file. This example looks for a class from the verification step:
grep -o "text-blue-600" dist/assets/*.cssIf the class name is present, Tailwind found it in your source. Because v4 stores colors as theme variables, you can also look for the variable definition:
grep -o "\-\-color-blue-600:[^;]*" dist/assets/*.cssTailwind v4 only emits the theme variables that your utilities use, so a variable like --color-red-500 is missing when no file uses a red utility. This is by design. It keeps the file small.
If you defined --color-brand in @theme and never use bg-brand or text-brand anywhere, it may be absent from the output too. If you want a theme variable to always be emitted, use the static option:
@import "tailwindcss";
@theme static {
--color-brand: #3b82f6;
}With static, Tailwind writes the variable to the output even when no utility references it. Use this when other code reads the variable directly, for example var(--color-brand) in a stylesheet that Tailwind does not scan.
Dynamic class names are the main production failure
Most "works in dev, broken in production" reports come from class names that Tailwind cannot see as complete strings. Tailwind does not run your code. It reads your files as plain text and looks for tokens that look like class names.
This code breaks:
const color = 'red'
const html = `<p class="text-${color}-500">Warning</p>`The text text-${color}-500 never appears as a whole token in the file, so Tailwind does not generate text-red-500. You may see the style in dev if the same class was used elsewhere earlier in your session, which makes this bug hard to track down.
Use complete class names and select between them:
const colorClasses = {
red: 'text-red-500',
blue: 'text-blue-500',
}
const color = 'red'
const html = `<p class="${colorClasses[color]}">Warning</p>`Each full class name appears in the source, so Tailwind finds both.
If the class names come from data you cannot edit, such as a CMS, you can force them with @source inline():
@import "tailwindcss";
@source inline("text-{red,blue,green}-500");This tells Tailwind to generate text-red-500, text-blue-500, and text-green-500 as if they appeared in your source. Brace expansion keeps the list short. Use it sparingly, because every entry adds CSS to the output.
Check browser support for the built CSS
The build output uses modern CSS features. Tailwind v4 targets Safari 16.4+, Chrome 111+, and Firefox 128+. Features such as @property and color-mix() are central to how v4 works. If you must support older browsers, stay on Tailwind v3.4. A different build.target in Vite does not change this, because Tailwind relies on browser features, not on JavaScript syntax that Vite can lower.
Set the base path for deployment
If you deploy to a sub-path, such as https://example.com/docs/, set Vite's base option so that the CSS link in index.html points to the right place:
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
base: '/docs/',
plugins: [
tailwindcss(),
],
})This is a Vite concern, not a Tailwind one. A stylesheet that loads from the wrong path looks like a Tailwind failure, but the 404 in the browser's network tab tells you what is wrong.
Common production problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Styles work in dev but a class is missing after build | The class name is built dynamically | Write complete class names, or use `@source inline()` |
| A component library is unstyled in production | Its files are in `node_modules`, which is not scanned | Add `@source "../node_modules/the-library"` |
| The page has no styles at all | The CSS file is not imported, or `base` is wrong | Check the `import './style.css'` line and the network tab |
| A theme variable is missing from the CSS | No utility uses it | Use `@theme static` |
| Build fails before Tailwind runs | A TypeScript error in `tsc -b` | Fix the type error, then rebuild |
| Old styles appear after a deploy | The CDN cached `index.html` | Cache `index.html` briefly, and cache hashed assets for a long time |
A pre-deploy checklist
- Run
npm run buildand confirm that it exits without errors. - Check that exactly one CSS file appears in
dist/assetsand that its size is reasonable. - Run
npm run previewand look at every page that uses distinctive styles. - Search the CSS file for one or two classes that you generate from data.
- Open the browser's network tab and confirm that the CSS file returns status 200.
Exercise
Your app builds a class name with the template bg-${tone}-100, where tone is either "green" or "amber". The styles show up in the dev server but are missing in the production preview. Why can this happen, and how do you fix it in two different ways?
Show answer
Tailwind scans files as plain text and never runs your code, so bg-green-100 and bg-amber-100 never appear as complete strings. They may appear to work in dev only if another file already uses them. Fix 1: replace the template with an object that maps each tone to a full class name, such as { green: 'bg-green-100', amber: 'bg-amber-100' }. Fix 2: add @source inline("bg-{green,amber}-100"); to your CSS so Tailwind always generates both classes.
Exercise
After npm run build, you search dist/assets/*.css for --color-brand, which you defined in @theme, and find nothing. No file uses bg-brand or text-brand. What happened, and what change makes the variable appear?
Show answer
Tailwind v4 emits only the theme variables that the generated utilities use, so an unused variable is left out. Change @theme to @theme static so that Tailwind always writes the variable to the output.