Styling List Items with First-child and Last-child in Tailwind CSS
📖 Scenario: You are creating a simple webpage that shows a list of fruits. You want to style the first and last items in the list differently to make them stand out.
🎯 Goal: Build an HTML list of fruits and use Tailwind CSS classes to style the first item with a green background and the last item with a blue background using first: and last: variants.
📋 What You'll Learn
Create an unordered list with exactly 5 fruit names as list items.
Use Tailwind CSS first:bg-green-200 to style the first list item with a light green background.
Use Tailwind CSS last:bg-blue-200 to style the last list item with a light blue background.
Ensure the list is inside a <main> element with proper semantic HTML.
Make sure the page is responsive and uses semantic HTML5 elements.
💡 Why This Matters
🌍 Real World
Styling lists with first-child and last-child selectors is common in menus, navigation bars, and item lists to highlight important elements.
💼 Career
Understanding how to use Tailwind CSS variants for pseudo-classes like first-child and last-child helps you build visually appealing and accessible user interfaces quickly.
Progress0 / 4 steps
1
Create the HTML structure with a list of fruits
Create a <main> element containing an unordered list <ul> with exactly five list items <li>. The list items should be: Apple, Banana, Cherry, Date, and Elderberry.
Tailwind
Hint
Use semantic tags: <main> for the main content and <ul> for the list. Add exactly five <li> elements with the fruit names.
2
Add Tailwind CSS classes for first-child and last-child styling
Add Tailwind CSS classes to the <ul> element so that the first list item uses the class first:bg-green-200 and the last list item uses the class last:bg-blue-200. This will style the first item with a light green background and the last item with a light blue background.
Tailwind
Hint
Add the classes first:bg-green-200 and last:bg-blue-200 inside the class attribute of the <ul> tag.
3
Wrap the list in a container with padding and center alignment
Add a div container around the <ul> inside <main>. Give this div the Tailwind CSS classes p-6 for padding and max-w-md mx-auto to center it horizontally with a max width.
Tailwind
Hint
Wrap the <ul> inside a <div> with classes p-6 max-w-md mx-auto to add padding and center the content.
4
Add accessible roles and aria-label for the list
Add the attribute role="list" and aria-label="Fruit list" to the <ul> element to improve accessibility for screen readers.
Tailwind
Hint
Add role="list" and aria-label="Fruit list" inside the <ul> tag for better accessibility.
Practice
(1/5)
1. What does the Tailwind class first:bg-blue-500 do in a list of items?
easy
A. It changes the background color of the first item to blue.
B. It changes the background color of all items to blue.
C. It changes the background color of the last item to blue.
D. It removes the background color from the first item.
Solution
Step 1: Understand the first: prefix
The first: prefix in Tailwind targets only the first child element in a group.
Step 2: Apply the background color
The class bg-blue-500 sets the background color to blue. Combined with first:, only the first item gets this color.
Final Answer:
It changes the background color of the first item to blue. -> Option A
Quick Check:
first:bg-blue-500 targets first child [OK]
Hint: Remember: first: styles only the first child [OK]
Common Mistakes:
Thinking it styles all items
Confusing first-child with last-child
Assuming it removes styles instead of adding
2. Which of these is the correct Tailwind syntax to style only the last child element with red text?
easy
A. last-child:text-red-500
B. lastchild:text-red-500
C. lastChild:text-red-500
D. last:text-red-500
Solution
Step 1: Recall Tailwind pseudo-class syntax
Tailwind uses first: and last: prefixes without hyphens or camel case.
Step 2: Identify the correct prefix for last child
The correct prefix is last:, so last:text-red-500 applies red text to the last child.
Final Answer:
last:text-red-500 -> Option D
Quick Check:
Use last: prefix for last child [OK]
Hint: Tailwind uses simple last:, no hyphens or camelCase [OK]
In practice, the first paragraph gets blue text, the last gets red text, and the middle stays default. What best explains this behavior?
medium
A. The first: and last: classes must be on the same element but only one applies per element.
B. Tailwind's first: and last: only work if all siblings share the same class.
C. The first: and last: classes must be on the parent, not children.
D. The first: and last: classes must be on each paragraph without repeating.
Solution
Step 1: Understand how first: and last: work on the same element
When both first: and last: 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, so 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.
Final Answer:
The first: and last: classes are on the same element but only one applies per element. -> Option A
Quick Check:
One element can't be both first and last child [OK]
Hint: One element can only be first or last child, not both [OK]
Common Mistakes:
Thinking classes must be on parent
Believing all siblings must share classes
Expecting both styles on same element
5. You have a navigation bar with 5 links inside 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?