できない.dev

Tailwind CSS で動的に組み立てたクラス名が反映されない

Tailwind の JIT スキャナはソース上の完全な文字列だけをクラス候補に拾う。
bg-${color}-500 のような実行時補完は検出されず CSS に出力されない。
完全文字列を分岐で書く、safelist に登録する、arbitrary value で逃がす、のいずれかで解決する。

公開: 更新:

症状できない · exit 1
grep -c "bg-red-500" out.css
0

2026-11-19 に node:20(Tailwind CSS 3.4.19 / Node.js 20.20.2) で実際に打って取った出力。検証環境の詳細

要約

Tailwind の JIT スキャナは ソースコード上の完全な文字列だけ をクラス候補として抽出する。
テンプレートリテラルで組み立てた `bg-${color}-500` のような文字列は検出されず、最終 CSS にクラスが含まれない。
完全文字列で書く・safelist に入れる・arbitrary value で CSS 変数を渡す、の 3 系統で解決する。

実行例

テンプレートリテラルで組み立てたクラス名はスキャナに拾われず、bg-red-500 の出力件数は 0 のまま「No utility classes were detected」の警告まで出る。
完全な文字列を map に並べ直すと .bg-red-500 の宣言が CSS に現れ、safelist に pattern を登録した場合も bg-green-900 が出力される。

$ npx tailwindcss -i src/input.css -o out.css
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
 
Rebuilding...
 
warn - No utility classes were detected in your source files. If this is unexpected, double-check the `content` option in your Tailwind CSS configuration.
warn - https://tailwindcss.com/docs/content-configuration
 
Done in 99ms.
$ grep -c "bg-red-500" out.css
0
$ echo $?
1
$ npx tailwindcss -i src/input.css -o out.css
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
 
Rebuilding...
 
Done in 92ms.
$ grep -A2 '\.bg-red-500' out.css
.bg-red-500 {
  --tw-bg-opacity: 1;
  background-color: rgb(239 68 68 / var(--tw-bg-opacity, 1))
$ npx tailwindcss -i src/input.css -o out.css
Browserslist: caniuse-lite is outdated. Please run:
  npx update-browserslist-db@latest
  Why you should do it regularly: https://github.com/browserslist/update-db#readme
 
Rebuilding...
 
Done in 139ms.
$ grep -c "bg-green-900" out.css
22

— 2026-11-19 時点の出力

検証環境Node.js 20.20.2 / npm 10.8.2 / Python 3.11.2 / 2026-11-19 検証(ほか 2 件)
検証日
実行環境
node:20 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 20.20.2
  • npm 10.8.2
  • Python 3.11.2
  • Git 2.39.5
  • Tailwind CSS 3.4.19

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. 完全文字列で書く(最も推奨)

// NG: スキャンで検出されない
<div className={`bg-${color}-500`}>...</div>
 
// OK: 完全文字列を map に並べる
const bgMap = {
  red: "bg-red-500",
  blue: "bg-blue-500",
} as const;
<div className={bgMap[color]}>...</div>

公式の dynamic class names ガイド(新しいタブで開く) でも、変数を文字列補間で組み立てず 完全クラス名を分岐させる ことが第一の推奨と明記されている。

2. safelist に登録する

// tailwind.config.js
module.exports = {
  content: ["./src/**/*.{ts,tsx}"],
  safelist: [
    "bg-red-500",
    "bg-blue-500",
    { pattern: /bg-(red|blue|green)-(100|500|900)/ },
  ],
};

外部データに依存して静的解析に乗せられない場合は safelist(新しいタブで開く) を使う。
pattern で正規表現指定も可能で、グループ単位でまとめて登録できる。
この tailwind.config.js の書き方は v3 までのもので、v4 では CSS 側の @source inline(...)(新しいタブで開く) に置き換わっている。

3. arbitrary value で CSS 変数を渡す

<div
  style={{ "--brand": color } as React.CSSProperties}
  className="bg-[var(--brand)]"
>
  ...
</div>

色やサイズが完全に実行時依存(テーマカラーの動的選択など)なら、CSS 変数を介して Tailwind の任意値構文 bg-[var(--brand)] で受け取るのが自然である。
クラス名の組み立ては諦め、値を style 経由で渡すことになる。

よくある原因

  1. テンプレートリテラルで一部だけ変数化している。
    `text-${size}` のように書くと、完全な text-lg の連続文字列がソース中に出てこない。
  2. 三項演算子や props を埋め込んでいる。
    `text-${isActive ? 'red' : 'blue'}-500` も同様で、完全な text-red-500 がコード上に現れない。
    親から受け取った props を `bg-${prop}` で展開する書き方も、同じ罠にはまる。
  3. 配列や map でクラス名を生成している。
    色配列をループで回してクラス名を組み立てても、実行時に生成される文字列はビルド時には存在しない。
  4. 結果として、ビルド後の最終 CSS に対象クラスが出力されていない。
    そのため、ブラウザに当てるべき値が無い。