英文:
How can I change text color on hover while using template literals?
问题
我有以下这段代码:
<div
id={id}
className={`rounded-lg bg-background-section h-72 p-4 flex flex-col items-center relative hover:bg-background-highlight hover:text-[${color}] transition ease-in-out`}
>
你好
</div>
我正在渲染多个此div的实例,并为每个实例获取不同的color
值。问题是,悬停效果在我手动在代码中设置所需的颜色之前不起作用:
示例:
hover:text-[#7cc327]
保存后,转到浏览器测试它(它正常工作)。
然后回到原始代码:
hover:text-[${color}]
现在它会使用变量中的颜色。我必须对我有的9种颜色都做同样的事情,但这只是浪费时间,因为它不应该这样工作,而且... 每当重新启动开发服务器时,它就停止工作。
知道出了什么问题吗?
英文:
I have the following piece of code:
<div
id={id}
className={`rounded-lg bg-background-section h-72 p-4 flex flex-col items-center relative hover:bg-background-highlight hover:text-[${color}] transition ease-in-out`}
>
Hello there
</div>
I'm rendering multiple instances of that div, getting a different color
value for each one. Thing is, the hover doesn't work until I manually set the color that I want in the code:
Example:
hover:text-[#7cc327]
Save, go to the browser to test it out (it works just fine).
Go back and restore the code to its original form:
hover:text-[${color}]
And now it takes the color from the variable. I ve to do the same thing for all the 9 colors that I have, but its just a waste of time cause it shouldn't work that way and also... it stops working whenever I restart the dev server.
Any idea of whats going on?
答案1
得分: 3
根据文档:
Tailwind提取类名的最重要影响是它只会在源文件中找到作为_完整不中断字符串_存在的类。
如果你使用字符串插值或连接部分类名在一起,Tailwind将无法找到它们,因此也不会生成相应的CSS:
不要动态构建类名
<div class="text-{{ error ? 'red' : 'green' }}-600"></div>
在上面的例子中,字符串
text-red-600
和text-green-600
不存在,所以Tailwind不会生成这些类。
相反,确保你使用的任何类名都是完整的:始终使用完整类名
<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
你可以考虑在声明color
的地方使用完整的类名(确保这段代码也包含在content
文件中):
const color = `hover:text-[#7cc327]`;
…
<div className={`… ${color} …`}>
否则,如果颜色值真正是动态的或上述方法无法适用于你的用例,你可以使用style
属性,像这样:
<div
className={`… hover:text-[color:--color] …`}
style={{ '--color': color }}
>
英文:
As per 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:
>
> Don’t construct class names dynamically
>
> vue
> <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
>
> vue
> <div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>
>
You could consider using a full class names whereever color
is declared (ensuring that this code is covered in the content
file globs too):
const color = `hover:text-[#7cc327]`;
…
<div className={`… ${color} …`}>
Otherwise, if the color value is going to be truly dynamic or the above method cannot work for your use case, you could use the style
attribute like:
<div
className={`… hover:text-[color:--color] …`}
style={{ '--color': color }}
>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论