Styling List Items Using :first-child and :last-child
📖 Scenario: You are creating a simple webpage that shows a list of fruits. You want to style the first fruit in the list with a green background and the last fruit with a red background. This helps users quickly see the start and end of the list.
🎯 Goal: Build an HTML page with a list of fruits and use CSS :first-child and :last-child selectors to color the first item green and the last item red.
📋 What You'll Learn
Create an unordered list with exactly these fruits in order: Apple, Banana, Cherry, Date, Elderberry
Add CSS to style the first list item with a green background using
:first-childAdd CSS to style the last list item with a red background using
:last-childUse semantic HTML5 structure with
<main> and <section>Ensure the page is responsive and text is readable
💡 Why This Matters
🌍 Real World
Styling first and last items in lists is common in menus, navigation bars, and content lists to highlight important elements or create visual balance.
💼 Career
Understanding CSS pseudo-classes like :first-child and :last-child is essential for front-end developers to create polished, accessible, and user-friendly web pages.
Progress0 / 4 steps