Skip to content

Not able to apply dynamic value to className on the basis of condition #18456

Closed Answered by wongjn
muhammadmeharbhutta asked this question in Help
Discussion options

You must be logged in to vote

As per the documentation:

Since Tailwind scans your source files as plain text, it has no way of understanding string concatenation or interpolation in the programming language you're using.

❌ Don't construct class names dynamically

<div class="text-{{ error ? 'red' : 'green' }}-600"></div>

In the example above, the strings text-red-600 and text-green-600 do not exist, so Tailwind will not generate those classes.

Instead, make sure any class names you’re using exist in full:

✅ Always use complete class names

<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

If you're using a component library like React or Vue, this means you shouldn't use props to dynamically construct…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by muhammadmeharbhutta
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #18455 on July 05, 2025 19:19.