Bird
Raised Fist0
CSSmarkup~20 mins

First-child and last-child in CSS - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
First and Last Child Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
selector
intermediate
2:00remaining
Identify the color of the first list item
Given the CSS below, what color will the first
  • inside the
      have when rendered in the browser?
  • CSS
    ul li:first-child { color: red; }
    ul li:last-child { color: blue; }
    
    <ul>
      <li>Apple</li>
      <li>Banana</li>
      <li>Cherry</li>
    </ul>
    AGreen
    BBlue
    CRed
    DBlack (default color)
    Attempts:
    2 left
    💡 Hint
    Remember, :first-child targets the very first child element of its parent.
    selector
    intermediate
    2:00remaining
    Which element is selected by :last-child?
    Consider the HTML and CSS below. Which element will have a blue background color?
    CSS
    div p:last-child { background-color: blue; }
    
    <div>
      <p>Paragraph 1</p>
      <p>Paragraph 2</p>
      <span>Span element</span>
    </div>
    AParagraph 2
    BNo element is selected
    CSpan element
    DParagraph 1
    Attempts:
    2 left
    💡 Hint
    :last-child selects the last child of the parent, regardless of type.
    🧠 Conceptual
    advanced
    2:30remaining
    Difference between :first-child and :first-of-type
    Which statement correctly explains the difference between :first-child and :first-of-type selectors?
    A<code>:first-child</code> selects the last child; <code>:first-of-type</code> selects the first child.
    B<code>:first-child</code> selects the first element of its type; <code>:first-of-type</code> selects the first child of the parent.
    C<code>:first-child</code> and <code>:first-of-type</code> select the same elements always.
    D<code>:first-child</code> selects the first child of its parent regardless of type; <code>:first-of-type</code> selects the first sibling of its type.
    Attempts:
    2 left
    💡 Hint
    Think about whether the selector cares about the element type or just position.
    layout
    advanced
    2:30remaining
    Styling first and last items in a flex container
    You have a horizontal menu using flexbox. You want the first menu item to have extra left padding and the last menu item to have extra right padding. Which CSS will achieve this?
    CSS
    nav ul {
      display: flex;
      list-style: none;
      padding: 0;
      margin: 0;
    }
    
    nav ul li:first-child {
      padding-left: 2rem;
    }
    
    nav ul li:last-child {
      padding-right: 2rem;
    }
    AUse :first-child and :last-child selectors on the <li> elements inside the flex container.
    BUse :first-of-type and :last-of-type selectors on the <ul> element.
    CAdd padding to the <nav> element instead of the <li> elements.
    DUse JavaScript to add classes to first and last items and style those classes.
    Attempts:
    2 left
    💡 Hint
    Think about which elements are the flex items and how to target them with CSS selectors.
    accessibility
    expert
    3:00remaining
    Accessibility impact of styling only first-child and last-child
    If you style only the first and last items in a navigation list with different colors using :first-child and :last-child, what accessibility concern might arise?
    AUsers relying on keyboard navigation might not perceive the difference if color contrast is insufficient or color is the only cue.
    BThe first and last items will not be focusable by keyboard.
    CThe page will fail HTML validation due to use of :first-child and :last-child selectors.
    DScreen readers will ignore the first and last items because of the CSS selectors.
    Attempts:
    2 left
    💡 Hint
    Think about how color-only styling affects users with visual impairments or color blindness.

    Practice

    (1/5)
    1. What does the CSS selector :first-child do?
    easy
    A. It selects the last child element inside its parent.
    B. It selects the first child element inside its parent.
    C. It selects all child elements except the first.
    D. It selects only the parent element.

    Solution

    1. Step 1: Understand the selector purpose

      The :first-child selector targets only the very first child element within a parent container.
    2. Step 2: Compare with other options

      :last-child targets the last child, others do not match the description.
    3. Final Answer:

      It selects the first child element inside its parent. -> Option B
    4. Quick Check:

      :first-child = first child selected [OK]
    Hint: First-child always picks the very first element inside a parent [OK]
    Common Mistakes:
    • Confusing :first-child with :last-child
    • Thinking it selects all children
    • Assuming it selects the parent
    2. Which of the following is the correct CSS syntax to style the last child of a <ul> list?
    easy
    A. ul > li:last-child { color: red; }
    B. ul:last-child { color: red; }
    C. ul li:first-child { color: red; }
    D. ul:last-child li { color: red; }

    Solution

    1. Step 1: Identify the target element

      The goal is to style the last <li> inside a <ul>, so the selector must target li elements that are last children.
    2. Step 2: Check selector correctness

      ul > li:last-child correctly selects the last li directly inside ul. Other options either select the wrong element or use incorrect syntax.
    3. Final Answer:

      ul > li:last-child { color: red; } -> Option A
    4. Quick Check:

      Correct syntax for last child inside ul = ul > li:last-child { color: red; } [OK]
    Hint: Use parent > child:last-child to style last child only [OK]
    Common Mistakes:
    • Using :last-child on the parent instead of the child
    • Confusing :first-child with :last-child
    • Missing the direct child combinator >
    3. Given this HTML:
    <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?
    medium
    A. Blue
    B. Green
    C. Black (default)
    D. Both blue and green

    Solution

    1. Step 1: Identify which elements get styled

      li:first-child styles the first li (Apple), li:last-child styles the last li (Cherry).
    2. Step 2: Determine Banana's position

      Banana is the second li, so it is neither first nor last child, so no color styles apply.
    3. Final Answer:

      Black (default) -> Option C
    4. Quick Check:

      Banana is middle child, no color applied [OK]
    Hint: Only first or last child get styles, middle ones stay default [OK]
    Common Mistakes:
    • Assuming all list items get styled
    • Confusing order of children
    • Thinking styles cascade to siblings
    4. This CSS code is intended to color the first and last paragraphs inside a <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?
    medium
    A. Because the colors green and blue are invalid.
    B. Because the CSS syntax is incorrect.
    C. Because section cannot contain paragraphs.
    D. Because p elements are not the first or last child of section.

    Solution

    1. 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.
    2. Step 2: Check if p is first or last child

      If other elements (like headings or divs) come before or after the p, then p is not first or last child, so styles won't apply.
    3. Final Answer:

      Because p elements are not the first or last child of section. -> Option D
    4. Quick Check:

      :first-child and :last-child depend on element position, not type [OK]
    Hint: Check if element is truly first/last child, not just first/last of type [OK]
    Common Mistakes:
    • Assuming :first-child means first of type
    • Ignoring other sibling elements
    • Thinking syntax is wrong when it's position issue
    5. You want to style the first and last <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?
    hard
    A. ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; }
    B. ul:first-child li { font-weight: bold; } and ul:last-child li { font-style: italic; }
    C. ul li:first-child { font-weight: bold; } and ul li:last-child { font-style: italic; }
    D. ul li:first-of-type { font-weight: bold; } and ul li:last-of-type { font-style: italic; }

    Solution

    1. Step 1: Understand the requirement

      We want to style only li elements that are the first or last child of their ul parent.
    2. Step 2: Choose selectors that target direct children and correct position

      ul > li:first-child and ul > li:last-child select li elements that are direct children and first or last child respectively. This matches the requirement exactly.
    3. 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 to ul. ul li:first-of-type { font-weight: bold; } and ul li:last-of-type { font-style: italic; } uses :first-of-type which selects first li regardless of position among siblings.
    4. Final Answer:

      ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } -> Option A
    5. Quick Check:

      Direct child + :first-child and :last-child = ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } [OK]
    Hint: Use direct child combinator > with :first-child and :last-child [OK]
    Common Mistakes:
    • Omitting > combinator causing wrong matches
    • Confusing :first-child with :first-of-type
    • Applying selectors to wrong parent element