In tailwind why does bg-[#5ad0a1]
work, however assigning the hex to variable for example const color = '#5ad0a1';
then bg-[${color}]
not work?
<span className={`inline-flex items-center rounded-md bg-[${color}] px-2 py-2 text-md font-base text-[${color}]`}>
{params?.value}
</span>
2
Answers
Because Tailwind needs the actual color (#5ad0a1) to be able to parse the code to create the css. It does not convert it to html with javascript and then parse the code. So it never sees
#5ad0a1
only ever${color}
So it is likely creating something like
What you can try is use css variables
In your css add, or if you need it to be variables add the root css in your js.
Then in your class use
From tailwind’s docs:
So this is wrong and tailwind wont find the classes:
The recommended way is to map your props to static class names: