Performance: First-child and last-child targeting
This affects how CSS selectors target elements, impacting style calculation and layout performance.
<ul class="list-none"> <li class="text-red-500">Item 1</li> <li>Item 2</li> <li class="text-blue-500">Item 3</li> </ul>
<ul class="list-none"> <li class="first:text-red-500 last:text-blue-500">Item 1</li> <li class="first:text-red-500 last:text-blue-500">Item 2</li> <li class="first:text-red-500 last:text-blue-500">Item 3</li> </ul>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| :first-child and :last-child selectors on large lists | High (checks each child element) | Medium (style recalculation triggers layout) | Medium (style changes may repaint) | [X] Bad |
| Direct class application to first and last items | Low (no complex selector matching) | Low (minimal style recalculation) | Low (only changed elements repaint) | [OK] Good |
first:bg-blue-500 do in a list of items?first: prefixfirst: prefix in Tailwind targets only the first child element in a group.bg-blue-500 sets the background color to blue. Combined with first:, only the first item gets this color.first:bg-blue-500 targets first child [OK]first: and last: prefixes without hyphens or camel case.last:, so last:text-red-500 applies red text to the last child.last: prefix for last child [OK]last:, no hyphens or camelCase [OK]<ul> <li class="first:text-green-600 last:text-red-600">Apple</li> <li class="first:text-green-600 last:text-red-600">Banana</li> <li class="first:text-green-600 last:text-red-600">Cherry</li> </ul>
first:text-green-600 and last:text-red-600. These apply green text to the first child and red text to the last child only.<section> differently using Tailwind. You wrote:<section> <p class="first:text-blue-700 last:text-red-700">Intro</p> <p class="first:text-blue-700 last:text-red-700">Content</p> <p class="first:text-blue-700 last:text-red-700">Summary</p> </section>
first: and last: work on the same elementfirst: and last: are on the same element, only one can apply because an element cannot be both first and last child simultaneously.first:text-blue-700 applies but last:text-red-700 does not. Summary is last child, so last:text-red-700 applies but first: does not. Content matches neither.first: and last: classes are on the same element but only one applies per element. -> Option A<nav>. You want the first link to have a green border, the last link to have a red border, and all others to have a gray border. Which Tailwind classes and structure will achieve this best?first: and last: prefixes.border-gray-300 to all links. Add first:border-green-500 last:border-red-500 to all links too. First link matches first: (green override), last matches last: (red override), others match neither (gray).<br><pre><nav>
<a class="first:border-green-500 last:border-red-500 border-gray-300">Home</a>
<a class="first:border-green-500 last:border-red-500 border-gray-300">About</a>
<a class="first:border-green-500 last:border-red-500 border-gray-300">Services</a>
<a class="first:border-green-500 last:border-red-500 border-gray-300">Blog</a>
<a class="first:border-green-500 last:border-red-500 border-gray-300">Contact</a>
</nav>Pass the test and this pattern moves unknown