Sub topic 10
How Tailwind finds your classes, and @source
This chapter assumes you have a working Vite project with @import "tailwindcss"; in your CSS and the @tailwindcss/vite plugin registered. You should also know how the dev server loads that stylesheet.
Tailwind generates only what it finds
Tailwind does not ship every utility. It scans your project for strings that look like class names and writes CSS only for those. If text-3xl never appears in a scanned file, the stylesheet has no .text-3xl rule. This keeps the production CSS small.
Automatic source detection
In v3 you listed files in the content array of tailwind.config.js. In v4 you list nothing. Tailwind starts from the project root and scans every file except these:
- Files ignored by
.gitignore, which normally coversnode_modulesanddist. - Binary files such as images, videos, and archives.
- CSS files.
- Lock files such as
package-lock.json.
Tailwind does not parse your code. It reads each file as plain text and extracts any token that could be a class name. That works in HTML, JavaScript, TypeScript, JSX, Vue, Svelte, and Markdown alike.
Class names must appear in full
Because the scan is plain text, Tailwind only sees complete strings. This code fails:
const color = 'blue'
const html = `<p class="text-${color}-600">Hello</p>`The text text-blue-600 never appears in the file, so Tailwind never generates it. Map each variant to a full class name instead:
const colors = {
blue: 'text-blue-600',
red: 'text-red-600',
}
const html = `<p class="${colors.blue}">Hello</p>`Both complete strings are in the source, so both rules are generated.
Adding sources with @source
Automatic detection misses files that .gitignore excludes. The usual case is a UI library in node_modules that uses Tailwind classes. Register it with @source in your CSS:
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";The path is relative to the CSS file that contains the directive, not to the project root. Here the stylesheet lives in src/, so ../ reaches the root. Tailwind scans the listed directory in addition to its automatic sources.
Newer v4 releases (4.1 and later) add two related forms:
@source not "../legacy";excludes a path from scanning.@source inline("underline");always generates the listed classes, which helps when class names come from a database or CMS and never appear in your files.
If you use these, check that your installed version supports them.
Quick reference
| Situation | What to do |
|---|---|
| Classes in your own source files | Nothing. Detected automatically. |
| Classes in a package under `node_modules` | Add `@source` with the package path. |
| Classes built from string pieces | Write full class names in the source. |
| Classes that never appear in files | Use `@source inline(...)`, if your version supports it. |
Exercise
Your component has ` bg-${tone}-500 where tone is "red" or "green"`. The background never appears. Why, and what are two ways to fix it?
Show answer
Tailwind scans files as plain text and never evaluates the template string, so bg-red-500 and bg-green-500 never appear in full and no CSS is generated. Fix it by writing the full class names in the source, for example a lookup object such as { red: 'bg-red-500', green: 'bg-green-500' }. Alternatively, force generation with @source inline("bg-red-500 bg-green-500"); in your CSS, if your version supports it.
Exercise
Your stylesheet is src/style.css, and a package at node_modules/@acme/ui uses Tailwind classes. Write the @source line that makes Tailwind scan it.
Show answer
@source "../node_modules/@acme/ui";. The path is relative to src/style.css, so ../ goes up to the project root.