![](/img/trans.png)
[英]Do not count a specific TR in a table, and apply Even/Odd CSS style to all others
[英]What is the correct way to apply classes dynamically to odd/ even rows of Table TR element using Tailwind CSS
尝试在我的 tailwindcss 样式页面中交替行颜色,下面的代码对我的 <tr> 没有影响:
<style>
tr:nth-child(even) {
class="bg-gray-50";
}
tr:nth-child(od) {
class="bg-white";
}
</style>
请问我错过了什么?
在我看来,您正在混合 CSS 样式和 HTML 类。 你必须选择其中一个。 假设.bg-gray-50
对应于#ccc
,您可以按如下方式应用样式:
<style>
tr:nth-child(even) {
background-color: #ccc;
}
tr:nth-child(od) {
background-color: #fff;
}
</style>
您正在尝试将某种 css 类设置为 css 标签。
另外我建议你设置border-collapse: collpase;
到你的桌子,这样,你的单元格之间就不会有分隔。
您必须像下面的演示一样直接设置 css:
table{ border-collapse: collapse; } tr:nth-child(even) { /*class="bg-gray-50";*/ background: gray; } tr:nth-child(od) { /* class="bg-white";*/ background: white; }
<table> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </tbody> </table>
Tailwindish 的方式是使用even:someclass
或odd:someclass
。 需要先开启,详情看这里
正如 stefan.at.wpf 提到的,你应该像这样扩展你的顺风配置:
// tailwind.config.js
module.exports = {
// ...
variants: {
extend: {
backgroundColor: ['even'],
}
},
}
然后你甚至可以在你的课程中使用:
<tr class="even:bg-grey">
您可以使用@apply 来实现这一点,然后列出要应用于相关元素的顺风类。
tr:nth-child(even) {
@apply bg-blue-50;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.