You can’t generate classes dynamically in Tailwind

By

Published · Updated

Tagged: CSS

Why Tailwind cannot detect dynamically constructed class names, how to map values to complete classes, and how to safelist utilities in Tailwind CSS v4.

I wanted to have a dynamic color in Tailwind, using a syntax like this in JSX:

bg-${color}-500

But it wasn’t applied to the page because Tailwind couldn’t find for example the text bg-red-500 in the code, so the code was not added to the final CSS.

Why it happens

Tailwind scans source files as plain text. It can generate bg-red-500 when that complete token exists in the source, but it cannot evaluate a template literal and infer every possible result.

It doesn’t run your code, and it doesn’t parse JSX, Vue or PHP. It looks for strings that could be class names, and generates CSS only for those.

So all of these fail, because the full class name never appears in the file:

<div className={`bg-${color}-500`}>
<div className={`text-${error ? 'red' : 'green'}-600`}>
<div className={'p-' + size}>

This one works, because both complete names are in the source:

<div className={error ? 'text-red-600' : 'text-green-600'}>

Why it sometimes works

This bug is confusing because it can look random. If bg-red-500 appears anywhere else in your project, in another component or even in a comment, Tailwind generates it, and your dynamic class works for red. Then you pass blue and nothing happens.

So when a dynamic class “works”, don’t trust it. It only works by accident.

Map values to complete class names

Map each allowed value to a complete class name:

const colorClasses = {
  green: 'bg-green-500 text-white',
  blue: 'bg-blue-500 text-white',
  red: 'bg-red-500 text-white'
}

Then use the mapped class:

<h1 className={`mt-10 ${colorClasses[color] ?? ''}`}>
  Hello
</h1>

The same works for a component that takes a prop. Each variant can use its own combination of classes, including hover: ones:

const variants = {
  primary: 'bg-blue-600 hover:bg-blue-500 text-white',
  danger: 'bg-red-600 hover:bg-red-500 text-white',
  warning: 'bg-yellow-300 hover:bg-yellow-400 text-black'
}

function Button({ variant = 'primary', children }) {
  return (
    <button className={`rounded px-4 py-2 ${variants[variant]}`}>
      {children}
    </button>
  )
}

When the value is really dynamic

Sometimes you can’t list the values in advance. For example a user picks a brand color, or the color comes from a database.

Don’t build a Tailwind class for it. Put the value in a CSS variable with the style attribute, and use the variable in a Tailwind class:

<div
  className="bg-(--brand) p-4"
  style={{ '--brand': user.brandColor }}
>
  Hello
</div>

bg-(--brand) is Tailwind v4 shorthand for bg-[var(--brand)], and it generates background-color: var(--brand). The class name is static, so Tailwind finds it, and only the variable changes at runtime.

Some utilities can’t tell a color from a size. With text-, add a type hint: text-(color:--fg) for the color, text-(length:--size) for the font size.

Safelist classes with @source inline()

If a class cannot appear in your templates, Tailwind CSS v4 can explicitly include it with @source inline() in your CSS:

@import "tailwindcss";
@source inline("grid-cols-{1..3}");

That generates grid-cols-1, grid-cols-2 and grid-cols-3. The string is brace expanded, so you can also add variants and ranges with a step:

@source inline("{hover:,}bg-red-{50,{100..900..100},950}");

This generates every shade of bg-red from 50 to 950, each with a hover: version too.

This is useful when class names come from content Tailwind never sees, like Markdown stored in a CMS. Keep the list small: everything you safelist ends up in your CSS, even if no page uses it.

There’s also the opposite, @source not inline(), to make sure some classes are never generated, even if they appear in your files:

@source not inline("bg-blue-500");

When the class is in a file Tailwind ignores

Sometimes the class name is complete, but Tailwind still doesn’t generate it. Then check which files it scans. Tailwind v4 scans your project but skips:

So if you use a UI library built with Tailwind that lives in node_modules, register it with @source. The path is relative to your CSS file:

@import "tailwindcss";
@source "../node_modules/@acmecorp/ui-lib";

Prefer the map when the value controls a component variant. It is easy to review, keeps invalid values out, and lets each variant use a different combination of utilities.

Want me to talk about your product? You can sponsor this site.

Related posts about css: