Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to apply classes dynamically to odd/ even rows of Table TR element using Tailwind CSS

Trying to alternate row colors in my tailwindcss styled page, the code below has no effect on my <tr>s:

<style>
    tr:nth-child(even) {
        class="bg-gray-50";
    }
    tr:nth-child(od) {
        class="bg-white";
    }
</style>

What am I missing out please?

like image 679
user1729972 Avatar asked Nov 24 '25 16:11

user1729972


1 Answers

Add even and odd prefixes to your class list

<tbody>
    <tr class="even:bg-gray-50 odd:bg-white"></tr>
</tbody>

See First, last, odd, and even from their documentation

like image 192
Roman Mahotskyi Avatar answered Nov 27 '25 07:11

Roman Mahotskyi



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!