Sub topic 8
How Tailwind finds the classes you use
This chapter assumes you have a working Tailwind v4 setup in a Vite and React project, as in the setup chapter. It also assumes you can read a short CSS file and a JSX component. You don't need to know how Tailwind works internally. By the end you will know how it decides which CSS to generate, why some classes silently do nothing, and how to fix the cases where detection fails.
Tailwind generates CSS on demand
Tailwind does not ship a stylesheet with every class in it. Its full set of utilities, with every variant and every theme value, would be far too large to send to a browser. Instead, Tailwind reads your source files, collects the class names it finds, and writes CSS only for those.
When you write p-6 in a component, Tailwind sees p-6 and generates this rule:
.p-6 {
padding: calc(var(--spacing) * 6);
}If no file contains p-6, the rule does not exist in the output. This is why a production Tailwind stylesheet is usually small, and why a class you "used" can have no effect when Tailwind never saw it.
The detection pipeline
Detection has four steps:
- Tailwind builds a list of files to scan.
- It reads each file as plain text and splits it into tokens.
- It checks each token against its known utilities, variants and theme values.
- It generates CSS for the tokens that match and ignores the rest.
Step 2 matters most. Tailwind does not parse your TSX. It does not know what a className attribute is, and it does not evaluate your JavaScript. It treats each file as a stream of characters and picks out anything that looks like it could be a class name. A token is only a candidate at that stage. It becomes CSS only if it matches something Tailwind knows.
Three consequences follow from this.
- A class name works in any file type, because Tailwind does not care about the language. It works in
.tsx,.html,.md, or a.jsonfile with strings in it. - A class name in a comment still generates CSS, because comments are text too.
- A word in prose that happens to be a utility name also generates CSS. If a Markdown file contains the word "flex", Tailwind emits
.flex. This is harmless, but it can surprise you when you inspect the output.
Unknown tokens are dropped without a warning. A typo such as bg-bleu-500 produces no error and no CSS. The element just looks unstyled.
Which files are scanned
Tailwind v4 detects sources automatically. You don't list the files to scan. It scans the project and skips files that are unlikely to contain classes:
- Anything listed in your
.gitignore. - The
node_modulesdirectory. - Binary files such as images, videos and fonts.
- CSS files.
- Lock files and package manager files.
The Vite plugin also takes part in the dev server's module graph. When you save a component, Tailwind rescans the changed file and updates the CSS through hot module replacement. You don't restart anything.
The .gitignore rule is the one that causes trouble. If a directory is ignored by Git, Tailwind ignores it too. That is usually what you want for dist/, but not when you generate source files into an ignored folder.
Adding and removing sources
The @source directive adds paths to the scan. Put it in the same CSS file as @import "tailwindcss". Paths are relative to the CSS file that contains the directive.
@import "tailwindcss";
/* A component library installed from npm contains Tailwind classes */
@source "../node_modules/@acme/ui";
/* Skip a folder of old code that should not contribute CSS */
@source not "../src/legacy";Use the first form when a package ships React components styled with Tailwind classes. Because node_modules is skipped by default, those classes are invisible until you add the package path. The symptom is a third-party component that renders with no styling in your app. The second form, @source not, is available in Tailwind v4.1 and later.
You can also change the base of the scan on the import itself:
/* Scan only ./src, not the whole project */
@import "tailwindcss" source("../src");
/* Turn off automatic detection and list every source yourself */
@import "tailwindcss" source(none);
@source "../src";
@source "../node_modules/@acme/ui";Narrowing the scan with source("../src") can speed up builds in large repositories. It also means files outside that folder, such as an index.html in the project root, stop contributing classes. If you use source(none), you take over the whole list, so check that nothing is missing.
Safelisting classes that never appear in source
Sometimes a class is real but never appears as text in a scanned file. Examples are classes that come from a database, a CMS, or a user setting. Tailwind cannot find them, so you tell it to generate them anyway. In v4.1 and later, @source inline() does this:
@import "tailwindcss";
/* Always generate these classes, with and without the hover: variant */
@source inline("{hover:,}bg-red-500");
@source inline("underline");The braces expand into several names, so the first line produces bg-red-500 and hover:bg-red-500. Use this sparingly. Each safelisted class is CSS you ship whether or not it is used. If the set of classes is known at build time, writing them out in your source is simpler and keeps the code searchable.
The most common failure: classes built from pieces
Because Tailwind matches complete tokens, a class name assembled at runtime is never seen. The source text contains text- and -600, but never text-red-600.
type Props = { color: 'red' | 'green' }
export function StatusBad({ color }: Props) {
// Tailwind sees the text "text-${color}-600", which is not a class name.
return <p className={`text-${color}-600`}>Status</p>
}
export function StatusGood({ color }: Props) {
// Both complete class names appear in the source, so both are generated.
const colorClasses = {
red: 'text-red-600',
green: 'text-green-600',
} as const
return <p className={colorClasses[color]}>Status</p>
}StatusBad renders a <p> with the class text-red-600 in the DOM. The class is present on the element, but no CSS rule backs it, so the text stays the default color. The next chapter covers patterns for conditional and dynamic class names in depth. For now, remember the rule behind them: every class you want must appear in full somewhere in a scanned file.
Checking what was generated
When a class does nothing, work through these checks in order.
- Look at the element in browser dev tools. If the class is on the element but no rule is applied, Tailwind did not generate it. If the class is missing from the element, your React logic is the problem, not Tailwind.
- Search the project for the full class name. If it only exists as fragments, it was never detected.
- Check the file's location. Is it in a Git-ignored folder, or inside
node_modules? If so, add an@sourceline or move the file. - Check the spelling and the variant order. An unknown token is dropped silently.
- Inspect the built CSS. You can confirm what shipped:
npm run build
grep -o "text-red-600" dist/assets/*.cssIf grep prints nothing, the class is not in the stylesheet. Vite's output file names include a hash, so the glob dist/assets/*.css is needed instead of one fixed name.
Edge cases to keep in mind
| Situation | What happens | Fix |
|---|---|---|
| Class built with a template string | Not detected | Map values to full class strings |
| Class lives in an npm package | Skipped, because `node_modules` is ignored | Add `@source "../node_modules/pkg"` |
| Source file is in a Git-ignored folder | Not scanned | Add `@source` for it, or stop ignoring it |
| Class stored in a database or CMS | Never appears in source | Use `@source inline()` |
| Class only mentioned in a comment | CSS is generated | Remove it if you want a smaller stylesheet |
| Typo in a class name | No CSS, no error | Use editor autocomplete and lint tooling |
Summary
Tailwind reads your files as text, collects tokens, and writes CSS for the tokens that match a known utility. Detection is automatic for most of the project and can be extended with @source. Anything assembled at runtime, stored outside your source, or placed in an ignored folder needs extra help.
Exercise
A teammate writes <div className={bg-${tone}-100 p-4}> where tone is 'blue' or 'amber'. The p-4 padding works but the background never appears. Explain why, and show a fix.
Show answer
Tailwind scans source files as plain text and only generates CSS for complete class names. The text bg-${tone}-100 never contains bg-blue-100 or bg-amber-100, so those rules are not generated. p-4 appears in full, so it works. The fix is to map each value to a full class string: const toneClasses = { blue: 'bg-blue-100', amber: 'bg-amber-100' } as const and then use className={${toneClasses[tone]} p-4}. Both complete names now appear in the source.
Exercise
You install a package @acme/ui that ships React components styled with Tailwind classes. In your app they render with no styling. What is the likely cause, and what line fixes it?
Show answer
Tailwind skips node_modules during automatic source detection, so it never saw the classes used inside the package. Add @source "../node_modules/@acme/ui"; to the main CSS file, next to @import "tailwindcss";. The path is relative to the CSS file that contains the directive.