0
0
Tailwindmarkup~20 mins

First-child and last-child targeting in Tailwind - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
First and Last Child Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
Which Tailwind class targets only the first child element?
You want to apply a red text color only to the first child of a list. Which Tailwind CSS class should you use?
Tailwind
<ul>
  <li class="???">Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
Afirst-child:text-red-500
Bchild:first:text-red-500
Ctext-red-500:first
Dfirst:text-red-500
Attempts:
2 left
💡 Hint
Tailwind uses the prefix 'first:' to apply styles to the first child element.
selector
intermediate
2:00remaining
How to style only the last child with Tailwind?
You want to add a bottom border only to the last item in a navigation menu. Which Tailwind class applies this style correctly?
Tailwind
<nav>
  <a href="#" class="???">Home</a>
  <a href="#">About</a>
  <a href="#">Contact</a>
</nav>
Aborder-b-2:last
Blast-child:border-b-2
Clast:border-b-2
Dchild:last:border-b-2
Attempts:
2 left
💡 Hint
Tailwind uses 'last:' prefix to target the last child element.
rendering
advanced
2:00remaining
What is the visual result of this Tailwind code?
Given this HTML and Tailwind classes, which items have a green background?
Tailwind
<ul class="list-disc">
  <li class="first:bg-green-300 last:bg-green-300">Apple</li>
  <li class="first:bg-green-300 last:bg-green-300">Banana</li>
  <li class="first:bg-green-300 last:bg-green-300">Cherry</li>
</ul>
AOnly 'Apple' and 'Cherry' have green backgrounds.
BAll items have green backgrounds.
COnly 'Apple' has a green background.
DOnly 'Cherry' has a green background.
Attempts:
2 left
💡 Hint
The 'first:' and 'last:' prefixes apply styles only to the first and last child respectively.
accessibility
advanced
2:00remaining
How to ensure keyboard focus styles only on the first child using Tailwind?
You want to add a visible focus ring only on the first button inside a toolbar when it receives keyboard focus. Which Tailwind class combination achieves this?
Tailwind
<div role="toolbar">
  <button class="???">Save</button>
  <button>Cancel</button>
  <button>Delete</button>
</div>
Afirst:focus:outline-none first:focus:ring-2 first:focus:ring-blue-500
Bfocus:first:outline-none focus:first:ring-2 focus:first:ring-blue-500
Cfocus:first-child:outline-none focus:first-child:ring-2 focus:first-child:ring-blue-500
Dfirst-child:focus:outline-none first-child:focus:ring-2 first-child:focus:ring-blue-500
Attempts:
2 left
💡 Hint
Tailwind uses 'first:' and 'focus:' prefixes combined to target first child on focus.
layout
expert
2:00remaining
Which Tailwind class combination correctly adds margin only between siblings except before the first child?
You want to add a left margin of 1rem only to all list items except the first one, using Tailwind. Which option achieves this spacing correctly?
Tailwind
<ul class="flex">
  <li class="???">Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
Afirst:ml-0 ml-4
Bml-4 first:ml-0
Cml-4 last:ml-0
Dml-0 first:ml-4
Attempts:
2 left
💡 Hint
Apply a default margin and override it on the first child with zero margin.