These selectors help you style the very first or very last item inside a group. It makes your page look neat and organized.
First-child and last-child in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
selector:first-child { property: value; } selector:last-child { property: value; }
:first-child selects an element if it is the first child of its parent.
:last-child selects an element if it is the last child of its parent.
li:first-child { color: red; }
p:last-child { margin-bottom: 2rem; }
button:first-child { background-color: lightblue; }
div:last-child { border: none; }
This example shows a shopping list. The first item is highlighted with a green background and bold text. The last item has a pink background and italic text. The other items have a simple border and normal style.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>First-child and Last-child Example</title> <style> ul { list-style: none; padding: 0; max-width: 300px; margin: 1rem auto; font-family: Arial, sans-serif; } li { padding: 0.5rem; border: 1px solid #ccc; margin-bottom: 0.5rem; border-radius: 0.25rem; } li:first-child { background-color: #d1e7dd; font-weight: bold; } li:last-child { background-color: #f8d7da; font-style: italic; } </style> </head> <body> <main> <h1>Shopping List</h1> <ul> <li>Apples</li> <li>Bananas</li> <li>Carrots</li> <li>Dates</li> </ul> </main> </body> </html>
If the element is not the very first or last child of its parent, these selectors won't apply.
They only look at the position among siblings, not the type of element.
Use these selectors to improve user experience by visually grouping or separating items.
:first-child styles the first child element inside a parent.
:last-child styles the last child element inside a parent.
They help make lists and groups look clearer and more attractive.
Practice
:first-child do?Solution
Step 1: Understand the selector purpose
The:first-childselector targets only the very first child element within a parent container.Step 2: Compare with other options
:last-childtargets the last child, others do not match the description.Final Answer:
It selects the first child element inside its parent. -> Option BQuick Check:
:first-child = first child selected [OK]
- Confusing :first-child with :last-child
- Thinking it selects all children
- Assuming it selects the parent
<ul> list?Solution
Step 1: Identify the target element
The goal is to style the last<li>inside a<ul>, so the selector must targetlielements that are last children.Step 2: Check selector correctness
ul > li:last-childcorrectly selects the lastlidirectly insideul. Other options either select the wrong element or use incorrect syntax.Final Answer:
ul > li:last-child { color: red; } -> Option AQuick Check:
Correct syntax for last child inside ul = ul > li:last-child { color: red; } [OK]
- Using :last-child on the parent instead of the child
- Confusing :first-child with :last-child
- Missing the direct child combinator >
<ul> <li>Apple</li> <li>Banana</li> <li>Cherry</li> </ul>
And CSS:
li:first-child { color: blue; }
li:last-child { color: green; }What color will the text "Banana" have when rendered?
Solution
Step 1: Identify which elements get styled
li:first-childstyles the firstli(Apple),li:last-childstyles the lastli(Cherry).Step 2: Determine Banana's position
Banana is the secondli, so it is neither first nor last child, so no color styles apply.Final Answer:
Black (default) -> Option CQuick Check:
Banana is middle child, no color applied [OK]
- Assuming all list items get styled
- Confusing order of children
- Thinking styles cascade to siblings
<section> green and blue respectively:section p:first-child { color: green; }
section p:last-child { color: blue; }But only the first paragraph turns green, the last paragraph stays black. Why?
Solution
Step 1: Understand :first-child and :last-child context
These selectors check if the element is the very first or last child of its parent, regardless of type.Step 2: Check if
If other elements (like headings or divs) come before or after thepis first or last childp, thenpis not first or last child, so styles won't apply.Final Answer:
Becausepelements are not the first or last child ofsection. -> Option DQuick Check:
:first-child and :last-child depend on element position, not type [OK]
- Assuming :first-child means first of type
- Ignoring other sibling elements
- Thinking syntax is wrong when it's position issue
<li> elements inside every <ul> differently, but only if the <li> is also the first or last child of its parent. Which CSS selectors correctly achieve this?Solution
Step 1: Understand the requirement
We want to style onlylielements that are the first or last child of theirulparent.Step 2: Choose selectors that target direct children and correct position
ul > li:first-childandul > li:last-childselectlielements that are direct children and first or last child respectively. This matches the requirement exactly.Step 3: Evaluate other options
ul li:first-child { font-weight: bold; } and ul li:last-child { font-style: italic; } misses the direct child combinator, which can cause incorrect matches if nested lists exist. ul:first-child li { font-weight: bold; } and ul:last-child li { font-style: italic; } incorrectly applies :first-child toul. ul li:first-of-type { font-weight: bold; } and ul li:last-of-type { font-style: italic; } uses :first-of-type which selects firstliregardless of position among siblings.Final Answer:
ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } -> Option AQuick Check:
Direct child + :first-child and :last-child = ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } [OK]
- Omitting > combinator causing wrong matches
- Confusing :first-child with :first-of-type
- Applying selectors to wrong parent element
