0
0
CSSmarkup~5 mins

First-child and last-child in CSS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the CSS selector :first-child do?
It selects the very first child element inside its parent element.
Click to reveal answer
beginner
What does the CSS selector :last-child do?
It selects the very last child element inside its parent element.
Click to reveal answer
intermediate
Can :first-child select the first element of a specific type only?
No, :first-child selects the first child regardless of its type. To select the first of a specific type, use :first-of-type.
Click to reveal answer
beginner
How would you style only the first paragraph inside a <div> using :first-child?
Use div p:first-child { /* styles */ }. This styles a p only if it is the first child of the div.
Click to reveal answer
beginner
What happens if you use :last-child on an element that is not the last child?
The style will not apply because :last-child only matches elements that are the last child of their parent.
Click to reveal answer
Which CSS selector targets the first child element of its parent?
A:first-child
B:last-child
C:nth-child(2)
D:only-child
What does li:last-child select?
AThe last <code>li</code> inside its parent
BAll <code>li</code> elements
CThe first <code>li</code> inside its parent
DOnly <code>li</code> elements with no siblings
If a p is the second child of a div, will div p:first-child style it?
AYes, always
BNo, only if it is the first child
CYes, if it is the only <code>p</code>
DNo, it styles the last child
Which selector would style the last li in a list?
Ali:first-child
Bli:nth-child(1)
Cli:last-child
Dli:only-child
What is true about :first-child and :last-child selectors?
AThey select all children of a parent.
BThey select elements by their class name.
CThey select elements by their ID.
DThey select elements based on their position among siblings.
Explain how the :first-child and :last-child selectors work in CSS.
Think about how these selectors depend on the element's position among siblings.
You got /3 concepts.
    Describe a real-life example where you would use :first-child or :last-child in styling a webpage.
    Consider menus, lists, or groups of items on a page.
    You got /3 concepts.