Why Dynamically Constructed Class Names Can Fail
Tailwind's scanner works with source text rather than executing application logic to discover every possible CSS class. Consider a component that constructs a background utility from separate values representing a color and intensity. The resulting class may exist when the application runs, but the complete class name may never appear in the source files during compilation. Consequently, Tailwind might not generate the necessary CSS. A safer approach is to map supported application states to complete, explicitly written utility strings. For instance, an alert component can maintain distinct predefined styling combinations for success, warning, and error conditions. Those complete class names remain visible to the source scanner.
This technique also creates a reviewable contract: developers can verify which visual states the application actually supports. The same principle applies to dynamic sizing, tenant branding, and status-dependent appearance. When values are genuinely continuous or determined at runtime, CSS custom properties may be a better mechanism than constructing utility class names.


