Tailwind CSS interview question · Most asked

Why doesn't className={`bg-${color}-500`} work, and what should you do instead?

Create a free account to save this page, take notes and track your progress. Sign in

Summary

Tailwind never sees the complete string bg-red-500 in your source, so it doesn't generate the rule. Map values to complete class strings (const styles = { red: "bg-red-500", green: "bg-green-500" }), or safelist genuinely dynamic values with @source inline().

Longer Answer

This is the most common Tailwind production bug; it often hides locally when an earlier build or a dev safelist still has the class.

Key Points

  • Fragments aren't detected.
  • Map to complete strings.
  • Safelist only true data.
  • Hidden by stale builds.

Deep dive: Tailwind CSS in Production: The Badge That Lost Its Colour

Explanation

What interviewers look for

The mapping solution.

More most-asked Tailwind CSS questions

← All Tailwind CSS questions

Was this page helpful?