All the selectors available in CSS are also available in Tailwind CSS. The first-child
selector lets you apply styles on first-child elements and the last-child
selector is useful to style the last element. However, the nth-child
selector can be used to select any element from a list. You can use the nth-child
selector with even and odd modifiers to select the even and odd elements. Here are 2 methods to select even and odd elements from a list.
Method 1: Using :nth-child
Selector with Even and Odd Modifiers
This method is simple and useful. You can combine the even or odd modifiers with nth-child
selector in TailwindCSS to style the even/odd elements. This syntax looks weird but is more helpful than what TailwindCSS recommends on their official docs. In the following example, you can see how easy it is to select even/odd elements by just adding classes to the parent element.
1 2 3 4 5 6 7 8 9 10 11 12 |
<ul class="list-disc [&>*:nth-child(even)]:text-blue-600 [&>*:nth-child(even)]:font-bold [&>*:nth-child(odd)]:text-green-600 [&>*:nth-child(odd)]:italic "> <li>Even List Item</li> <li>Odd List Item</li> <li>Even List Item</li> <li>Odd List Item</li> <li>Even List Item</li> <li>Odd List Item</li> </ul> |
You can see the working example and also the output below as a screenshot.
Method 2: Using Even Odd Selector Class on Child Elements
Tailwind CSS official docs recommend using the even:
and odd:
modifiers on child elements in a list. If the element is even, the styles with even:
modifiers will apply and if it’s an odd element the classes with :odd
modifier will be applied. This is useful when you are using a framework like react, vue or laravel where you use a dynamic loop to print a list of elements. Here is an example.
1 2 3 4 5 6 7 8 9 10 11 12 |
<ul class="list-disc "> <li class="even:text-blue-600 odd:text-green-600 font-medium">Even List Item</li> <li class="even:text-blue-600 odd:text-green-600 font-medium">Odd List Item</li> <li class="even:text-blue-600 odd:text-green-600 font-medium">Even List Item</li> <li class="even:text-blue-600 odd:text-green-600 font-medium">Odd List Item</li> <li class="even:text-blue-600 odd:text-green-600 font-medium">Even List Item</li> <li class="even:text-blue-600 odd:text-green-600 font-medium">Odd List Item</li> </ul> |
In the above example, the even:
modifier classes will only be applied if it’s an even element in the list and the same goes for the odd elements. You can see the working example on the tailwind playground.
Table Example with Even Odd Selector in TailwindCSS
A table would be a perfect example to demonstrate the even-odd selector in TailwindCSS. In the following code example, all the even child elements have a different background color and all the odd elements in the list have a different color for the background. There is just one place where all this is applied which is the parent element of table rows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
<table class="min-w-full text-xs border border-gray-300"> <thead class="rounded-t-lg bg-white "> <tr class="text-right"> <th title="Ranking" class="p-3 text-left">#</th> <th title="Team name" class="p-3 text-left">Team</th> <th title="Wins" class="p-3">W</th> <th title="Losses" class="p-3">L</th> <th title="Win percentage" class="p-3">Win%</th> <th title="Games behind" class="p-3">GB</th> <th title="Home games" class="p-3">Home</th> <th title="Away games" class="p-3">Away</th> <th title="Last 10 games" class="p-3">L10</th> <th title="Current streak" class="p-3">Streak</th> </tr> </thead> <tbody class="[&>*:nth-child(even)]:bg-gray-100 [&>*:nth-child(odd)]:bg-gray-300"> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>1</span> </td> <td class="px-3 py-2 text-left"> <span>MIA</span> </td> <td class="px-3 py-2"> <span>31</span> </td> <td class="px-3 py-2"> <span>17</span> </td> <td class="px-3 py-2"> <span>.646</span> </td> <td class="px-3 py-2 text-right"> <span>0</span> </td> <td class="px-3 py-2"> <span>17-5</span> </td> <td class="px-3 py-2"> <span>14-12</span> </td> <td class="px-3 py-2"> <span>8-2</span> </td> <td class="px-3 py-2"> <span>W2</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>2</span> </td> <td class="px-3 py-2 text-left"> <span>CHI</span> </td> <td class="px-3 py-2"> <span>30</span> </td> <td class="px-3 py-2"> <span>17</span> </td> <td class="px-3 py-2"> <span>.638</span> </td> <td class="px-3 py-2 text-right"> <span>0.5</span> </td> <td class="px-3 py-2"> <span>17-6</span> </td> <td class="px-3 py-2"> <span>13-11</span> </td> <td class="px-3 py-2"> <span>4-6</span> </td> <td class="px-3 py-2"> <span>W2</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>3</span> </td> <td class="px-3 py-2 text-left"> <span>CLE</span> </td> <td class="px-3 py-2"> <span>30</span> </td> <td class="px-3 py-2"> <span>19</span> </td> <td class="px-3 py-2"> <span>.612</span> </td> <td class="px-3 py-2 text-right"> <span>1.5</span> </td> <td class="px-3 py-2"> <span>15-9</span> </td> <td class="px-3 py-2"> <span>15-10</span> </td> <td class="px-3 py-2"> <span>7-3</span> </td> <td class="px-3 py-2"> <span>W3</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>4</span> </td> <td class="px-3 py-2 text-left"> <span>BKN</span> </td> <td class="px-3 py-2"> <span>29</span> </td> <td class="px-3 py-2"> <span>19</span> </td> <td class="px-3 py-2"> <span>.604</span> </td> <td class="px-3 py-2 text-right"> <span>2</span> </td> <td class="px-3 py-2"> <span>12-13</span> </td> <td class="px-3 py-2"> <span>17-6</span> </td> <td class="px-3 py-2"> <span>4-6</span> </td> <td class="px-3 py-2"> <span>L3</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>5</span> </td> <td class="px-3 py-2 text-left"> <span>MIL</span> </td> <td class="px-3 py-2"> <span>30</span> </td> <td class="px-3 py-2"> <span>20</span> </td> <td class="px-3 py-2"> <span>.600</span> </td> <td class="px-3 py-2 text-right"> <span>2</span> </td> <td class="px-3 py-2"> <span>17-8</span> </td> <td class="px-3 py-2"> <span>13-12</span> </td> <td class="px-3 py-2"> <span>5-5</span> </td> <td class="px-3 py-2"> <span>L1</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>6</span> </td> <td class="px-3 py-2 text-left"> <span>PHI</span> </td> <td class="px-3 py-2"> <span>28</span> </td> <td class="px-3 py-2"> <span>19</span> </td> <td class="px-3 py-2"> <span>.596</span> </td> <td class="px-3 py-2 text-right"> <span>2.5</span> </td> <td class="px-3 py-2"> <span>11-10</span> </td> <td class="px-3 py-2"> <span>17-9</span> </td> <td class="px-3 py-2"> <span>7-3</span> </td> <td class="px-3 py-2"> <span>W2</span> </td> </tr> <tr class="text-right border-b border-opacity-20 "> <td class="px-3 py-2 text-left"> <span>7</span> </td> <td class="px-3 py-2 text-left"> <span>CHA</span> </td> <td class="px-3 py-2"> <span>27</span> </td> <td class="px-3 py-2"> <span>22</span> </td> <td class="px-3 py-2"> <span>.551</span> </td> <td class="px-3 py-2 text-right"> <span>4.5</span> </td> <td class="px-3 py-2"> <span>13-7</span> </td> <td class="px-3 py-2"> <span>14-15</span> </td> <td class="px-3 py-2"> <span>7-3</span> </td> <td class="px-3 py-2"> <span>W1</span> </td> </tr> </tbody> </table> |
You can check the working example and also the screenshot of the output.
Conclusion
The even or odd elements can be selected to apply styles in TailwindCSS with 2 different methods. The first method is to apply classes on the parent element for even-odd children and the second method is to add even and odd modifiers when printing a list with a loop. Both methods are used to select the even and odd elements to apply any styles.