In Tailwind CSS, why does a badge class built as `bg-${status}-600` from an API value render with no background, and how do you fix it?
answer
- how the build finds class names
- text scan, not code execution
- what the scanner literally sees
- lookup object of full strings
- accidental match from another file
basics
~20 sTailwind scans source files as plain text and generates CSS only for complete class names it finds. The fragment bg-${status}-600 never appears whole, so no rule is generated; map each status to a full class string such as bg-emerald-600.
solid answer
~50 sTailwind never runs your code. Its scanner reads each source file as plain text, collects tokens that look like class names, and generates CSS only for those that match a real utility. In `` `bg-${status}-600` `` it sees only fragments such as `bg-`, never `bg-emerald-600`, so that rule is not in the stylesheet and the badge gets no background. The fix is to make every class exist in full in the source: a lookup object such as `{ confirmed: "bg-emerald-600", cancelled: "bg-rose-600" }` keyed by the API value, with a neutral fallback. If the interpolated version seems to work, the full class is usually written in some other file, and it breaks when that file changes. A genuinely arbitrary value, such as a hex color from the API, belongs in an inline style or a CSS custom property instead.
code
jsx · 13 lines// Never generated: the scanner only sees fragments
<span className={`bg-${status}-600`}>{label}</span>
// Generated: each class exists in full in the source
const statusClasses = {
confirmed: "bg-emerald-600 text-white",
waiting: "bg-amber-600 text-white",
cancelled: "bg-rose-600 text-white",
};
<span className={statusClasses[status] ?? "bg-gray-600 text-white"}>
{label}
</span>go deeper
Recall that Tailwind generates only class names it finds written out in full in your files, and that the fix for a dynamic class is a map of complete class strings.
Explain the scanner: it reads files as plain text, extracts class-like tokens and discards those that are not utilities, which is why an object of full strings works and interpolation does not.
Show the diagnosis: no rule in DevTools means it was never generated, a class that works only because another file mentions it is a latent bug, and new API values need a fallback.
Weigh where data-driven styling should live: a closed map of classes beside the component for finite sets, inline styles or custom properties for open values, and a review rule that keeps the map honest.
## How Tailwind decides which CSS to generate Tailwind CSS does not ship a stylesheet containing every possible utility. At build time a **scanner** reads your project's files, and the compiler generates CSS only for the class names that scanner found. Two properties of the scanner explain this whole question: - It treats every file as **plain text**. It does not run, evaluate or type-check your code, so it has no idea what a template literal, a prop or a variable holds. - It collects every **token** that could be a class name and then throws away the tokens that do not map to a utility the framework knows. So, apart from utilities you deliberately force into the build, a utility ends up in the output only when its **complete name** appears somewhere in a scanned file and matches a real utility. Nothing that happens later in the browser makes Tailwind add a rule to a stylesheet that was built before the page loaded. ## Why `bg-${status}-600` produces nothing Picture the appointment dashboard of a veterinary clinic. Each appointment carries a status from the API (`confirmed`, `waiting`, `cancelled`), and a badge component builds its background class like this: `` className={`bg-${status}-600`} `` with a status-to-color step somewhere upstream. The scanner reads that line as characters. It sees fragments such as `bg-` and `-600`, neither of which is a utility, and it never sees `bg-emerald-600` or `bg-rose-600`, because those strings exist only at run time, after the API has answered. The CSS was finished long before. In DevTools the element shows the class in its `class` attribute, but no rule matches it, so the badge has no background. The same failure appears in every form of assembly: - interpolating a variant, such as `hover:bg-${tone}-500`; - concatenating with `+`, as in `"bg-" + tone + "-600"`; - server templates, such as `text-{{ error ? 'red' : 'green' }}-600`; - building only the shade, as in `bg-emerald-${weight}`. ## The fix: every class written out in full 1. List the statuses the API can return. 2. Write a lookup object that maps each status to a **complete** class string. 3. Read from the map, with a neutral fallback for a status nobody planned for. ```jsx const statusClasses = { confirmed: "bg-emerald-600 text-white", waiting: "bg-amber-600 text-white", cancelled: "bg-rose-600 text-white", }; <span className={statusClasses[status] ?? "bg-gray-600 text-white"}>{label}</span> ``` The map is not only a workaround. It lets each status choose its own shade and text color, and it keeps the vocabulary of badge styles in one reviewable place. | Pattern in source | Generated? | Why | |---|---|---| | `className="bg-emerald-600"` | Yes | the complete token is in the file | | `` `bg-${status}-600` `` | No | only fragments are in the file | | `{ confirmed: "bg-emerald-600" }[status]` | Yes | the complete token sits in an object literal | | `"bg-" + status + "-600"` | No | concatenation leaves the same fragments | | `error ? "text-rose-600" : "text-emerald-600"` | Yes | both branches are complete tokens | ## Why the dynamic version sometimes seems to work The scanner covers the whole project, not one component. If `bg-emerald-600` is written out in full in some other file (a legend, a story, a test fixture), the rule exists, and the interpolated class picks it up **by accident**. Confirmed badges look fine, cancelled ones do not, and the day someone edits that other file the confirmed badges lose their color too, with no error and no warning. This is also not a development-versus-production difference. Tailwind generates utilities on demand in development as well, so a class that is never written in full is missing in both. "It worked yesterday" usually means another file mentioned the class yesterday. ## When the value really is open-ended Some values cannot be listed. If the clinic's admins can pick any hex color for a custom appointment type, no finite set of classes covers it. That value belongs in an inline `style` attribute or in a CSS custom property set on the element; a class can still reference the property, but it cannot carry the value itself. A different case is a finite set of classes whose names never appear in your source at all, for example classes typed by content editors into a CMS. Tailwind has a separate mechanism to force those utilities to be generated. It is a different problem from this one: here the code is yours, so the right move is to write the classes out in full where the badge is rendered.
- Does a class name inside a JavaScript object or a JSON fixture count, or only one inside a class attribute?Any complete token in any scanned file counts. Tailwind reads files as plain text, so it does not care whether the class sits in a `class` attribute, an object literal, a JSON file or a comment. That is why a lookup object works, and also why a stray class name in a comment can generate a rule nobody uses. Files the scanner skips by default are a separate matter.
- The API may add a new status next month. How do you stop that badge from silently rendering unstyled?Treat the map as the contract. Give it a fallback entry for unknown statuses, such as `bg-gray-600`, and in a typed codebase type its keys from the API's status union so a new value fails the type check. Someone still has to add the new class to the map by hand: the scanner cannot learn about a value it never sees in the source.
- How do you tell whether the class was never generated or was generated but lost to another rule?Inspect the element. If DevTools lists no rule at all for `bg-rose-600`, it was never generated; searching the built stylesheet for the class name confirms it. If the rule is there but struck through, generation worked and the problem is which rule wins in the cascade, which is a different investigation.
The scanner is a word-search solver that can only circle words printed whole on the page. A word you plan to assemble later from two halves is never circled, however obvious it looks to you.
- Tailwind evaluates template literals at build time, so interpolated classes are generated.
- The class is only missing in production; development generates every utility.
- Switching from a template literal to + concatenation makes the class detectable.
- It rendered correctly yesterday, so the interpolated class must be detected.
- Restarting the dev server lets the scanner pick up values the API returns.