Sub topic 9
Why dynamic class names fail
This chapter assumes you can write arbitrary values such as w-[317px], and that you know variants like hover: and md:. It also assumes basic JavaScript, such as template strings and objects. You will learn how Tailwind finds class names in your code, why building names from pieces breaks that, and how to fix it.
How Tailwind finds your classes
Tailwind does not run your code. It reads your source files as plain text and looks for strings that could be class names. For each candidate that matches a utility, it writes the CSS rule. Anything it never sees as a complete string gets no CSS.
That has a direct consequence for arbitrary values. w-[317px] is only generated if the exact text w-[317px] appears somewhere in a scanned file.
The classic mistake
This code looks reasonable, but it fails:
// Not detected: Tailwind never sees the complete class name
const width = 240;
const broken = `w-[${width}px]`;In the file, Tailwind sees the text w-[${width}px]. That is not a valid class name, because ${width} is not a length. The finished string w-[240px] only exists while the program runs, and by then the CSS has already been built. The browser gets an element with a class that matches no rule, so nothing happens. No error appears.
The same failure happens with other ways of building names:
| Pattern | Why it fails |
|---|---|
| `` `bg-${color}-500` `` | The color is only filled in at runtime |
| `"p-" + size` | The two halves never appear together |
| `` `${prefix}:underline` `` | The variant is assembled from a variable |
| `["w-[", w, "px]"].join("")` | The parts are scattered across the text |
Fix 1: write complete class names
Keep every class name whole in the source and choose between them at runtime. A lookup object is the usual way:
// Detected: every class name appears in full in the source
const widths = {
narrow: "w-[240px]",
wide: "w-[480px]",
};
function panelClass(size) {
return widths[size];
}
console.log(panelClass("wide")); // w-[480px]Tailwind scans the object literal, finds both strings, and generates both rules. The runtime code only picks which one to use. The same applies to ternaries:
const isActive = true;
const tone = isActive ? "bg-[#bada55]" : "bg-[#cccccc]";Both branches are complete strings, so both are detected.
Fix 2: pass the changing value through a CSS variable
A lookup object only works when the set of values is fixed. If the value is truly open-ended, such as a width from a database or a user-picked color, you cannot list every option. Keep the class static and put the changing value in a CSS variable on the element's style attribute.
<div class="w-[var(--panel-width)] bg-[color:var(--panel-tone)]"
style="--panel-width: 317px; --panel-tone: #bada55">
Panel
</div>The classes w-[var(--panel-width)] and bg-[color:var(--panel-tone)] are complete and never change, so Tailwind generates them once. Only the variable values change at runtime. The color: hint, from the earlier chapter on type hints, tells Tailwind to treat the variable as a color.
In a template, you would set the style from data, for example style="--panel-width: ${width}px". The class string itself stays untouched.
Fix 3: tell Tailwind about classes it cannot see
Sometimes the class names live outside your scanned files, for example in a database or a content management system. You can then ask Tailwind to generate specific classes anyway.
- In Tailwind v4, use
@source inline()in your CSS, such as@source inline("w-[240px]");. - In Tailwind v3, add the classes to the
safelistarray intailwind.config.js.
Treat this as a last resort. The list has to be kept in sync with your data by hand, and it is easy to forget.
A quick debugging routine
- Open the element in the browser's developer tools and confirm the class is on the element.
- Check whether a rule for that class exists in the loaded stylesheet.
- If the class is present but the rule is missing, search your source for the full class name. If you cannot find it as one unbroken string, rewrite the code using Fix 1 or Fix 2.
- If the rule exists but has no effect, the problem is something else, such as specificity or a missing type hint.
Exercise
A component builds its classes with ` text-[${size}px] hover:bg-[${hex}] where size and hex` come from user settings and can be any number or color. Neither class gets any CSS. Rewrite the component's markup so both classes are detected and still follow the user's settings.
Show answer
Use static classes that read CSS variables, and set the variables from the settings. For example: <p class="text-[length:var(--text-size)] hover:bg-[color:var(--hover-tone)]" style="--text-size: 18px; --hover-tone: #bada55">. The length: and color: hints tell Tailwind which property each variable sets. The class names never change, so Tailwind generates them once. Only the variable values come from the settings at runtime.