Sub topic 10
How Tailwind finds your classes, and @source
This chapter explains how Tailwind CSS v4 decides which class names to turn into CSS, and how to extend that with the @source directive. 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 your main CSS file. You should be comfortable reading HTML, JavaScript, and CSS.
Why Tailwind scans your files
Tailwind does not ship a fixed stylesheet with every utility in it. The full set of possible utilities is enormous, because values like w-[37px], bg-brand, and md:hover:text-blue-600 can be combined in many ways. Instead, Tailwind reads your source files, collects strings that look like class names, and generates CSS only for the ones it finds.
This has two consequences:
- The CSS output stays small, because unused utilities are never generated.
- A class that Tailwind cannot see in your files does not exist in the output, and the element will look unstyled.
The second point causes most "Tailwind is installed but nothing works" reports once the basic setup is correct.
How automatic source detection works
In v3 you listed paths in the content array of tailwind.config.js. In v4 there is no content array. The Vite plugin detects sources automatically, and you do not need to configure anything for a typical project.
Detection follows these rules:
- Tailwind starts from the project root, which is the current working directory when the build starts.
- It scans every file under that root, whatever the file type. HTML, JavaScript, TypeScript, JSX, TSX, Vue, Svelte, and Markdown files all count.
- It skips files listed in your
.gitignore, sonode_modules,dist, and other ignored folders are not scanned. - It skips binary files such as images, video, and archives.
- It skips CSS files, and it skips lock files such as
package-lock.json.
Tailwind does not parse your code. It treats each file as plain text and extracts anything that looks like a candidate class name. It does not run JavaScript or understand template syntax. That is why it works with any framework, and also why some patterns fail, as the next section shows.
Write class names as complete strings
Because Tailwind only reads text, a class name must appear in full somewhere in a scanned file. Building a class name from pieces hides it from the scanner.
This example does not work:
const color = 'red'
const html = `<p class="text-${color}-600">Error</p>`The scanner sees text- and -600 but never sees text-red-600, so no CSS is generated for it. The fix is to write the complete class name and choose between full names:
const colorClasses = {
red: 'text-red-600',
green: 'text-green-600',
}
const color = 'red'
const html = `<p class="${colorClasses[color]}">Error</p>`Both text-red-600 and text-green-600 now appear as whole strings, so both are generated. The same rule applies to any helper that assembles class names from fragments.
Where detection can miss files
Automatic detection is usually enough, but these situations are common exceptions:
| Situation | Why it is missed | Fix |
|---|---|---|
| A component library in `node_modules` | `node_modules` is in `.gitignore`, so it is skipped | Add it with `@source` |
| A sibling package in a monorepo | The package sits outside the project root | Add its path with `@source` |
| A file listed in `.gitignore` that you still want scanned | Ignored files are skipped | Add it with `@source` |
| Classes stored in a database or CMS | They never appear in a file at build time | Use `@source inline()` |
| A build run from a different working directory | The project root is not what you expect | Run from the project root, or add `@source` |
Add sources with @source
The @source directive adds a path that Tailwind should scan in addition to what it detects automatically. Place it in your CSS file after the import:
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";Details to keep in mind:
- The path is resolved relative to the CSS file that contains the directive, not relative to the project root. In this example, the CSS file is in
src/, so../node_modules/...points to thenode_modulesfolder one level up. - You can point to a directory or to a single file.
- You can use
@sourcemore than once, one directive per path. - A path you add with
@sourceis scanned even if.gitignorewould normally exclude it. That is why it works fornode_modules.
A project with a component library and a sibling package might look like this:
@import "tailwindcss";
@source "../node_modules/@my-company/ui-lib";
@source "../../shared-components/src";Narrow detection with @import source()
Sometimes the automatic scan is too broad. Scanning from the project root can pick up large folders of generated files or documentation that contain example class names. You can change the base path of the automatic scan by passing source() to the import:
@import "tailwindcss" source("../src");With this form, Tailwind scans only ../src relative to the CSS file, instead of the whole project root. Directories you add with @source are still scanned in addition.
Exclude paths with @source not
If most of a folder should be scanned but part of it should not, use @source not:
@import "tailwindcss";
@source not "../src/legacy";Tailwind then ignores ../src/legacy even if automatic detection would have included it. This is useful for folders with old class names, which would otherwise add unused CSS.
Safelist classes with @source inline()
Some classes never appear in a file, for example when a CMS returns class names at runtime. The v3 safelist option is gone in v4. Use @source inline() instead:
@import "tailwindcss";
@source inline("underline");
@source inline("{hover:,focus:,}bg-red-{50,{100..900..100},950}");The first line always generates underline. The second uses brace expansion to generate bg-red-50, bg-red-100 through bg-red-900 in steps of 100, and bg-red-950, each with no variant, hover:, and focus: prefixes. Use this sparingly, because every class you list is added to the CSS whether or not it is used.
Check what was generated
To confirm whether a class was detected, run the dev server and inspect the element in the browser's developer tools. If the class appears in the Elements panel but no rule applies, Tailwind did not generate it. Then work through this checklist:
- Is the class written as a complete string in a scanned file?
- Is the file inside the project root and not excluded by
.gitignore? - Is the file type text, not binary?
- If the file is in a dependency or another package, did you add it with
@source? - Did you restart the dev server after changing the Vite config? Changes to CSS directives normally reload on their own, but changes to
vite.config.tsneed a restart.
Common mistakes
- Writing
@sourcepaths relative to the project root. They are relative to the CSS file. A wrong path fails silently, so no error appears and the classes are missing. - Using
contentfrom v3. Atailwind.config.jswith acontentarray is not read automatically in v4. Move those paths into@sourcedirectives. - Dynamic class fragments. Strings like
bg-${name}-500cannot be detected. Use full class names. - Placing
@sourcebefore the import. Put@import "tailwindcss";first, then your directives, so the order is easy to follow and consistent across files. - Expecting CSS files to be scanned. Class names written only inside other
.cssfiles are not picked up. Use@applywith real utilities in your CSS, or put the class names in a scanned file.
Exercise
Your app imports a button component from node_modules/@acme/ui. The buttons render with no styling, although the same classes work in your own components. What is the most likely cause, and what line in your CSS file fixes it, assuming the CSS file is src/style.css?
Show answer
node_modules is listed in .gitignore, so automatic source detection skips it, and the classes used inside the library are never generated. Add @source "../node_modules/@acme/ui"; after @import "tailwindcss"; in src/style.css. The path is relative to the CSS file, so ../ goes from src/ up to the project root.
Exercise
This code produces no styling on the badge. Why, and how do you fix it?
const size = 'lg'
const html = <span class="text-${size}">New</span>
Show answer
Tailwind reads files as plain text and never sees the full class text-lg, only the fragment text-. Write complete class names instead, for example a lookup object such as { sm: 'text-sm', lg: 'text-lg' } and use sizes[size]. If the value truly comes from outside your code, add @source inline("text-lg"); to the CSS.