Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't tailwind find my dynamic class?

Tags:

tailwind-css

so i'm trying to load classes dynamically based on an Object Array

<div v-for="item in items"
     :key="item.id"
     :class="'text-' + item.color + '-600'"
>
{{ item.name }}
</div>

i checked on the Elements Panel on the browser and the class property loads correctly but the css doesn't.

Why is that so? Any help would be greatly appreciated.

like image 294
HanirTxZ Avatar asked Sep 12 '25 21:09

HanirTxZ


1 Answers

Tailwind generates a CSS file containing only the classes used in your project. It can't recognise the dynamically generated class name you're using so doesn't include it in the output file.

To quote the documentation:

The most important implication of how Tailwind extracts class names is that it will only find classes that exist as complete unbroken strings in your source files.

If you use string interpolation or concatenate partial class names together, Tailwind will not find them and therefore will not generate the corresponding CSS.

https://tailwindcss.com/docs/content-configuration#class-detection-in-depth

I answered a similar question on this subject recently: Fontawesome icons not accepting color props through react functional components via tailwindcss


To resolve the issue, either pass in the full class name or safelist it instead.

like image 178
Nathan Dawson Avatar answered Sep 14 '25 14:09

Nathan Dawson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!