Sub topic 11
Dynamic class names and how Tailwind detects classes
This chapter assumes you can write basic Tailwind markup, know what a utility class is, and have a project that builds Tailwind CSS with a bundler, the CLI, or a framework plugin. You should also know basic JavaScript string and object syntax.
How Tailwind finds classes
Tailwind does not run your code. It reads your source files as plain text and looks for strings that could be class names. Each candidate token is checked against the utilities it knows. If a token matches, Tailwind writes the CSS for it into the output file. Everything else is left out, which keeps the stylesheet small.
This means detection depends only on what is written in the file. Tailwind cannot see values that exist only at runtime.
The common mistake: building class names from pieces
A frequent error is assembling a class name from fragments. The code below looks reasonable, but Tailwind never sees a full class name.
// Does not work: Tailwind cannot see the full class name in the source.
const bad = (color) => `text-${color}-600`;
// Works: every complete class name appears literally in the source.
const colorClasses = {
red: 'text-red-600',
green: 'text-green-600',
};
const good = (color) => colorClasses[color];In the first function, the scanner finds text-, -600, and the template syntax. It never finds text-red-600, so no CSS is generated. In development you may see the style work if another file happens to contain the full name. In production the style disappears. That inconsistency makes the bug hard to trace.
The second version works because both full strings appear in the file. The scanner does not care that they sit inside an object. It only needs the complete token somewhere in the text.
Patterns that work
- Lookup objects. Map each value to a complete class string, as in
colorClassesabove. This also gives you one place to see every allowed option. - Full strings in conditionals. Write
isActive ? 'bg-indigo-600' : 'bg-slate-200'instead of building the name. - Complete variants. Write each variant in full, such as
hover:bg-indigo-700, rather than joining a prefix and a class. - Inline styles for truly runtime values. If a value comes from user input or a database, such as an arbitrary hex color, use a
styleattribute or a CSS variable. Tailwind cannot pre-generate classes for values it never sees.
Making sure files are scanned
Tailwind only scans files it knows about. In version 4, it detects sources automatically and skips files listed in .gitignore, binary files, and CSS files. If classes live somewhere unusual, such as a package in node_modules or a shared library, register the location in your CSS.
@import "tailwindcss";
@source "../node_modules/my-ui-library/src";Classes stored outside your source files, for example in a CMS that returns HTML at runtime, are never scanned either. For those cases, list them in your source, or use @source inline() to force specific classes into the output.
Debugging a missing style
- Open the browser's developer tools and check whether the class is on the element.
- Search the built CSS for the class. If it is absent, the scanner did not see it.
- Search your source for the complete class name. If you cannot find the whole string, it is being assembled dynamically.
- Check that the file containing the class is in a scanned location.
Exercise
A component uses ` bg-${status}-500 where status is "green" or "red"`. The style works in development but is missing in production. Why, and how would you fix it?
Show answer
Tailwind scans source text and never sees the complete strings bg-green-500 or bg-red-500. In development they may have been generated by chance from another file. Fix it with a lookup object such as { green: 'bg-green-500', red: 'bg-red-500' } and read from it using status, so both full class names appear literally in the source.
Exercise
Your design system lives in a package under node_modules, and its Tailwind classes do not appear in your build. What do you add to your CSS?
Show answer
Add an @source directive pointing to the package's source folder, for example @source "../node_modules/my-ui-library/src";, so Tailwind scans those files too.