此问题在此处已有答案:
Why can't I pass variable as a className to tailwind-css?(1个答案)
4天前关闭。
我试图使一个网格的基础上,网格大小变量,我试图设置网格模板行和使用顺风变量。
但是类名不会被编译,而是我得到了带有方括号中的生成值的类名。
const App = () => {
const gridSize = getGridSize();
const players = getPlayers();
// const numberOfPlayers = players.length;
const activePlayer = getActivePlayer();
const gridItems = Array(gridSize * gridSize)
.fill(null)
.map((_, index) => (
<div
key={index}
className="bg-orange-300 rounded-lg w-16 h-16 cursor-pointer"
></div>
));
return (
<div
className={`bg-[${players[activePlayer]}] w-[60vw] h-[100vh] mx-[20vw] flex justify-center items-center`}
>
<div className={`grid grid-rows-${gridSize} grid-cols-${gridSize} gap-2`}>
{gridItems}
</div>
</div>
);
};
export default App;
字符串
的数据
而这里的类名是正确应用的,
的
1条答案
按热度按时间oo7oh9g91#
根据文件:
Tailwind如何提取类名的最重要的含义是,它只会在源文件中找到作为 * 完整完整的字符串 * 存在的类。
如果你使用字符串插值或将部分类名连接在一起,Tailwind将找不到它们,因此不会生成相应的CSS:
不要动态构造类名
字符串
在上面的示例中,字符串
text-red-600
和text-green-600
不存在,因此Tailwind不会生成这些类。相反,请确保您使用的任何类名完整存在:始终使用完整的类名
型
你可以使用
style
属性,比如:型