# Tailwind CSS Dynamic Class Generation ## Rule Never construct Tailwind CSS class names dynamically using string concatenation or template literals. Tailwind's build process cannot detect dynamically generated class names, causing styles to be missing in production. ## Why This Matters - Tailwind uses static analysis at build time to determine which CSS classes to include - Dynamically constructed class names (e.g., `bg-${color}-500`) are invisible to Tailwind's scanner - Classes work in development with JIT but fail silently in production builds - This is a common source of "it works locally but not in production" bugs ## Bad Code ```vue ``` ## Good Code ```vue ``` ## Using Conditional Objects ```vue ``` ## Safelist for Truly Dynamic Classes If you must use dynamic classes, add them to Tailwind's safelist: ```javascript // tailwind.config.js module.exports = { safelist: [ 'bg-red-500', 'bg-blue-500', 'bg-green-500', // Or use patterns (use sparingly - increases bundle size) { pattern: /bg-(red|blue|green)-(100|500|900)/ } ] } ``` ## Alternative: CSS Custom Properties For truly dynamic values, use CSS custom properties: ```vue ``` ## References - [Tailwind CSS Dynamic Class Names](https://tailwindcss.com/docs/content-configuration#dynamic-class-names) - [Tailwind Safelist](https://tailwindcss.com/docs/content-configuration#safelisting-classes)