Sub topic 8
How Tailwind finds the classes you use
This chapter assumes you can build React components in TSX and have a Vite project with Tailwind v4 installed, as set up earlier in this tutorial. You should also know basic CSS.
Tailwind does not know your classes in advance
Tailwind does not ship a giant stylesheet with every possible class. It reads your source files, collects the class names it finds, and generates CSS for only those. This keeps the output small. It also explains the most common Tailwind bug: a class that you wrote but that has no styles.
In v4, the Vite plugin does this work. The plugin is registered in vite.config.ts:
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [react(), tailwindcss()],
})Your main stylesheet then pulls Tailwind in with one line:
@import "tailwindcss";How detection works
Tailwind treats your files as plain text. It does not run your JavaScript and does not parse JSX. It splits the text into tokens and checks each token against the utilities it knows. If a token matches, such as p-6 or hover:bg-blue-700, Tailwind writes the CSS rule for it.
Three consequences follow from this:
- A class name inside a string, a comment or a Markdown file still counts, if the file is scanned.
- A class name that never appears as a complete token is never generated.
- Tailwind ignores files listed in
.gitignore, and it ignores binary files and CSS files. If your classes live in a package outside your project, you need to tell Tailwind to scan it.
The most common mistake: building class names from pieces
Because Tailwind reads text, it cannot see a class name that you assemble at runtime. This example fails:
// Does not work: Tailwind cannot see the complete class name in the source.
const bad = <p className={`text-${color}-600`}>Status</p>
// Works: every complete class name appears in the source as a string.
const colorClasses = {
red: 'text-red-600',
green: 'text-green-600',
} as const
const good = <p className={colorClasses[color]}>Status</p>In the first line, the source text contains text-${color}-600. That is not a real class, so Tailwind generates nothing. At runtime the browser receives text-red-600, but no CSS rule for it exists. In the second version, text-red-600 and text-green-600 both appear in full, so Tailwind finds them.
Diagnosing a missing style
When a class has no effect, check these in order:
- Open the browser's dev tools and look for the rule. If the class is on the element but no rule exists, Tailwind did not generate it.
- Search your source for the complete class name. If you cannot find it as one unbroken string, detection failed.
- Check for typos, such as
bg-blue600. An unknown token is silently ignored. - Check that the file is scanned. Files in
.gitignoreand files outside your project root are skipped. - Confirm that your entry file imports the stylesheet containing
@import "tailwindcss";.
Telling Tailwind about extra sources
If a class lives somewhere Tailwind does not scan, register that location in your CSS with the @source directive:
@import "tailwindcss";
@source "../node_modules/my-ui-library";If a class must exist even though it never appears in your source, such as a value that comes from a database, use @source inline():
@import "tailwindcss";
@source inline("text-red-600 text-green-600");Prefer the lookup-table approach from earlier whenever you can. It keeps every class visible in your code, which also helps people searching the repository.
Exercise
A component uses className={bg-${tone}-100} where tone is "red" or "green". The background never appears. Why does it fail, and how do you fix it?
Show answer
Tailwind scans source files as plain text and never evaluates the template string, so the complete classes bg-red-100 and bg-green-100 never appear in the source and no CSS is generated for them. Fix it with a lookup object such as { red: 'bg-red-100', green: 'bg-green-100' } and use toneClasses[tone], so each full class name appears literally in the source.