0
0
CSSmarkup~20 mins

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

Choose your learning style9 modes available
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.