Performance: Odd and even row styling
This affects the rendering speed and visual stability of tables or lists by applying styles conditionally to rows.
<table class="w-full"> <tr class="odd:bg-white even:bg-gray-100">...</tr> <tr class="odd:bg-white even:bg-gray-100">...</tr> <tr class="odd:bg-white even:bg-gray-100">...</tr> <tr class="odd:bg-white even:bg-gray-100">...</tr> </table>
<table> <tr class="bg-white">...</tr> <tr class="bg-gray-100">...</tr> <tr class="bg-white">...</tr> <tr class="bg-gray-100">...</tr> </table>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual class on each row | Increased DOM nodes and attributes | Multiple reflows if classes change | Higher paint cost due to more DOM | [X] Bad |
| Tailwind odd: and even: variants | Minimal DOM changes, styles via CSS | No extra reflows | Lower paint cost, efficient style application | [OK] Good |
odd: prefix do in Tailwind CSS when applied to a list of rows?odd: prefix meaningodd: prefix targets elements in odd positions (1st, 3rd, 5th, etc.) in a list or table.odd: = odd rows styled [OK]even:.even:bg-gray-200.even:bg-gray-200 -> Option Aeven: + class = even:bg-gray-200 [OK]even: before class for even rows [OK]odd: instead of even:<ul> <li class="odd:bg-blue-100 even:bg-green-100">Item 1</li> <li class="odd:bg-blue-100 even:bg-green-100">Item 2</li> <li class="odd:bg-blue-100 even:bg-green-100">Item 3</li> <li class="odd:bg-blue-100 even:bg-green-100">Item 4</li> </ul>
bg-blue-100, even positions (2, 4) get bg-green-100.<tr class="odd:bg-gray-100 even:bg-white">...</tr>
<tbody>.<tr> elements are not inside <tbody> or <table>, the browser may not count rows correctly, so even rows don't get styled.<tbody> or <table> is missing, so odd/even can't apply. -> Option C<tbody> for odd/even to work [OK]even: doesn't work on <tr><tbody>odd: and even: prefixes cannot condition styles on text content length, as CSS lacks selectors for character count.odd:bg-blue-100 even:bg-green-100 classes to rows and use JavaScript to detect text length >10 characters, then add a modifier class (e.g., long-text). Combine with custom variants like long-text:odd:bg-blue-100 via Tailwind config or arbitrary variants for conditional application.odd:bg-blue-100 even:bg-green-100 and add JavaScript to add a class for long text rows. -> Option A:has() to handle text lengthPass the test and this pattern moves unknown