First-child and last-child targeting helps you style the first or last item in a group differently. This makes your design clearer and more organized.
First-child and last-child targeting in Tailwind
first:<utility> last:<utility>
Use first: before any Tailwind utility to style the first child element.
Use last: before any Tailwind utility to style the last child element.
<li class="first:text-red-500">Item 1</li> <li>Item 2</li>
<button>Save</button>
<button class="last:rounded-lg">Cancel</button><p class="first:mt-4">Paragraph 1</p> <p>Paragraph 2</p>
This example shows a list where the first item has a light blue background and the last item has a light green background. The middle item has no special background. Borders separate the items.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>First and Last Child Tailwind Example</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body class="p-6"> <ul class="border border-gray-300 max-w-sm mx-auto"> <li class="first:bg-blue-100 last:bg-green-100 p-4 border-b border-gray-200">First item</li> <li class="p-4 border-b border-gray-200">Middle item</li> <li class="last:bg-green-100 p-4">Last item</li> </ul> </body> </html>
Make sure your HTML elements are siblings for first: and last: to work correctly.
You can combine these with other Tailwind utilities for more effects.
Use browser DevTools to inspect and test your styles live.
first: targets the first child element to style it differently.
last: targets the last child element to style it differently.
These help make lists and groups look neat and clear.
Practice
first:bg-blue-500 do in a list of items?Solution
Step 1: Understand the
Thefirst:prefixfirst:prefix in Tailwind targets only the first child element in a group.Step 2: Apply the background color
The classbg-blue-500sets the background color to blue. Combined withfirst:, only the first item gets this color.Final Answer:
It changes the background color of the first item to blue. -> Option AQuick Check:
first:bg-blue-500targets first child [OK]
- Thinking it styles all items
- Confusing first-child with last-child
- Assuming it removes styles instead of adding
Solution
Step 1: Recall Tailwind pseudo-class syntax
Tailwind usesfirst:andlast:prefixes without hyphens or camel case.Step 2: Identify the correct prefix for last child
The correct prefix islast:, solast:text-red-500applies red text to the last child.Final Answer:
last:text-red-500 -> Option DQuick Check:
Uselast:prefix for last child [OK]
last:, no hyphens or camelCase [OK]- Using CSS syntax like last-child:
- Writing camelCase or lowercase without colon
- Adding extra hyphens or misspelling
<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>
What color will the text of each list item be?
Solution
Step 1: Understand the classes on each <li>
Each <li> hasfirst:text-green-600andlast:text-red-600. These apply green text to the first child and red text to the last child only.Step 2: Apply styles to list items
Apple is the first <li>, so it gets green text. Cherry is the last <li>, so it gets red text. Banana is in the middle, so it keeps default text color.Final Answer:
Apple is green, Banana is default, Cherry is red -> Option BQuick Check:
first: green, last: red, others default [OK]
- Assuming all items get styled
- Mixing up first and last child colors
- Thinking middle items get styles too
<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>
In practice, the first paragraph gets blue text, the last gets red text, and the middle stays default. What best explains this behavior?
Solution
Step 1: Understand how
When bothfirst:andlast:work on the same elementfirst:andlast:are on the same element, only one can apply because an element cannot be both first and last child simultaneously.Step 2: Analyze the HTML structure
Each paragraph has both classes. Intro is first child, sofirst:text-blue-700applies butlast:text-red-700does not. Summary is last child, solast:text-red-700applies butfirst:does not. Content matches neither.Final Answer:
Thefirst:andlast:classes are on the same element but only one applies per element. -> Option AQuick Check:
One element can't be both first and last child [OK]
- Thinking classes must be on parent
- Believing all siblings must share classes
- Expecting both styles on same element
<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?Solution
Step 1: Understand how to apply default and conditional styles
All links should have a gray border by default. The first and last links get additional border colors usingfirst:andlast:prefixes.Step 2: Check class placement and repetition
Applyborder-gray-300to all links. Addfirst:border-green-500 last:border-red-500to all links too. First link matchesfirst:(green override), last matcheslast:(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>Final Answer:
Option C correctly applies default and conditional borders. -> Option CQuick Check:
Default border on all, first and last get overrides [OK]
- Adding first/last classes to all links
- Putting first/last classes on wrong elements
- Not applying default styles to all links
Ready to prove it?
Pass the test and this pattern moves unknown
